I am deploying an app. It’s a very basic app. But when deploying, it shows the following error.
File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)File "/app/hand-detection/app.py", line 2, in <module>
import cv2
Just tried opencv-python-headless but still getting the same error.
In fact, I tried removing the import cv2 line from code. Then I am getting stuck at importing mediapipe.
Here is the error-
File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)File "/app/hand-detection/app.py", line 3, in <module>
import mediapipe as mpFile "/home/appuser/venv/lib/python3.10/site-packages/mediapipe/__init__.py", line 16, in <module>
import mediapipe.python.solutions as solutionsFile "/home/appuser/venv/lib/python3.10/site-packages/mediapipe/python/solutions/__init__.py", line 17, in <module>
import mediapipe.python.solutions.drawing_stylesFile "/home/appuser/venv/lib/python3.10/site-packages/mediapipe/python/solutions/drawing_styles.py", line 20, in <module>
from mediapipe.python.solutions.drawing_utils import DrawingSpecFile "/home/appuser/venv/lib/python3.10/site-packages/mediapipe/python/solutions/drawing_utils.py", line 19, in <module>
import cv2
HI @ajaygithub2, it appears that mediapipe depends on opencv. Have you tried looking around the forum for solutions people have found when trying to use mediapipe or opencv? Based on these instructions Installation | MediaPipe | Google Developers it appears that there are a number of dependencies for getting mediapipe working.
You might also look around on GitHub for other people who have created streamlit apps with mediapipe. Here are a few:
These aren’t guaranteed to be using the best practices, and they may be a bit out of date, but at least you can see things like “what did they put in requirements.txt”, “what did they put in packages.txt”, etc. My guess is that you will probably need to add some packages to packages.txt to get mediapipe working properly.
If this is your repo: https://github.com/ajaygithub2/hand-detection
your app won’t work anyway in any hosted environment, because you cannot use direct hardware access e.g. cv2.VideoCapture(0)
I just tried this. When I import cv2 using opencv-python-headless without mediapipe, it works just fine.
When I import mediapipe without opencv-python or opencv-python-headless, it works just fine.
But when I import both of them in any combo like mediapipe and opencv-python or mediapipe and opencv-python-headless, I am getting errors.
import streamlit as st
import cv2
import mediapipe as mp
print(st.__version__)
print(cv2.__version__)
print(mp.__version__)
gave me the folowing log output in a Python 3.9. environment
1.21.0
4.7.0
0.9.2.1
The above quick solution should work. IMHO the problem with mediapipe is, that it enforces the installation of a non-headless version of opencv, which causes issues in a headless environment. If i have the time, i will elaborate more on this.
Thank you so much… It works like a charm. After testing this it imports both cv2 and mediapipe. You have been so helpful. I understand what you are saying. When I checked the logs, I found that it was uninstalling and installing opencv a number of times. It must be because opencv is a dependency of mediapipe. That’s sorted now, thanks to you.
I understand that you are a busy person, so if you don’t have time to answer the following, its fine.
I encountered another problem. Since, I can’t capture local webcam using cv2.VideoCapture(), I thought I would get the frames using webrtc. But when I try getting the webcam feed, it works just fine locally, but when I do the same with a repo online, it tries to do something Connection Pair and then shows ICE state connection is closed. It is able to access my webcam, as it asks for permission and my webcam light turns on for a minute, but then the above line shows up and it stops taking in the feed.
I also tried using some other person’s web app which uses webrtc to access webcam and the same thing happens there too. It accesses my webcam for a minute and then cuts the connection.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.