R329-opencv4_contrib——wechat_qrcode
微信的CV团队开源了微信的二维码扫码引擎,并且已经加入到opencv4_contrib模块,今天我们就来试试它。
安装opencv_contrib
前边的教程我们只是安装了opencv的python版本(其实不用安装也可以使用,因为R329的镜像里已经内置了编译好的opencv库的C++版本和python版本,但是版本好像是opencv3),但是今天我们的实验只有opencv是不行的,我们需要先卸载它,然后安装opencv_contrib。
先卸载已经安装的opencv,没有安装的就不用卸载,查看自己是否安装可以用“pip3 list”来查看查看是否有opencv-python
pip3 uninstall opencv-python
然后下载whl包
下载后传到R329上,具体方法见以前的博客,然后cd到whl文件所在的文件夹并执行以下命令即可安装
pip3 install opencv_contrib_python-4.5.5.62-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
模型下载:
具体实现代码
首先新建文件夹,命名成qrcode,再新建一个qrcode.py文件,内容为
#-*- coding: utf-8 -*-
import time
import cv2
cap = cv2.VideoCapture(0)
#设置图像的高
cap.set(3,240)
#设置图像的宽,这里虽然设置的是240但是实际上是320,
cap.set(4,240)
#写入指定设备
f = open('/dev/fb0','wb')
try:
detector = cv2.wechat_qrcode_WeChatQRCode(
"detect.prototxt", "detect.caffemodel", "sr.prototxt", "sr.caffemodel")
except:
print("---------------------------------------------------------------")
print("Failed to initialize WeChatQRCode.")
print("Please, download 'detector.*' and 'sr.*' from")
print("https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode")
print("and put them into the current directory.")
print("---------------------------------------------------------------")
exit(0)
prevstr = ""
while True:
st = time.time()
ret,img = cap.read()
if ret:
#截取240*240的图像,lcd最大能显示的是240*240个16位像素
img = img[:,0:240]
#将灰度图顺时针旋转180度
img = cv2.rotate(img,cv2.ROTATE_180)
res, points = detector.detectAndDecode(img)
for t in res:
if t != prevstr:
print(t)
#转换为16位色彩,(因为lcd是16位显示)
img = cv2.cvtColor(img,cv2.COLOR_BGR2BGR565)
cv2.putText(img, "{0}" .format(str(1 / (time.time() - st))), (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 1)
#写入图像的二进制数据
f.seek(0)
f.write(bytearray(img))
将下载好的这四个模型文件上传到qrcode文件夹里
然后cd到qrcode文件夹,执行
python3 qrcode.py
效果如图所示,无二维码时大概7-8帧,识别到二维码时大概2-3帧,感觉还可以,比K210的那种纯代码识别要好很多。群友说的把caffemodel转化成NCNN模型效率会更高一些,并且已经有人实现,而且在github上开源出来,但是它是用C++写的,我对C++只能说会hello world,我琢磨琢磨,把模型转化一下,然后代码也用C++重构一下,这个效率应该会提高不少,但是不知道要等到什么时候。
回复777催更