(其中opencv 4.0.1以及3.4.5 已经集成了二维码识别,cv.QRCodeDetector().detect(frame),可以不用pyzbar)
一.pyzbar安装
sudo apt-get updatesudo apt-get install python3-pip python3-devsudo pip3 install --upgrade pipsudo apt-get install libzbar-devsudo pip3 install pyzbar验证:运行python3执行:from pyzbar.pyzbar import decode没报错,就ok二.opencv安装
1.下载源码git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git2.安装构建OpenCV的相关工具:sudo apt-get install build-essential -y sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y3.安装numpy (尝试下sudo pip3 install numpy 如果报错按照下面执行) 1).源码下载wget https://files.pythonhosted.org/packages/2d/80/1809de155bad674b494248bcfca0e49eb4c5d8bee58f26fe7a0dd45029e2/numpy-1.15.4.zip 2).解压 unzip numpy-1.15.4.zip 3).编译 cd numpy-1.15.4 sudo python3 setup.py install (如果你arm处理器差,可能编译到一半,就卡死了。可以用 <sudo python3 setup.py build -j 4 install> 数字4表示开启4个线程,如全志h3 -j4都卡住,就-j 1或者2 都可以,就编译的慢点罢了 )4.在opencv中构建环境cd opencv
mkdir release/ cd release/ cmake -D BUILD_opencv_python3=YES -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_LIBV4L=ON -D OPENCV_EXTRA_MODULES=../../opencv_contrib/modules -D PYTHON3_LIBRARIES=/usr/lib/arm-linux-gnueabihf/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include/ ..
(对应的位置自己去找)5.编译sudo make (如果你arm处理器好的话,比如rk3399 ,sudo make -j8或16都可以;处理器差的话,就比如全志h5之类的,就直接sudo make -j4,或者不加默认是一个线程编译)6.安装并更新动态库sudo make installsudo ldconfig7.验证运行python3,输入 import cv2,没有报错就ok了
三.测试代码
from pyzbar.pyzbar import decodeimport cv2import osimage = cv2.VideoCapture(0)x = Truewhile x: ret, frame = image.read() a = decode(frame) if len(a): print(a[0].data.decode('utf-8'))