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

时间:2019-06-28 13:06:44

标签: python-3.x opencv datamatrix

新的python用户在这里。我正在使用带有树莓派和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()

0 个答案:

没有答案