3

new python user here. I'm working on a project with a raspberry pi with a picamera to decode Datamatrix codes. I'm trying to recycle some parts of an older project that I did which decodes QR codes and works great but doesn't support datamatrix (pyzbar), but there seems to be an issue that I can't figure out when I try using pylibdmtx.

This code works as intended. Shows a video screen, highlights found barcodes and captures the image.


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

This code prints [INFO] starting video stream... and does nothing else. No errors. Ideally, I could also convert these images to black and white to make decoding a little easier, but haven't gotten it to work at all yet:


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()
Flaxamax
  • 31
  • 1
  • 2

1 Answers1

0

The documentation of Pyzbar version 0.1.9 (2022) states 'Read one-dimensional barcodes and QR codes from Python 2 and 3 using the zbar library'. Therefore the only 2D-codes Pyzbar can handle are QR-codes, and not DataMatrix codes. See https://pypi.org/project/pyzbar/