如何在python中使用opencv读取datamatrix代码?

kokeuurv  于 2022-11-15  发布在  Python
关注(0)|答案(1)|浏览(415)

我是一个新的python用户。我正在做一个项目,用一个raspberry pi和一个picamera来解码Datamatrix代码。我正在尝试回收我以前做过的一个旧项目的一些部分,这个项目解码QR代码,工作很好,但是不支持datamatrix(pyzbar),但是当我尝试使用pylibdmtx时,似乎有一个问题我无法解决。
此代码按预期工作。显示一个视频屏幕,突出显示找到的条形码并捕获图像。

from pyzbar import pyzbar
import time
import cv2
import os
import numpy as np

def nothing(x):
    pass

print("[INFO] starting video stream...")
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FPS, 22)
build = 1
count = True
i=0

time.sleep(2.0)

found = set()
found.clear()


while cv2.waitKey(1) !=27 and cap.isOpened():
    _,frame = cap.read()

    barcodes = pyzbar.decode(frame)

    for barcode in barcodes:

        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)

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

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

        if barcodeData not in found:
            build = build+1
            print('saving')
            cv2.imwrite(os.path.join(str(text) + 'test.jpeg'),frame)
            found.add(barcodeData)

    cv2.imshow("Barcode Scanner", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

print("[INFO] cleaning up...")
cap.release()
cv2.destroyAllWindows()

这段代码打印[INFO]开始视频流...并且不做其他任何事情。没有错误。理想情况下,我也可以将这些图像转换为白色,使解码更容易一点,但还没有让它工作:

from pylibdmtx import pylibdmtx
import time
import cv2
import os
import numpy as np

def nothing(x):
    pass

print("[INFO] starting video stream...")
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FPS, 22)
cap = cv2.cvtcolor(cap, cv2.COLOR_BGR2GRAY)
build = 1
count = True
i=0

time.sleep(2.0)

found = set()
found.clear()


while cv2.waitKey(1) !=27 and cap.isOpened():
    _,frame = cap.read()

    barcodes = decode(frame)

    for barcode in barcodes:

        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)

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

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

        if barcodeData not in found:
            build = build+1
            print('saving')
            cv2.imwrite(os.path.join(str(text) + 'test.png'),frame)
            found.add(barcodeData)

    cv2.imshow("Barcode Scanner", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

print("[INFO] cleaning up...")
cap.release()
cv2.destroyAllWindows()
wj8zmpe1

wj8zmpe11#

Pyzbar版本0.1.9(2022)的文档声明“使用zbar库从Python 2和3读取一维条形码和QR码”。因此,Pyzbar只能处理QR码,而不是DataMatrix码。请参见https://pypi.org/project/pyzbar/

相关问题