上一篇介绍了使用opencv和boofcv再嵌入式平台上的识别效果。这一篇继续使用上面的方法,依然使用python编写代码测试zbar和zxing的效果。

1、zbar测试

首先按照pyzbar的教程安装完zbar。测试代码依然延续前面的。使用opencv读取视频流,使用zbar解码图片。操作比较简单。zbar除了qrcode还可以识别其它类型的条形码和其它类型二维码。我这里只测试qrcode的。代码如下:

import numpy as np
from  pyzbar.pyzbar import decode
from  pyzbar.pyzbar import ZBarSymbol
import cv2
import os
import time


video_path = '~/Downloads/'

# VideoCapture
cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
cap.set(3, 1280)
cap.set(4, 720)
cap.set(5, 30)

# VideoWriter
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
outVideo = cv2.VideoWriter()
outVideo.open('output.mp4',fourcc,10.0,(1280,720), True )

# QRCodeDetector
findQR = False
qrResult = ''



print('Demo will work')
cnt = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        #frame = cv2.flip(frame,-1)
        outVideo.write(frame)
        
        # QR Code Detector
        if not findQR :
            frame_mono = cv2.cvtColor(np.uint8(frame), cv2.COLOR_BGR2GRAY)
            start = time.time_ns()
            barcodes = decode(frame_mono, symbols=[ZBarSymbol.QRCODE])
            if len(barcodes)>0:
                end = time.time_ns()
                print("running time is ", str(end - start))
                print("find QR Code " )
                findQR = True
                qrResult = barcodes[0].data.decode('utf-8')

    else:
        print('fail to open camera')
        break
    cnt += 1
    if cnt > 100 :
        print('Have created output.mp4')
        break

if findQR:
    print("QR Code  is: {}".format(qrResult))
else:
    print("Didn't find QR Code!")

cap.release()
outVideo.release()
cv2.destroyAllWindows()


    

单独用手机展示第一篇中的两张二维码测试,效果如下:

可以看出zbar的识别速度只有130ms左右,再现在这款嵌入式平台上碾压pyboof和opencv。识别效果也很不错。在测试过程中没有发现错误识别和识别不到的情况。(我的测试比较常规,没有进行遮挡。但手机测试应该一定的扭曲、颠倒。)

2、zxing

这里使用比较容易安装的python版本的zxing,可能性能会有所损失

https://github.com/dlenski/python-zxing 确保java安装成功之后,直接使用pip3 install zxing

import numpy as np
import zxing
import cv2
import os
import time


video_path = '~/Downloads/'

# VideoCapture
cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
cap.set(3, 1280)
cap.set(4, 720)
cap.set(5, 30)

# VideoWriter
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
outVideo = cv2.VideoWriter()
outVideo.open('output.mp4',fourcc,10.0,(1280,720), True )

# QRCodeDetector
findQR = False
qrResult = ''
reader = zxing.BarCodeReader()


print('Demo will work')
cnt = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        #frame = cv2.flip(frame,-1)
        outVideo.write(frame)
        
        # QR Code Detector
        if not findQR :
            frame_mono = cv2.cvtColor(np.uint8(frame), cv2.COLOR_BGR2GRAY)
            cv2.imwrite("temp.bmp", frame_mono)
            start = time.time_ns()
            barcode = reader.decode("temp.bmp", possible_formats = 'QR_CODE')
            if barcode is not None:
                end = time.time_ns()
                print("running time is ", str(end - start))
                print("find QR Code " )
                findQR = True
                qrResult = barcode.parsed

    else:
        print('fail to open camera')
        break
    cnt += 1
    if cnt > 100 :
        print('Have created output.mp4')
        break

if findQR:
    print("QR Code  is: {}".format(qrResult))
else:
    print("Didn't find QR Code!")

cap.release()
outVideo.release()
cv2.destroyAllWindows()


    

这个版本的zxing非常不好用,只能读取图片。所以视频流需要先存为照片然后再读入内存。这应该会耗费很多时间。

测试结果也非常令人遗憾:

是已经测试的几个中最差的,用时已经几乎是1.5秒左右了。而且识别效果似乎也不太好。

 

3、quirc

最初觉得比较幸运的是quirc也有python版本:https://github.com/svartalf/python-quirc。但灯测试时发现这个项目年久失修,无论python3还是python都不可用。考虑到opencv实际上是借助quirc来实现二维码识别的。暂时认为效果差不多。

既然开始了这个项目,不妨用c语言继续测试quirc。

 

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐