arm开发板上移植opencv+pyzbar+numpy,用python3实现二维码识别

时间:2022-12-07 06:33:57

(其中opencv 4.0.1以及3.4.5 已经集成了二维码识别,cv.QRCodeDetector().detect(frame),可以不用pyzbar)

一.pyzbar安装
sudo apt-get update
sudo apt-get install python3-pip python3-dev
sudo pip3 install --upgrade pip
sudo apt-get install libzbar-dev
sudo pip3 install pyzbar
验证:
运行python3
执行:from pyzbar.pyzbar import decode
没报错,就ok

二.opencv安装
1.下载源码
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
2.安装构建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 -y
3.安装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 install
sudo ldconfig
7.验证
运行python3,输入 import cv2,没有报错就ok了

三.测试代码

from pyzbar.pyzbar import decode
import cv2
import os
image = cv2.VideoCapture(0)
x = True
while x:
    ret, frame = image.read()
    a = decode(frame)
    if len(a):
        print(a[0].data.decode('utf-8'))