Unable to import cv2

ImportError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).


File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/script_runner.py", line 430, in _run_script
    exec(code, module.__dict__)File "/app/facerecog/app3.py", line 2, in <module>
    import cv2File "/home/appuser/venv/lib/python3.9/site-packages/cv2/__init__.py", line 8, in <module>
    from .cv2 import *

Hey @AmeenBarech

Welcome to the Streamlit Community :tada::tada:

Since you haven’t listed your requirements.txt file so I am not certain about it but the most common fix for this error is replacing opencv-python with opencv-python-headless in the requirements file.

Do let me know if that fixes this issue or please attach some more details about the error.


1 Like

Hello @Kanak
thanks for giving time,
I have added opencv-python-headless in requirements.txt but still have the same error.
Help will be appreciated.


Can you share your repo link? Because it is really hard to guess the cause of the error this way.

repo: GitHub - mohammadameenbarech/faceroc
app3.py is main file.

  • Are there any other errors during deployment?
  • Besides the cv2 issue, i don’t think your app will work on Streamlit Cloud at all, because afaik cv2.VideoCapture() does not work this way, because it wants to access the local videocamera of the machine running streamlit, but the streamlit cloud runtime has no videocamera nor a videodriver at all.

Thanks @Franky1 ,
Can you please suggest me some alternatives, so I can access my web-cam on Streamlit Cloud.

Search for streamlit-webrtc in the forum

New Component: streamlit-webrtc, a new way to deal with real-time media streams

GitHub - whitphx/streamlit-webrtc: Real-time video and audio streams over the network, with Streamlit.

Access Webcam Video from a hosted Streamlit application

There is also a component streamlit-webcam but i am not sure how well it works, seems to be not maintained:

GitHub - tconkling/streamlit-webcam

Thanks @Franky1 <3

Hello, did you solve it? Im having the same issue.

We can’t use cv2.VideoCapture on cloud platform it only works on localhost

Hey @AmeenBarech,

You can check my app out - I managed to set up webcam access on Streamlit cloud.

Hey @angelicaba23,

Can you try replacing cv2 with opencv-python-headless?

A quick sidenote here, the opencv package is known to create some issue when deploying on Streamlit sharing. To bypass this, replace opencv-python with opencv-python-headless.