Как я могу прочитать коды datamatrix, используя opencv в python? - PullRequest
0 голосов
/ 28 июня 2019

новый пользователь 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()
...