0

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!

4
  • if you have a library that can decode the pattern once located, you just need to locate the barcode. that is not a machine learning problem though. it appears to require perfect alignment, seems to have been designed for scanned documents, not to be captured by a camera. Commented Dec 5, 2022 at 15:37
  • Thank you for your response @ChristophRackwitz. The library that is capable of decoding the pattern once detected is pdf417decoder. But detecting the pattern itself might be addressed via several distinct methods. Deep Learning could be one of them (e.g., github.com/dchakour/Barcode-detection). Do you have a proposition about the detection method that I can implement to properly detect the barcode area? Knowing that it might be in different locations for the different identification documents. Commented Dec 5, 2022 at 16:27
  • 1
    take inspiration from the recommended detection logic for QR code finder patterns: scanlines across the image, looking for specific timing sequences. that's how you find the start and stop patterns. once you have those, you locally find these patterns' edges, the patterns' extents along the edge, and by going along the normal to those edges of one pattern, you will encounter the other pattern. Commented Dec 5, 2022 at 22:51
  • Have you tried dbr? pip install dbr Commented Jan 11, 2023 at 5:32

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

It's very interesting to see your solution. Where can you see it?
@Ammar be aware that the shown barcode reveals patient information once decoded (name, birthdate, ...). If this is not a dummy, you might want to remove/obfuscate it.
@mozway it is certainly dummy data.
@AmmarMohanna are you sure? I wouldn't be sure as it looks like a real photo. Better double check with whoever provided it as it could be pretty bad if this is real data
@AmmarMohanna I'm very interested to see your solution. Could you please let me know how can I see?
1
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/

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.