测试 二维码解码软件包的功能

二维码识别软件

01 QR解码


一、前言

  二维码在很多方面都有很多的应用, 在Python中也有很多软件包可以用来对图像中的二维码进行解码。 针对不同的场景下,不同尺寸和分辨率的二维码图像, 测试二维码识别的准确性。 测试软件包括有  pyzbar, 以及 CV2 中的 QR CodeDetector 两个软件包。
GM1683333893_1280_800.MPG|_-6

▲ 图1.1.1 待测试的二维码图片

▲ 图1.1.1 待测试的二维码图片

二、测试结果

  这是利用 pyzbar 检测的结果。 程序中, 调用了 pyzbar 的 decode 函数, 获得解码结果。 这是返回的解码结果。 其中包括二维码的内容, 二维码的位置, 二维码的质量, 二维码的朝向等信息。 可以看到解码适应度还是非常强的。 对于一些图像质量不好的二维码无法识别。 。
GM1683336018_1280_800.MPG|_-11

from headm import *
from PIL import Image
from pyzbar import pyzbar
import qrcode

imgid = [676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687]
tspsetpen(color=0xff, width=3)

tsprv()

for id in imgid:
    outfile = tspgetdopfile(id)

    ret = pyzbar.decode(Image.open(outfile))
    outfile = tspgetdopfile(id)
    box = tspgetbox(id)

    if len(ret) > 0:
        tspbox(0, *box)
[Decoded(data=b'https://qrco.de/bbYfSx', type='QRCODE', rect=Rect(left=58, top=55, width=90, height=90), polygon=[Point(x=58, y=55), Point(x=58, y=144), Point(x=148, y=145), Point(x=148, y=55)], quality=1, orientation='UP')]

▲ 图1.2.1 识别的结果

▲ 图1.2.1 识别的结果

  下面改动代码, 绘制出二维码在图片中的位置。 可以看到识别出的二维码的位置是以矩形标记出来的, 如果二维码是倾斜的, 标记的方框是二维码外接矩形。 如果二维码是倾斜的, 标记二维码边框为长方形。 如果没有识别出二维码, 则不返回任何信息。
GM1683336958_1280_800.MPG|_-5

from headm import *
from PIL import Image
from pyzbar import pyzbar
import qrcode

imgid = [676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687]
tspsetpen(color=0xff, width=3)

tsprv()

for id in imgid:
    outfile = tspgetdopfile(id)

    img = Image.open(outfile)

    size = img.size
    printf(size)

    ret = pyzbar.decode(img)
    outfile = tspgetdopfile(id)
    box = tspgetbox(id)
    boxwidth = box[2] - box[0]
    boxheight = box[3] - box[1]

    if len(ret) > 0:
        left = ret[0][2][0]
        top = ret[0][2][1]
        width = ret[0][2][2]
        height = ret[0][2][3]

        printff(left, top, width, height)

        bleft = left * boxwidth // size[0] + box[0]
        btop = top * boxheight // size[1] + box[1]

        bright = (left + width) * boxwidth // size[0] + box[0]
        bbottom = (top + height) * boxheight // size[1] + box[1]

        tspbox(0, bleft, btop, bright, bbottom)

  下面测试 CV2 中的 QRCodeDetector 函数的性能。 这是测试的结果。很显然, 这个函数适应性比起 pyzbar 的性能差了很多。 只有四个二维码被识别出来。 对于倾斜的二维码, 识别结果中的位置似乎也出现了偏差。 这张图中的二维码非常清晰, 但并没有被识别出来。 因此, CV2 中的二维码识别效果并不是很理想。
GM1683337983_1280_800.MPG|_-4

from headm import *
from PIL import Image
from pyzbar import pyzbar
import qrcode
import cv2

imgid = [676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687]
tspsetpen(color=0xff, width=3)

tsprv()
d = cv2.QRCodeDetector()

for id in imgid:
    outfile = tspgetdopfile(id)

    img = Image.open(outfile)
    size = img.size

    val,a,b = d.detectAndDecode(cv2.imread(outfile))

    box = tspgetbox(id)
    boxwidth = box[2] - box[0]
    boxheight = box[3] - box[1]

    if len(val) > 0:

        tspbox(0, *box)
        printf(a)

        left = a[0][0][0]
        top = a[1][0][1]
        right = a[2][0][0]
        bottom = a[2][0][1]
        width = right-left
        height = bottom-top

        printff(left, top, width, height)

        bleft = int(left * boxwidth // size[0] + box[0])
        btop = int(top * boxheight // size[1] + box[1])

        bright = int((left + width) * boxwidth // size[0] + box[0])
        bbottom = int((top + height) * boxheight // size[1] + box[1])

        tspbox(0, bleft, btop, bright, bbottom)

  下面再测试一些 样板, 在CV2 的识别结果中,只有四个被正确识别。   pyzbar 识别的结果中, 有八个被正确的识别。 这再一次验证了 pyzbar 识别的适应性。

GM1683338722_1280_800.MPG|_-2

  这是在不同角度下的识别结果。 Pyzbar 对于所有角度下的图片都能够正确的识别。 CV2的识别结果中, 有一个未被识别。

▲ 图1.2.2 不同角度下的识别结果上:Pyzbar识别结果,下:CV2识别结果

▲ 图1.2.2 不同角度下的识别结果
上:Pyzbar识别结果,下:CV2识别结果

  下面测试不同的横向压缩的情况下识别结果, 使用 CV2 的检测结果,只能识别前面两个二维码, 使用 Pyzbar 则可以识别横向压缩 50% 的图像中的二维码。
GM1683340122_1280_800.MPG|_-2

▲ 图1.2.3 不同比左右压缩比识别结果上:CV2识别结果,下:Pyzbar识别结果

▲ 图1.2.3 不同比左右压缩比识别结果
上:CV2识别结果,下:Pyzbar识别结果

  测试对原图进行不同比例的压缩对识别影响。 CV2 只能识别原尺寸的图像。  Pyzbar 则可以识别压缩到 50% 的结果。 不过有一个比较奇怪的现象, 其中有一个比较大的图片中的二维码居然没有识别出来。
GM1683340632_1280_800.MPG|_-2

▲ 图1.2.4 不同压缩比识别结果上:CV2识别结果,下:Pyzbar识别结果

▲ 图1.2.4 不同压缩比识别结果
上:CV2识别结果,下:Pyzbar识别结果

  结 ※


  文对于两款二维码识别软件进行测试, 包括有不同场景下的二维, 不同的压缩以及旋转对识别的结果。  实验结果展示 Pyzbar 具有较强的识别性能。
GM1683340740_1280_800.MPG|_-3


● 相关图表链接:

猜你喜欢

转载自blog.csdn.net/zhuoqingjoking97298/article/details/130519005