1、首先使用指令安装cv2和其拓展:
sudo pip3 install opencv-python -i http://pypi.douban.com/simple/
sudo pip3 install opencv-contrib-python -i http://pypi.douban.com/simple/
如果下载太慢可以将其下载网址复制后到浏览器下载后使用pip3安装
2、在终端中运行python3
导入模块cv2:import cv2
如果报错则根据报错信息安装下面相应的包:
Traceback (most recent call last):
File "running.py", line 5, in
import cv2
File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in
from .cv2 import *
ImportError: libhdf5_serial.so.103: cannot open shared object file: No such file or directory
sudo apt-get update
sudo apt-get install libhdf5-dev -y
sudo apt-get install libatlas-base-dev -y
sudo apt-get install libjasper-dev -y
sudo apt-get install libqt4-test -y
sudo apt-get install libqtgui4 -y
sudo apt-get install libhdf5-serial-dev -y
sudo apt install libqtgui4 -y
sudo apt install libqt4-test -y
sudo apt-get update
安装完一个尝试一次import cv2直到所有都安装完
具体可参考:
import cv2时ImportError: libjasper.so.1: cannot open shared object file: No such file or directory
树莓派4b安装opencv遇到的一些问题以及解决
所有都安装完后可能报如下错误:
>>> import cv2
Traceback (most recent call last):
File "", line 1, in
File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in
from .cv2 import *
ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8
这个错误在树莓派上安装Opencv遇到的小bug解决方法有说明,博主给出的解释是:
发现github上面有官方的issue,里面说这个是一个bug, 难怪很多人抱怨安装不来opencv呢。
上述GitHub的原解决方案在:
opencv: undefined symbol: __atomic_fetch_add_8
及在终端加上:LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1
pi@raspberrypi:~ $ LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1
3、最后如果还不行那么就是1默认安装的是最新版的opencv,和树莓派不兼容,可以尝试卸载当前版本,安装低版本,这里是3.4.6.27版:
下载方式:
pip3 install opencv-python==3.4.6.27 -i http://pypi.douban.com/simple/
pip3 install opencv-contrib-python==3.4.6.27 -i http://pypi.douban.com/simple/
最后完美解决!
pi@raspberrypi:~/MyProject/FaceRecognize $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>