1

I have been trying to detect barcodes, QR codes and Datamatrix from a label. The pyzbar library detects barcodes and QR codes but fails to detect datamatrix. Then I tried pylibdmtx library which decodes only one datamatrix from an image (which has 6 datamatrix codes). It is also very slow! :(

Is there any other way to detect multiple datamatrix from an image?

Here is my code for datamatrix :

from pylibdmtx.pylibdmtx import decode
import cv2
import numpy as np


def dataMat(image, bgr):
    gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    data = decode(gray_img)
    print(data)
    for decodedObject in data:
        points = decodedObject.rect

        pts = np.array(points, np.int32)
        pts = pts.reshape((-1, 1, 2))
        cv2.polylines(image, [pts], True, (0, 255, 0), 3)

        cv2.putText(frame, decodedObject.data.decode("utf-8") , (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1,
                    bgr, 2)

        print("Barcode: {} ".format(decodedObject.data.decode("utf-8")))

bgr = (8, 70, 208)

frame  = cv2.imread('datamat.png')
code = dataMat(frame, bgr)
print(code)
cv2.imshow('Data Matrix reader', frame)
code = cv2.waitKey(0)
shradha
  • 11
  • 1
  • 4
  • Have you tried supplying a good high res image? – ZdaR Dec 04 '19 at 10:51
  • @ZdaR Yes, I did! I tried with many samples, but it is detecting only one datamatrix in each sample – shradha Dec 04 '19 at 12:46
  • In the documentation examples it is detecting multiple objects, Can you try with the test image provided with the repo? `pylibdmtx/tests/datamatrix.png` – ZdaR Dec 04 '19 at 12:56
  • Yes, I tried with the sample image and it works. But I am trying with real images, and it's not working the same there! My codes are denser than the sample image.@ZdaR – shradha Dec 05 '19 at 05:24
  • Then maybe you need to pre-process the input images to make them more crisp. Try Binary thresholding the image and then passing it over to the library. – ZdaR Dec 05 '19 at 11:22
  • Give a try to Aspose.BarCode Cloud SDK for Python, it can read multiple barcodes from an image. https://products.aspose.cloud/barcode/python – Tilal Ahmad Sep 28 '20 at 05:58
  • There is also a blog post here explaining how some pretreatments can be applied to improve further code detection https://kdmurray.id.au/post/2022-03-21_decode-qrcodes/ – oo1on Sep 16 '22 at 14:18

0 Answers0