Skip to main content

No project description provided

Project description

streamlit-webrtc

Handling and transmitting real-time video/audio streams over the network with Streamlit Open in Streamlit

Tests Frontend Tests

PyPI PyPI - Python Version PyPI - License PyPI - Downloads

GitHub Sponsors

Buy Me A Coffee

Examples

⚡️Showcase including following examples and more: 🎈Online demo

  • Object detection
  • OpenCV filter
  • Uni-directional video streaming
  • Audio processing

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

⚡️Real-time Speech-to-Text: 🎈Online demo

It converts your voice into text in real time. This app is self-contained; it does not depend on any external API.

⚡️Real-time video style transfer: 🎈Online demo

It applies a wide variety of style transfer filters to real-time video streams.

⚡️Video chat

(Online demo not available)

You can create video chat apps with ~100 lines of Python code.

⚡️Tokyo 2020 Pictogram: 🎈Online demo

MediaPipe is used for pose estimation.

Install

$ pip install -U streamlit-webrtc

Quick tutorial

Create 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. Basic example of streamlit-webrtc

Next, edit 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. Vertically flipping example

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 video_processor_factory argument. The callback receives and returns a frame. The frame is an instance of av.VideoFrame (or av.AudioFrame when dealing with audio) of PyAV library.

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.

Limitations

The callback methods (VideoProcessor.recv() and similar ones) are executed in threads different from the main thread, so there are some limitations:

  • Streamlit methods (st.* such as st.write()) do not work inside the callbacks.
  • Variables outside the callbacks cannot be referred to from inside, and vice versa.
    • It's impossible even with the global keyword, which also does not work in the callbacks properly.
  • You have to care about thread-safety when accessing the same objects both from outside and inside the callbacks.

A technique to pass values between inside and outside the callbacks

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

  • to change some parameters used in the callback to new values passed from the main scope.
  • to refer to the results of some processing inside the callback from the main scope.

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

If the passed value is a complex object, you may also have to consider about using something like threading.Lock or queue.Queue for thread-safety.

The sample app, app.py has many cases where this technique is used and can be a hint for this topic.

Serving from remote host

When deploying apps to remote servers, there are some things you need to be aware of.

HTTPS

streamlit-webrtc uses 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. If not, you will encounter an error when starting using the device. For example, it's something like below on Chrome.

Error: navigator.mediaDevices is undefined. It seems the current document is not loaded securely.

Streamlit Cloud is a recommended way for HTTPS serving. You can easily deploy Streamlit apps with it, and most importantly for this topic, it serves the apps via HTTPS automatically by defualt.

Network connectivity

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.

API

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.

For users since versions <0.20

VideoTransformerBase and its transform method have been marked as deprecated in v0.20.0. Please use VideoProcessorBase#recv() instead. Note that the signature of the recv method is different from the transform in that the recv has to return an instance of av.VideoFrame or av.AudioFrame. See the samples in app.py.

Resources

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit-webrtc-0.33.0.tar.gz (953.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_webrtc-0.33.0-py3-none-any.whl (963.1 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-webrtc-0.33.0.tar.gz.

File metadata

  • Download URL: streamlit-webrtc-0.33.0.tar.gz
  • Upload date:
  • Size: 953.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.12 Linux/5.11.0-1022-azure

File hashes

Hashes for streamlit-webrtc-0.33.0.tar.gz
Algorithm Hash digest
SHA256 39c1240e3d0a369b9655dc45e68a46a5200a993ba22688215f1b71036bc34a4f
MD5 0f5b10eea26f4bfe273eb5436d53e9f0
BLAKE2b-256 b70a38b3b78f8b2754852afefda9c98634c37dcf43a1db15acea950a9dc5902d

See more details on using hashes here.

File details

Details for the file streamlit_webrtc-0.33.0-py3-none-any.whl.

File metadata

  • Download URL: streamlit_webrtc-0.33.0-py3-none-any.whl
  • Upload date:
  • Size: 963.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.12 Linux/5.11.0-1022-azure

File hashes

Hashes for streamlit_webrtc-0.33.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7866d4b7cb0e57c9e00bcf6ec1dc90bd6d9f7d7a38521238880b18bb09930632
MD5 f85885b60f3d043f2a84caab4e00d30b
BLAKE2b-256 86d5831a1191f8fdb450dc1ffd461f3d1575fd501a14537b7dbbb9f4148e5f0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page