Reputation: 103
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?
Upvotes: 10
Views: 16563
Reputation: 223
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/
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
Upvotes: 4
Reputation: 1529
Actually, this issue has been raised for MacOS Catalina. I know the ask to solve this is painful but as of now the only clean way is to remove/uninstall anaconda completely and then do a fresh reinstall with installation directory set as /Users//anaconda3. Currently if you would notice this is in /opt which is not appropriate for MacOS Catalina. I may also recommend to use the command line installer.
More info: https://www.anaconda.com/blog/how-to-restore-anaconda-after-macos-catalina-update
Issue report: https://github.com/ContinuumIO/anaconda-issues/issues/10998.
Upvotes: 0
Reputation: 2085
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.
Upvotes: 16
Reputation: 11
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:
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+
brew install opencv
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
Upvotes: 1