python-3.x - 如何在 python 中使用 opencv 读取数据矩阵代码?

这里是新的 python 用户。我正在使用带有 picamera 的树莓派来解码 Datamatrix 代码。我正在尝试回收我所做的旧项目的某些部分,该项目解码 QR 码并且效果很好但不支持数据矩阵(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] starting video stream... 并且什么都不做。没有错误。理想情况下,我还可以将这些图像转换为黑白图像,使解码更容易一些,但还没有让它工作:


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()

最佳答案

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

https://stackoverflow.com/questions/56807522/

相关文章:

node.js - GCP 应用引擎 : Random pending requests for s

rust-cargo - 如何修复 cargo 的 "failed to parse manifes

c++ - 使用 Microsoft Visual Studio 运行 C++ "hello wor

html - 仅适用于移动设备的引导轮播

pyspark - 如何将 FPGrowth 项集限制为 2 或 3

angular - Uncaught TypeError : ace. acequire 不是函数

swagger - 如何在开放式 API 规范中为字符串类型的属性指定空字符串值作为默认值

c# - 如何在 Flutter 中使用外部 DLL(Dot Net)?

c# - 在不更改 url 的情况下显示自定义页面

python - 如何在 python 中为 for 循环手动输入?