背景
想要尝试在python3环境下使用opencv进行图像的变换处理,所以需要在centos7的系统里面安装opencv(注:在centos7同时存在python2和python3)
安装尝试
1,尝试使用yum安装出现的问题
使用yum查找,能够找到安装包,并安装成功
# yum search opencv
......
opencv-devel-docs.noarch : Development files for using the OpenCV library
opencv-python.x86_64 : Python bindings for apps which use OpenCV
opencv.i686 : Collection of algorithms for computer vision
opencv.x86_64 : Collection of algorithms for computer vision
......
# yum install opencv-python
......
0:1.3.7-1.el7
python-setuptools.noarch 0:0.9.8-7.el7 which.x86_64 0:2.20-7.el7
Complete!
但是在import的时候出现错误,python2,python3都存在同样的问题(电脑上有两个版本python2,3,python默认是python2)
# python3
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
上面的问题是找不到对应的模块导致的,通过find找到安装包的位置。看下面的位置按说python2应该可以import成功,但是实际不能(证明在包的引入过程中没有把下面的路径包含进去)
参考https://www.cnblogs.com/suntiger/p/6674548.html 操作,在python2的环境可以正常使用import cv2。但是问题是我的代码是在python3执行,在python3的环境下还是不行。
find / -name 'cv2.so'
/usr/lib64/python2.7/site-packages/cv2.so
2,尝试使用pip3安装opencv
为了在python3上使用,将上面安装的opencv卸载掉(yum remove opencv-python),重新使用pip3尝试安装。
# pip3 install opencv-python
(1)出现第一个错误:报错Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-i087q3mf/opencv-python/,貌似是由于pip没有安装导致(目前有pip3,但是没有pip)。解决办法:安装pip
pip3 install --upgrade pip
(2)重新使用pip3安装opencv-python,出现下面的错误:
ERROR: Cannot uninstall 'numpy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
解决办法:先卸载掉numpy(yum remove numpy)
重新安装即可完成opencv-python的安装,在python3上面能够正常import
# python3
>>> import cv2
>>>
参考:https://blog.csdn.net/weixin_43211480/article/details/90097888