Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV giving an error whenever import cv2 is used

I installed cv2 with pip3 install opencv-contrib-python on terminal and it worked, but on the python IDLE whenever I try to import cv2 or run a vscode file with cv2 imported it says

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/cv2.cpython-38-darwin.so, 2): Symbol not found: _inflateValidate
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/.dylibs/libpng16.16.dylib (which was built for Mac OS X 10.13)
  Expected in: /usr/lib/libz.1.dylib
 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/.dylibs/libpng16.16.dylib

in the terminal. Any idea how to fix this?

like image 316
乇乂ㄒ尺卂 ㄒ卄丨匚匚 Avatar asked Feb 17 '20 01:02

乇乂ㄒ尺卂 ㄒ卄丨匚匚


People also ask

How do I fix import cv2 error?

The Python "ModuleNotFoundError: No module named 'cv2'" occurs when we forget to install the opencv-python module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install opencv-python command.

How do you fix import cv2 could not be resolved?

It just happened to me and I solved it installing both opencv-python and opencv-python-headless with pip and reloading the Visual Studio Code window right after it.

Why can't I import cv2 in python?

importerror no module named cv2 error occurs when cv2 module is not properly installed or its path is not properly set or configured. The straight way fix for this error (no module named cv2) is to reinstall this module (OpenCV-python). In some scenario reinstalling this module automatically remove the older version.

Why is import cv2 not working Jupyter?

One of possibility is that you could have written import cv2 and its utilisation in separate cells of jupyter notebook. If this is the case then first run the cell having import cv2 part and then run the cell utilising the cv2 library.

Is OpenCV compatible with High Sierra?

I am on High Sierra and opencv 4.5 was incompatible and kept giving the error mentioned by OP. You post made it work ! OpenCV is also referred to as cv2 in Python. The installation of OpenCV varies betweenoperating systems, so below Iam providing instructions for Windows, Mac, and Linux:

Does OpenCV work on Windows 7?

Yes, only thing you need to do is run pip install opencv-contrib-python. I tested the command and the import worked on Windows 10. I have no reason to believe that it wouldn't work on Windows 7.

How do I install OpenCV in Anaconda?

OpenCV also requires that numpy be installed as well. matplolib is also recommended to be installed. You'll also have to locate the two .dll files in the bin folder of your opencv folder. And copy and paste them into the ffmpeg/bin folder as well as into the site-packages folder in your Anaconda folder.


3 Answers

I had the same question and I found it's because I use a high version of opencv (4.X.X), and my system version is low (mac os 10.12.5). So I installed a lower version of opencv (3.4.5.20), and then the question is solved.

You can use the following command to list the versions of opencv:

pip install opencv-python==

ERROR: Could not find a version that satisfies the requirement opencv-python== (from versions: 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.6.27, 3.4.7.28, 3.4.8.29, 3.4.9.31, 4.0.0.21, 4.0.1.24, 4.1.0.25, 4.1.1.26, 4.1.2.30, 4.2.0.32) ERROR: No matching distribution found for opencv-python==

Then you can try a lower version (3.4.5.20, for example), and install it using:

pip install opencv-python==3.4.5.20

Then you can retry import cv2 to see whether the question is solved.

like image 68
shilaimuslm Avatar answered Oct 26 '22 12:10

shilaimuslm


I was puzzling around for a few hours and the thing that worked for me was to downgrade opencv (cv2) version via pip in conda enviroment (I am big fan of virtualenv but does not work smoothly with apple silicon M1 for now).

Follow these steps in activated virtual enviroment. To set virtual enviroment follow "Install conda" section from https://sayak.dev/install-opencv-m1/

  1. pip3 uninstall opencv-python (equivalent is python3 -m pip uninstall opencv-python)
  2. pip3 install opencv-python==randomwords (equivalent is python3 -m pip install opencv-python==randomwords)
  3. Result should be error message which contains pip available versions for opencv (pick one)
  4. pip3 install opencv-python==4.4.0.40 (my version of choice)
  5. Verify with python3 -c "import cv2"

If there is not traceback you are good to go!

Notes:

Tested with python version 3.8.6

Operating system: MacOS Big Sur 11.3.1

like image 21
Pr0ph3t_sec Avatar answered Oct 26 '22 14:10

Pr0ph3t_sec


OpenCV is also referred to as cv2 in Python.

The installation of OpenCV varies betweenoperating systems, so below Iam providing instructions for Windows, Mac, and Linux:

Installing OpenCV on Windows

1.Open the command line and type:

pip install opencv-python

2.Then open a Python session and try:

import cv2

3.If you get no errors, then OpenCV has been successfully installed and you can skip the next steps.

4.If there is an error (usually saying that DLL load failed) then please download a precompiled wheel (.whl) file from this link and install it with pip. Make sure you downloadthe correct file for your Windows version and your Python version. For example, forPython 3.6 onWindows 64-bit you would do this:

pip install opencv_python3.2.0cp36cp36mwin_amd64.whl

5.Then try to import cv2 in Python again. If there's still an error, then please type the following again in the command line:

pip install opencv-python

6.Now you should successfully importcv2 in Python.

Installing OpenCV on Mac

Currently some functionalities of OpenCV are not supported for Python 3 on Mac OS, so it's best to install OpenCV for Python 2and use Python 2 to run the programsthat containscv2 code. Its' worth mentioning that Python 2 is installed by default on Mac, so no need to install Python 2. Here are the steps to correctly install OpenCV:

  1. Install brew:

Open your terminal and paste the following:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2. OpenCV depends on GTK+, so please install that dependencyfirst with brew (always from the terminal):

brew install gtk+

  1. Install OpenCV with brew:

brew install opencv

  1. Open Python 2 by typing:

python

  1. Import cv2 in Python:

import cv2

If you get no errors, that means OpenCV has been successfully installed.

Installing OpenCV on Linux

1.Please open your terminal and execute the following commands one by one:

sudo apt-get install libqt4-dev cmake -D WITH_QT=ON .. make sudo make install 2.If that doesn't work, please execute this:

sudo apt-get install libopencv-* 3.Then install OpenCV with pip:

pip install opencv-python

  1. Import cv2 in Python.If there are no errors, OpenCVhas been successfully installed.
like image 1
chandnii Avatar answered Oct 26 '22 13:10

chandnii