Error while deploying an app with mediapipe

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

This is how my requirements file looks.
screenshot

Use

opencv-python-headless

instead.

1 Like

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

I can’t see an error message in any of your posts.

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.

1 Like

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)

Yes. That’s my repo. Thank you for your reply. I get it.

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.

Thank you for your suggestions @blackary . Will definitely try this.

@ajaygithub2 Try this one:

requirements.txt

streamlit
mediapipe

packages.txt

libgl1-mesa-glx
libglib2.0-0

test.py

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.