首页 > 解决方案 > 使用 Zbar Python 检测多个条形码的问题

问题描述

我正在尝试将 pyzbar 用于自动条形码检查项目。目标是从具有多个条形码的艺术品表中检测每个条形码的信息和类型。

下面的代码适用于一个条形码或几个不同的条形码。但就我而言,总共有 20 个 4 个不同的条形码。结果只返回 4 个不同的代码和 4 个坐标。看起来 zbar 跳过了所有重复的条形码。标记框看起来也很奇怪。任何想法为什么?

2408 3468 2966 107

0690461219945EAN13

278 3468 2287 107

0690461219952EAN13

278 1529 2434 107

0990461219816EAN13

435 1529 4939 1939

0690461219990EAN13

from pyzbar import pyzbar
import cv2

image = cv2.imread("test-1.png")
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
t,bimage = cv2.threshold(gray,160,255,cv2.THRESH_BINARY)


barcodes = pyzbar.decode(bimage)


for barcode in barcodes:
    (x, y, w, h) = barcode.rect

    print(x,y,w,h)

    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)

    barcodeData = barcode.data.decode("utf-8")
    barcodeType = barcode.type
    print (barcodeData+barcodeType)

    text = "{} ({})".format(barcodeData, barcodeType)
    cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

cv2.imwrite("test1_re.png", image)
cv2.waitKey(0) 

测试样品

测试结果

标签: pythoncomputer-visionbarcode-scannerzbar

解决方案


推荐阅读