-2

I am trying to decode data matrix qr codes using pylibdmtx. I have below image

enter image description here

and using the below code:

import cv2
from pylibdmtx import pylibdmtx
import time
import os


image = cv2.imread('file1.png', cv2.IMREAD_UNCHANGED);
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
t1 = time.time()
msg = pylibdmtx.decode(thresh)
print(msg)
if msg:
    print(msg[0].data.decode('utf-8'))
t2 = time.time()
print(t2-t1)

It prints nothing. Is there anything I am missing in the code? I tried some data matrix decoder online, and they were able to decode it so I am sure the image is correct.

3
  • 1
    If you add print("hello") before the first import, does it still print nothing? Commented Jun 30, 2024 at 19:47
  • @mkrieger1 Sorry I didnt get you. Why do you want me to print hello ? Commented Jun 30, 2024 at 20:07
  • 1
    You said that it prints nothing, although print is called later in your script. I wanted to find out what is the last place in the script from which calling print prints something. Commented Jul 1, 2024 at 0:28

1 Answer 1

0

Your approach looks mostly correct, but a few tweaks are needed to ensure that the decoding process works correctly with pylibdmtx:

  1. Make sure the image file 'file1.png' exists in the same directory as your script or provide the correct path to it.
  2. Make sure the image data passed to the decode function is in the correct format.
  3. Sometimes, additional preprocessing (like thresholding) can affect the detection. Try decoding the image directly.

Here is a modified version of your code with some adjustments:

import cv2
from pylibdmtx import pylibdmtx
import time

image = cv2.imread('file1.png', cv2.IMREAD_UNCHANGED)

# Check if the image is read correctly from file system
if image is None:
    raise ValueError("Image not found or unable to read")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

t1 = time.time()
decoded_objects = pylibdmtx.decode(gray)
t2 = time.time()

print("Decoding time:", t2-t1)
if decoded_objects:
    for obj in decoded_objects:
        print(obj.data.decode('utf-8'))
else:
    print("No Data Matrix QR codes found")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.