Mac下安装OpenCV2 for Python 3.7

Mac下安装OpenCV2 for Python 3.7

最近需要调试一个有关摄像头检测的Python程序,程序需要OpenCV这个库,所以需要在我的MAC上安装opencv-python。由于是第一次安装opencv-python,这一路遇到不少坑,经过一翻折腾(中途多次想到过一句话,从入门到放弃),最终还是看到了成功的标志,以下就是这其中的曲折过程。

首先,通过pip安装 opencv-python

Pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python

不加“https://pypi.tuna.tsinghua.edu.cn/simple opencv-python”pip3 install opencv-python会提示

Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 65] No route to host')': /simple/opencv-python/

  Could not find a version that satisfies the requirement opencv-python (from versions: )

No matching distribution found for opencv-python

后来测试 pip3 install opencv-python这种方式偶尔可用,不过速度极其的慢,还容易断掉,几十M的文件要2小时,所以网不好的还是不建议使用了。

安装过程及安装完成后提示:

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple

Collecting opencv-python

  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/68/81/20771fab16b8ccd5f642a44ef355e274075754dc002b5d6f6e9779e715d8/opencv_python-4.2.0.32-cp37-cp37m-macosx_10_9_x86_64.whl (47.9MB)

    100% |████████████████████████████████| 47.9MB 481kB/s

Requirement already satisfied: numpy>=1.14.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from opencv-python) (1.16.4)

Installing collected packages: opencv-python

Successfully installed opencv-python-4.2.0.32

 

也可以到https://pypi.tuna.tsinghua.edu.cn/simple opencv-python  上直接下载文件安装 。

sudo pip3 install opencv_python-4.2.0.32-cp37-cp37m-macosx_10_9_x86_64.whl

WARNING: The directory '/Users/apple/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Processing ./opencv_python-4.2.0.32-cp37-cp37m-macosx_10_9_x86_64.whl

Requirement already satisfied: numpy>=1.14.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from opencv-python==4.2.0.32) (1.16.4)

Installing collected packages: opencv-python

Successfully installed opencv-python-4.2.0.32

提示opencv-python安装成功后,打开Python执行导入opencv验证一下,

>>> import cv2

执行后没有任何提示,网上好多贴子说没有任何提示就是成功,这个说法在我的环境上不完全对,在后来的验证过程中,有时没提示,也是错的。Import cv2没有提示后我们一般会执行

>>> print(cv2.__version__)

如果cv2成功引用,则会输出一个版本号,否则会提示__version__这个方法不存在。

失败后猜测是安装问题,卸载后通过先下载.whl,再执行安装

sudo pip3 install opencv_python-4.2.0.32-cp37-cp37m-macosx_10_9_x86_64.whl 成功后 提示Successfully installed opencv-python-4.2.0.32

 

再执行>>> import cv2

提示

>>> import cv2

Traceback (most recent call last):

  File "", line 1, in

  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/__init__.py", line 3, in

    from .cv2 import *

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: _clock_gettime

  Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/.dylibs/libavutil.56.31.100.dylib

  Expected in: /usr/lib/libSystem.B.dylib

 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/.dylibs/libavutil.56.31.100.dylib

这个提示困扰了我一下午,在网上翻资料,各种试,都无效,就要准备暂时放弃时,想过有人说过版本问题,那我就再试最后一次,opencv-python的版本降一下试试。

在https://pypi.tuna.tsinghua.edu.cn/simple opencv-python 下载了opencv_python-4.0.0.21-cp37-cp37m-macosx_10_6_x86_64这个版本。安装之前先执行sudo pip3 uninstall opencv_python-4.2.0.32-cp37-cp37m-macosx_10_9_x86_64.whl卸载之前的版本,执行过程中提示Proceed (y/n)? y 回答y然后提示

  Successfully uninstalled opencv-python-4.2.0.32 卸载成功后按前面的方法安装新版本。成功后 ,打开python, >>>后输入import cv2,执行无任何提示,嗯,有希望,>>> 后再输入print(cv2.__version__)执行,提示4.0.0OK,终于成功!!!

 

最后总结,做为一个opencv新手,不能太依赖pip3这种自动安装,我一直认为它会自己找适合的版本下载安装。把这个过程写下来,一是以后再配置环境可以参考,二是能给像我一样的新手小白一些参考,少浪费一些时间。

 

 

 

 

 

你可能感兴趣的:(python,opencv)