Path: blob/main/examples/gallery/streaming_videostream.ipynb
2011 views
Streaming Video: Live video streams made easy
In this example we will demonstrate how to develop a general tool to transform live image streams from the users web cam. We will be using Panels VideoStream
widget to record and stream the images.
We will also show how to apply blur, grayscale, sobel and face recognition models to the video stream.
Imports and Settings
Among other things we will be using numpy, PIL and scikit-image to work with the images.
We define the height and width of the images to transform. Smaller is faster. We also define the timeout, i.e. how often the videostream takes and streams a new image.
Base Image Models
We will need to define some base image models components. The base models are custom Panel components that inherit from Panels Viewer
class.
The base models makes it easy to later turn image to image algorithms into interactive UIs like the FaceDetectionModel
shown in the image just below.
Please note we restrict our selves to working with .jpg
images. The VideoStream
widget also support .png
images. But .png
images are much bigger and slower to work with.
Lets define a base model for working with PIL
images
Lets define a base model for working with Numpy
images.
Timer
Lets define a timer component to visualize the stats of the live videostream and the image transformations
VideoStreamInterface
The VideoStreamInterface
will be putting things together in a nice UI.
Lets define a helper function first
The VideoStreamInterface
will take a list of ImageModel
s. The user can the select and apply the models to the images from the VideoStream
.
Custom Image Models
We will now make specific image to image algorithms interactive.
Let us start with the Gaussian Blur algorithm.
Lets implement a Grayscale algorithm.
Lets implement the Sobel algorithm.
Lets implement the face detection model of scikit-image.
Please note that these models are just examples. You can also implement your own models using Scikit-Image, Pytorch, Tensorflow etc and use the VideoStreamInterface
to work interactively with them.
Its alive!
Lets define an instance of the VideoStreamInterface
Wrap it in a template
What makes Panel unique is that our components work very well in both the notebook and as standalone data apps.
We can wrap the component in the nicely styled FastListTemplate
to make it ready for production.
Serve it as a server side app
It is now possible to serve the live app via the command panel serve streaming_videostream.ipynb
. The app is the available at http://localhost:5006/streaming_videostream.
Serve it as a client side app
You can also panel convert
this app to web assembly for even better performance.
First you will need to create a requirements.txt
file with the following content
Then you can
Run
panel convert streaming_videostream.ipynb --to pyodide-worker --out pyodide --requirements requirements.txt
Run
python3 -m http.server
to start a web server locallyOpen http://localhost:8000/pyodide/streaming_videostream.html to try out the app.