You can try out this sample app using the following commands on your local env.
pip install streamlit-webrtc opencv-python-headless matplotlib pydub streamlit run https://raw.githubusercontent.com/whitphx/streamlit-webrtc-example/main/app.py
Converting your voice into text in real time. This app is self-contained; it does not depend on any external API.
It applies wide variety of style transfer filters to real-time video streams.
(Online demo not available)
You can create video chat apps with ~100 lines of Python code.
MediaPipe is used for pose estimation.
pip install -U streamlit-webrtc
app.py with the content below.
from streamlit_webrtc import webrtc_streamer webrtc_streamer(key="sample")
Unlike other Streamlit components,
webrtc_streamer() requires the
key argument as a unique identifier. Set an arbitrary string to it.
Then run it with Streamlit and open http://localhost:8501/.
streamlit run app.py
You see the app view, so click the "START" button.
Then, video and audio streaming starts. If asked for permissions to access the camera and microphone, allow it.
app.py as below and run it again.
from streamlit_webrtc import webrtc_streamer import av class VideoProcessor: def recv(self, frame): img = frame.to_ndarray(format="bgr24") flipped = img[::-1,:,:] return av.VideoFrame.from_ndarray(flipped, format="bgr24") webrtc_streamer(key="example", video_processor_factory=VideoProcessor)
Now the video is vertically flipped.
As an example above, you can edit the video frames by defining a class with a callback method
recv(self, frame) and passing it to the
The callback receives and returns a frame. The frame is an instance of
av.AudioFrame when dealing with audio) of
You can inject any kinds of image (or audio) processing inside the callback. See examples above for more applications.
Note that there are some limitations in this callback. See the section below.
The callback methods (
VideoProcessor.recv() and similar ones) are executed in threads different from the main thread, so there are some limitations:
st.write()) do not work inside the callbacks.
globalkeyword, which also does not work in the callbacks properly.
As stated above, you cannot directly pass variables from/to outside and inside the callback and have to consider about thread-safety.
Usual cases are
The solution is to use the properties of the processor object which is accessible via the context object returned from
webrtc_streamer() as below.
class VideoProcessor: def __init__(self): self.some_value = 0.5 def recv(self, frame): img = frame.to_ndarray(format="bgr24") ... self.do_something(img, self.some_value) # `some_value` is used here ... return av.VideoFrame.from_ndarray(img, format="bgr24") ctx = webrtc_streamer(key="example", video_processor_factory=VideoProcessor) if ctx.video_processor: ctx.video_processor.some_value = st.slider(...) # `some_value` is set here
The sample app,
app.py has many cases where this technique is used and can be a hint for this topic.
When deploying apps to remote servers, there are some things you need to be aware of.
getUserMedia() API to access local media devices, and this method does not work in an insecure context.
This document says
A secure context is, in short, a page loaded using HTTPS or the file:/// URL scheme, or a page loaded from localhost.
So, when hosting your app on a remote server, it must be served via HTTPS if your app is using webcam or microphone.
Streamlit Cloud is a recommended way. You can easily deploy Streamlit apps with it, and most importantly for this topic, it serves the apps via HTTPS automatically by defualt.
Video streaming does not work in some network environments. For example, in some office or public networks, there are firewalls which drop the WebRTC packets.
In such environments, setting up a TURN server is a solution. See https://github.com/whitphx/streamlit-webrtc/issues/335#issuecomment-897326755.
Currently there is no documentation about the interface. See the example app.py for the usage. The API is not finalized yet and can be changed without backward compatiblity in the future releases until v1.0.
VideoTransformerBase and its
transform method have been marked as deprecated in v0.20.0. Please use
Note that the signature of the
recv method is different from the
transform in that the
recv has to return an instance of
av.AudioFrame. See the samples in app.py.
streamlit-webrtcto build a real-time computer vision app.
streamlit-webrtchas been introduced and discussed about.