I’m making an app that can take pictures from a video livestream using the Streamlit-webrtc library and I’m using an example that can take picture from the livestream but the picture gets replaced every time I press the snapshot button.
I’ve got the code here:
import threading
from typing import Union
import av
import numpy as np
import streamlit as st
from PIL import Image
import cv2
import os
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
def main():
class VideoTransformer(VideoTransformerBase):
frame_lock: threading.Lock # `transform()` is running in another thread, then a lock object is used here for thread-safety.
out_image: Union[np.ndarray, None]
def __init__(self) -> None:
self.frame_lock = threading.Lock()
self.out_image = None
def transform(self, frame: av.VideoFrame) -> np.ndarray:
out_image = frame.to_ndarray(format="bgr24")
with self.frame_lock:
self.out_image = out_image
return out_image
ctx = webrtc_streamer(key="snapshot", video_transformer_factory=VideoTransformer)
if ctx.video_transformer:
snap = st.button("Snapshot")
if snap:
with ctx.video_transformer.frame_lock:
out_image = ctx.video_transformer.out_image
if out_image is not None:
st.write("Output image:")
st.image(out_image, channels="BGR")
my_path = os.path.abspath(os.path.dirname(__file__))
cv2.imwrite(os.path.join(my_path, "../Data/"+"filename.jpg"), out_image)
else:
st.warning("No frames available yet.")
if __name__ == "__main__":
main()
I’d like to modify the code so that the app can take a new picture every time without replacing the last one.