Access Webcam Video from a hosted Streamlit application

I am running streamlit to process the webcam video. I hosted this application on the web on a server (heroku). The problem is that I couldn’t get the application to work because it can’t not get access to the webcam. (I use OpenCV to access the webcam video on my local machine cap=cv.VideoCapture(0) )

Can any one of you help me with that ?

Hi @amineHY, and welcome to Streamlit!

Streamlit doesn’t currently have any browser-based camera support, so this isn’t possible right now. It seems like a really interesting use-case that we’d potentially like to support - but it also presents some real architectural challenges.

In particular, it’s hard to imagine how this could be done in a way that’s easily compatible with Streamlit’s execution model, which involves rerunning the app script from top to bottom each time an input changes. (We probably wouldn’t want to re-run an entire Streamlit app 30 times per second for each attached video stream.)

I’ve opened a feature request here, so that we can track this internally. In the meantime, can you describe your use case so that we have a better sense of how this might be implemented? (What sort of processing are you doing on the video frames? Is the processing done in real-time, or batched? Do you use every frame from a video stream, or select samples at some frequency?)


1 Like

Hi @tim,

Thank you for the reply, for the moment, I am working on a project to perform computer vision (in general). I successfully built the app and it works great locally process locally a video from a webcam/url/file. After hosting the app on a web server, the app works fine when using a video file from a url or file (copied on the server for testing but I can not get the video from the webcam to run on the server. I am not an expert, but I’ve read that I need to create a server / client communication protocol, using Flask/gunicorn for instance. At this moment streamlit does not support that and this is might not be a request feature anyway
You made a good point when you mentioned the 30 times re-run of the streamlit app, that justify my lack of experience in streaming video to the browser. Although I am not sure of that.

A typical use case is that a user launches a web page of the streamlit application and chose the source of the video, for instance, the webcam, then the user can choose a computer vision model (say, object detection or classification) algorithm and see the result in real-time. Another use case is to apply the same processing but not for the webcam video, but for a video that comes from an IP camera. This is a type of uses cases I can think if I want to visualize the result in real-time.

I don’t know if streamlit is meant to perform a similar task, or it is better to use a side solution.

Thank you again @tim for creating the FR.

A workaround solution to this problem is welcome.

Hi @amineHY,

Regarding Flask there is another topic that seems to be related to this: Streamlit restful app

Please let us know if this helps you

There is still no way to open the camera using streamlit?

Not currently.

1 Like

We’re working on a solution that will allow the webcam to be used in Streamlit Components (this isn’t currently possible for sandboxing reasons). There’s no specific date on this, but it should be happening soon!

1 Like