Python 条形码和二维码识别

个人用Django开发的博客已上线,欢迎访问:https://www.zhibibin.com

本文介绍一种利用pyzbar库结合opencv进行实时识别条形码和二维码的方法。

首先安装pyzbar 和opencv: pip install pyzbar opencv-python

(pyzbar的源码,其作者有上传到github中,具体可以参考:https://github.com/NaturalHistoryMuseum/pyzbar/)

pyzbar条用其decode()方法进行条形码和二维码的识别,传入参数可以时numpy.ndarry,PIL图像还有opencv的图像数据,本文这里使用的是opencv的图像数据。

pyzbar返回一个列表,包含条形码或二维码解码后的数据信息,图像尺寸信息,以及类型等,如下例子:

decode(Image.open('pyzbar/tests/qrcode.png'), symbols=[ZBarSymbol.QRCODE])
[
    Decoded(
        data=b'Thalassiodracon', type='QRCODE',
        rect=Rect(left=27, top=27, width=145, height=145),
        polygon=[
            Point(x=27, y=27), Point(x=27, y=172), Point(x=172, y=172),
            Point(x=172, y=27)
        ]
    )
]

 代码比较简单,直接上完整代码。有意思的是条形码只支持C类 CODE 128类型的条形码,不支持A类和B类的。

# Created by: Daniel G.
# Date: 2020-4-26
# detect QR code and barcode (only code 128 C type)

import csv
import datetime
from pyzbar import pyzbar
from PIL import Image
import cv2

csv = open("template.csv", "w")
found = set()
vs = cv2.VideoCapture(1)

while True:
    # grab the frame from the threaded video stream and resize it to

    ret,frame = vs.read()
    frame = cv2.resize(frame, (320, 240))

    # find the barcodes in the frame and decode each of the barcodes

    barcodes = pyzbar.decode(frame)
    # loop over the detected barcodes
    for barcode in barcodes:
        # extract the bounding box location of the barcode and draw
        # the bounding box surrounding the barcode on the image
        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)

        # the barcode data is a bytes object so if we want to draw it
        # on our output image we need to convert it to a string first
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        # draw the barcode data and barcode type on the image

        text = f"{barcodeData} ({barcodeType})"
        cv2.putText(frame, text, (x, y + h + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)

        # if the barcode text is currently not in our CSV file, write
        # the timestamp + barcode to disk and update the set
        if barcodeData not in found:
            print(text)
            csv.write(f"time:{datetime.datetime.now()}, barcode data: {barcodeData}\n")
            csv.flush()
            found.add(barcodeData)
        # show the output frame
    cv2.imshow("Barcode_QRcode Scanner", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

# close the output CSV file do a bit of cleanup
print("[INFO] cleaning up...")
# csv.close()
cv2.destroyAllWindows()
解码输出:
time:2020-04-26 20:58:26.069251, barcode data: Code 128
time:2020-04-26 20:58:27.069251,http://192.168.1.106:33703/send/w7un (QRCODE)

 

    

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值