Displaying images from opencv


I am thinking of using streamlit on a headless machine on which I run opencv that acquires frames from a webcam.

I am just testing this simple script:

import cv2
import streamlit as st
import time

cap = cv2.VideoCapture(0)


my_placeholder = st.empty()

while True:

        success, img = cap.read()
        my_placeholder.image(img, use_column_width=True)

        if cv2.waitKey(1) & 0xFF == ord('q'):
                break  # wait for ESC key to exit


which has 2 limits:

  1. I am forced to slow down the while loop ( when I am going to add some ML it will not be a problem)
  2. I get from streamlit the following errors:
MediaFileManager: Missing file 78c16466698a216a45ce28c152528e16a6437e66caf1926d8c4aa61d
MediaFileManager: Missing file f60585d35644b3cf69098c6e28c630230a5e1283f0543a625bbb8aff
MediaFileManager: Missing file 4d3f29b5ba094e1f9ad5c2e9cb85d290d3bbb6ba4a3037c124cdbdc6

In other words I want to display on a remote machine the results of image recognition algorithms using streamline web interface.

any suggestions on how to improve it and solving the errors…
pls the code is short, try on your own.




Hi @Blu-Eagle welcome to the forum !

Maybe a long shot, but your issue looks pretty similar to the 3 issues @nthmost is working on on this PR : https://github.com/streamlit/streamlit/pull/1494.

For now the MediaFileManager struggles to display rapid-fire images, which looks like what you are doing. I say let’s track this merge request (which should be soon) and test on your code snippet when it’s been merged.

In the meantime you can try to slowdown your image display with a higher timer.sleep() period and see if that solves your issue temporarily.



Hi guys,

Just to be clear, the MFM’s issue is that it is too fast, not too slow. :slight_smile:

I’m introducing a delay in the MediaFileManager’s garbage collection so that browsers have a few seconds’ “grace period” to successfully request the file before Streamlit considers it obsolete / replaced by a newer file.