Detect (and maybe decode) PDF417 barcodes using python

1.2k Views Asked by At

I am trying to detect the pdf417 barcode (2D barcode) from an image using python.

I will be receiving images of IDs where there is a barcode in them but it might not always be straight. So I am looking for an effective way to DETECT the pdf417 barcode using Python.

I tried all of the available methods that I could find (that uses python) e.g.,

  • pdf417decoder: requires the image to be cut exactly around the barcode just like in the image below: barcode format required for pdf417decoder

  • pyzbar: only detects 1D barcodes

  • python-zxing and zxing: didn't detect any of the pdf417 barcodes that I tried (around 10 different IDs - different country)

  • Barcode-detection: this is a DL approach that uses YOLO-V3 to detect barcodes, but again (after trying it), it only detects 1D barcodes...

Is there a method that I missed? Am I using a wrong approach towards this problem?

  • Possible solution that I am thinking of: using computer vision (some filters and transformations) to detect a box that has black and white dots... Something similar to this.

Thanks!

2

There are 2 best solutions below

5
On BEST ANSWER

After various trials, I ended up using an approach of template matching by OpenCV. You need to precisely choose your template image that will be the search reference of your algorithm. You need to feed it some grayscaled images. Then, you need to choose the boxes that have a result higher than a certain threshold (for me 0.55). Then apply NMS (non max suppression) to filter out the noisy boxes.

But keep in mind that there are many edge cases to encounter. If someone is interested to see the complete solution, please let me know.

enter image description here

0
On
import cv2
import zxingcpp
c=cv2.VideoCapture(0)
while 1:
    f=c.read()[1]
    s=zxingcpp.read_barcodes(f)
    for r in s:
        print(r.text)
    cv2.imshow('',f)
    if cv2.waitKey(1) & 0xFF==ord('q'):
        break
c.release()
cv2.destroyAllWindows()

You need to pip install zxing-cpp and then run the above code to scan any type of barcode you need from your webcam. If you want to input static image you can refer to https://pypi.org/project/zxing-cpp/