[Solved] Import cv2- no module exists error while deploying

Hi,
My streamlit app runs fine locally. I am doing image processing hence using opencv
But when I deploy the app it says no module found import cv2
I have added opencv in the requirements.txt file
The entire code is on my github here is the link: https://github.com/aniketwattamwar/Image-Processing-with-Streamlit
Any help is appreciated
Thanks

Hi, @aniketwattamwar can you please share the requirements.txt file, did you correctly mention opencv, it should be mentioned as opencv-python not just opencv, if it doesn’t work then try mentioning opencv-python-headless in requirements.

image
I did put opencv-python
Now that you mentioned put opencv-python-headless as well.
But I am still getting the same error.
This is the error(image below)

1 Like

I hosted on streamlit sharing,
so change the requirements.txt as follows

numpy
opencv-python-headless

This successfully worked for me, Hope this helps @aniketwattamwar.

By the way while running the code I encountered few errors:

  1. In Image Processing
    Contours
    ValueError: not enough values to unpack (expected 3, got 2)

  2. In Feature Detection in images
    SIFT
    AttributeError: module ‘cv2.cv2’ has no attribute ‘xfeatures2d’

After fixing the error and successfully deploying, please edit the title and add [Solved] at the beginning, it will be helpful for similar error encounters.

1 Like

Hi @sree369nidhi
Thanks a ton !
It is deployed now.

As for the errors, it runs perfectly on my local machine because I have an older version of opencv

In the newer versions of opencv, algorithms like SIFT, etc are not there.

I’ll update the code accordingly

Thanks again
My issue is resolved

1 Like

I’m having this same issue however the fix didn’t seem to work. I am using python 3.8.6 and this is what my requirements.txt looks like.
numpy==1.19.2
opencv-python==4.4.0.44

My python version was 3.6 and opecncv version was 3.4.1. I am not sure but it is just version issue.

Did you try putting opencv-python-headless ?
Maybe you could also try opencv-contrib-python.

Check out this link: https://pypi.org/project/opencv-contrib-python/

The problem can also be solved on Streamlit sharing by providing a packages.txt file like the following:

7 Likes