New Component: streamlit-webrtc, a new way to deal with real-time media streams

@Joe_Tay

Hi,

Please paste the complete code that I can copy, paste and run to reproduce the problem.

Though I haven’t tested but just have seen it, let me point out some potential problems;

@whitphx

Hi,

Thank you for your reply and attention. I am trying to capture a series of high res images. Here is my code as requested:

Blockquote

import streamlit as st
import numpy as np
import pandas as pd
import threading
from typing import Union
import cv2
import av
import os
from streamlit_webrtc import VideoProcessorBase, webrtc_streamer

img_counter = 5

df = pd.DataFrame({
‘first column’: [‘Self Training’, ‘Self Analyse’],
})

#sports option in side bar
s_option = st.sidebar.selectbox(
‘Select anlysis mode :’,
df[‘first column’])

def main():
class VideoTransformer(VideoProcessorBase):
frame_lock: threading.Lock # transform() is running in another thread, then a lock object is used here for thread-safety.
in_image: Union[np.ndarray, None]

    def __init__(self) -> None:
        self.frame_lock = threading.Lock()
        self.in_image = None
        self.img_list = []

    def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
        in_image = frame.to_ndarray(format="bgr24")
        list_img = frame.to_ndarray(format="bgr24")

        global img_counter

        with self.frame_lock:
            self.in_image = in_image
            if img_counter > 0:
                #print("capturing image")
                self.img_list.append(list_img)
                img_counter -= 1

        return av.VideoFrame.from_ndarray(in_image, format="bgr24")

ctx = webrtc_streamer(key="snapshot", video_processor_factory=VideoTransformer)

if ctx.video_transformer:
    if st.button("Snapshot"):
        with ctx.video_transformer.frame_lock:
            in_image = ctx.video_transformer.in_image
            img_list = ctx.video_transformer.img_list
            

        if in_image is not None: #put in column form 5 images in a row
            st.write("Input image:")
            st.image(in_image, channels="BGR")
            st.image(img_list[0], channels="BGR") #lower resolution than the one above in_image
        else:
            st.warning("No frames available yet.")

if name == “main”:
main()

Hi , I manage to solve the problem on my own. Thank you.