Member-only story
Real-Time Video Streams With Streamlit-WebRTC
Handling and transmitting real-time video over the network

By reading this piece, you will learn to build a sharable web app with real time video and audio streams. This is extremely useful for showcasing any computer vision and audio processing related projects. The whole implementation will be entirely in Python and can access it via URL from their smartphones or computers with webcam capability.
Have a look at the following gif which showcases one of my past research project based on the PyTorch implementation of AnimeGANv2.

This tutorial covers the basic implementation of creating a real-time video stream web app using a Python package called streamlit-webrtc. Based on the official repository in Github, this package provides
… real-time video and audio streams over the network, with Streamlit.
It is built on top of Streamlit, which is an open-source framework that turns Python data scripts into shareable web apps.
Let’s proceed to the next section and start installing the necessary modules.
Setup
It is highly recommended to create a new virtual environment before you continue with the installation.
Activate it and run the following command to install streamlit-webrtc
:
pip install streamlit-webrtc
The installation might take some time as there are quite a number of dependencies required for streamlit.
Implementation
In the same working directory, create a new file called app.py
.
Import
Add the following import statement at the top of the file:
import streamlit as st
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, RTCConfiguration
import av
import threading
av
contains Pythonic bindings for FFmpeg’s libraries. It will be used to construct a frame from a PIL.Image
or a numpy array.