如何用Python制作条形码阅读器?代码实现示例

2021年11月16日19:17:39 1 1,819 次浏览

如何用Python制作条形码阅读器?本文带你了解如何使用 Python 中的 pyzbar 和 OpenCV 库制作可解码条形码并将其绘制在图像中的条形码扫描仪。

条形码是在视觉和机器可读的形式表示数据的方法,它包括的条和间隔。今天,我们随处可见条形码,尤其是在超市的产品中。

Python如何制作条形码阅读器?条形码可以由光学条形码扫描仪读取,但在本教程中,我们将使用 Python 编写一个脚本,该脚本能够 读取和解码条形码,以及它们在给定图像中所在位置的绘图。

相关: 如何在 Python 中从视频中提取帧。

Python条形码阅读器示例介绍和解析 - 首先,我们需要安装几个库:

pip3 install pyzbar opencv-python

安装这些之后,打开一个新的 Python 文件并导入它们:

from pyzbar import pyzbar
import cv2

我几乎没有要测试的图像,你可以使用来自 Internet 或你自己的磁盘的任何图像,但是你可以在此目录中获取我的测试图像。

如何用Python制作条形码阅读器?我已经将每个功能都包装成一个函数,我们要讨论的第一个函数如下:

def decode(image):
    # decodes all barcodes from an image
    decoded_objects = pyzbar.decode(image)
    for obj in decoded_objects:
        # draw the barcode
        print("detected barcode:", obj)
        image = draw_barcode(obj, image)
        # print barcode type & data
        print("Type:", obj.type)
        print("Data:", obj.data)
        print()

    return image

decode()函数将图像作为 numpy 数组,并使用pyzbar.decode()它负责解码来自单个图像的所有条码,并返回有关检测到的每个条码的一组有用信息。

然后我们遍历所有检测到的条形码并在条形码周围绘制一个矩形并打印条形码的类型和数据。

为了清楚起见,以下是obj我们打印出来的样子:

Decoded(data=b'43770929851162', type='I25', rect=Rect(left=62, top=0, width=694, height=180), polygon=[Point(x=62, y=1), Point(x=62, y=179), Point(x=756, y=180), Point(x=756, y=0)])

Sopyzbar.decode()函数以矩形和多边形的形式返回包含条码、条码类型以及位置点的数据。

Python条形码阅读器示例:这将我们带到了我们使用的下一个函数draw_barcode()

def draw_barcode(decoded, image):
    # n_points = len(decoded.polygon)
    # for i in range(n_points):
    #     image = cv2.line(image, decoded.polygon[i], decoded.polygon[(i+1) % n_points], color=(0, 255, 0), thickness=5)
    # uncomment above and comment below if you want to draw a polygon and not a rectangle
    image = cv2.rectangle(image, (decoded.rect.left, decoded.rect.top), 
                            (decoded.rect.left + decoded.rect.width, decoded.rect.top + decoded.rect.height),
                            color=(0, 255, 0),
                            thickness=5)
    return image

Python如何制作条形码阅读器?该函数采用我们刚刚看到的解码对象和图像本身,它使用cv2.rectangle()函数在条码周围绘制一个矩形,或者你可以取消注释其他版本的函数;使用cv2.line()函数绘制多边形,选择权在你手中。我更喜欢矩形版本。

最后,它返回包含绘制条码的图像。现在让我们将这些函数用于我们的示例图像:

if __name__ == "__main__":
    from glob import glob

    barcodes = glob("barcode*.png")
    for barcode_file in barcodes:
        # load the image to opencv
        img = cv2.imread(barcode_file)
        # decode detected barcodes & get the image
        # that is drawn
        img = decode(img)
        # show the image
        cv2.imshow("img", img)
        cv2.waitKey(0)

如何用Python制作条形码阅读器?在我当前的目录中,我有barcode1.png、barcode2.png和barcode3.png,它们都是扫描条码的示例图像,我使用了glob以便我可以将所有这些图像作为列表获取并遍历它们。

在每个文件上,我们使用cv2.imread()函数加载它,并使用前面讨论的decode()函数解码条形码,然后显示实际图像。

请注意,这也将检测 QR 码,这很好,但为了获得更准确的结果,我建议你查看专用教程以在 Python 中检测和生成 qr 码。

当我运行脚本时,它会显示每个图像并打印其类型和数据,按任意键,你将获得下一个图像,这是Python条形码阅读器示例的输出:

detected barcode: Decoded(data=b'0036000291452', type='EAN13', rect=Rect(left=124, top=58, width=965, height=812), polygon=[Point(x=124, y=59), Point(x=124, y=869), Point(x=621, y=870), Point(x=1089, y=870), Point(x=1089, y=58)])
Type: EAN13
Data: b'0036000291452'

detected barcode: Decoded(data=b'Wikipedia', type='CODE128', rect=Rect(left=593, top=4, width=0, height=294), polygon=[Point(x=593, y=4), Point(x=593, y=298)])
Type: CODE128
Data: b'Wikipedia'

detected barcode: Decoded(data=b'43770929851162', type='I25', rect=Rect(left=62, top=0, width=694, height=180), polygon=[Point(x=62, y=1), Point(x=62, y=179), Point(x=756, y=180), Point(x=756, y=0)])
Type: I25
Data: b'43770929851162'

这是显示的最后一张图片:

如何用Python制作条形码阅读器?代码实现示例

结论

Python如何制作条形码阅读器?现在你有了一个很棒的工具来用 Python 制作你自己的条形码扫描仪。我知道你们都想直接从相机读取,因此,我准备了从相机读取并以实时方式检测条形码的代码,请在此处查看!

你还可以在检测到每个条形码时添加某种蜂鸣声,就像在超市中一样,查看播放声音的教程,这可能有助于你实现这一目标。

有关更多详细信息,我邀请你查看pyzbar 文档

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  1   博主  0

    • avatar 王晗 0

      我全部都在报错,太难受了