树莓派 python3 安装 opencv 最容易的方法(非编译方式)

所需环境

通过pip3安装的。
sudo pip3 install opencv-python imutils numpy scipy pillow
通过系统自带环境apt-get安装的。
sudo apt-get install libatlas-base-dev  libjasper-dev
sudo apt-get install libgstreamer1.0-0
sudo apt-get install libgstreamer-plugins-base1.0-0
sudo apt-get install libqtgui4  libqt4-test
## sudo apt-get install python3-pyqt5  #最后这个可以不安装的,因为文件比较大。根据实际情况安装

警告:

如果你没有对固件进行更新,很有可能opencv运行的时候还会缺少乱七八糟的库。

这种情况下的解决办法就必须更新固件(上述部分在zero上测试通过,在3b上发现有问题。如果上述操作依然没有解决你的问题,请执行下面的命令)

// 软件源更新
sudo apt-get update 
// 升级本地所有安装包,最新系统可以不升级,版本过高反而需要降级才能安装
sudo apt-get upgrade
// 升级树莓派固件,固件比较新或者是Ubuntu则不用执行
sudo rpi-update

如果还缺少.so文件。 运行下面指令,通过安装opencv2来把系统里面缺少的文件进行补充。

sudo apt install python-opencv
如果还有问题,可以留言提问。

下面是安装之后包的路径位置和安装过程中遇到bug的解决过程。

>>> import scipy
>>> scipy
<module 'scipy' from '/usr/local/lib/python3.5/dist-packages/scipy/__init__.py'>
>>> import imutils
>>> imutils
<module 'imutils' from '/usr/local/lib/python3.5/dist-packages/imutils/__init__.py'>
>>> import cv2
>>> cv2
<module 'cv2.cv2' from '/usr/local/lib/python3.5/dist-packages/cv2/cv2.cpython-35m-arm-linux-gnueabihf.so'>
>>> import numpy
>>> numpy
<module 'numpy' from '/usr/local/lib/python3.5/dist-packages/numpy/__init__.py'>
>>> import skimage.filters
>>> import PIL
>>> PIL
<module 'PIL' from '/usr/local/lib/python3.5/dist-packages/PIL/__init__.py'>

在树莓派上使用sudo apt install python3-pip安装pip的时候,出现cannot import name 'main’报错,这个时候通过修改/usr/bin/pip3来解决。

from pip import __main__

if __name__ == '__main__':
    sys.exit(__main__._main())

安装python opencv包的时候,sudo apt install python-opencv会安装到python2里面,python3安装opencv的方式较复杂,需要安装的包见
上面通过系统自带环境apt-get安装。

python imutils 这个包,在resize操作的时候,会触发Illegal instruction
这里是这个包的问题,详情见参考1.解决办法自己重写imutils.resize方法。

def resize(image, width=None, height=None):
    dim = None
    (h, w) = image.shape[:2]
    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))
    resized = cv2.resize(image, dim, cv2.INTER_AREA)
    return resized

参考:

https://github.com/jrosebr1/imutils/issues/76
https://blog.csdn.net/qq_30163461/article/details/80396258
https://stackoverflow.com/questions/20518632/importerror-numpy-core-multiarray-failed-to-import
https://blog.csdn.net/T_infinity/article/details/79885302
https://blog.csdn.net/leaves_joe/article/details/67656340

猜你喜欢

转载自blog.csdn.net/funnyPython/article/details/83144959
今日推荐