Search code examples
pythonopencvbarcode

Pyzbar recognizes EAN-13 barcode as PDF417?


I'm trying to build a realtime barcode reader using pyzbar. I have only EAN-13 barcodes and some of them are read properly but some are recognized as PDF417 and I get this message: WARNING: .\zbar\decoder\pdf417.c:89: : Assertion "g[0] >= 0 && g[1] >= 0 && g[2] >= 0" failed How to increase efficiency? Maybe there is another python library which I can use?

This is my code:

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

def decoder(image):
    imgGray = cv2.cvtColor(image,0)
    barcodes = decode(imgGray)

    for barcode in barcodes:
        (x, y, w, h) = barcode.rect

        cv2.rectangle(imgGray, (x-10, y-10),
                          (x + w+10, y + h+10), 
                          (255, 0, 0), 2)
              
        if barcode.data!="":
                
            print("Barcode: ", barcode.data)
            print(barcode.type)

cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    decoder(frame)
    cv2.imshow('Image', frame)
    code = cv2.waitKey(10)
    if code == ord('q'):
        break

Solution

  • If you only have EAN-13, you could specify that so pyzbar would only check for EAN-13. You can specify the desired codes in a list to the parameter symbols when calling decode(...).

    from pyzbar.pyzbar import decode, ZBarSymbol
    import cv2
    
    image = cv2.imread("testImage.png")
    decode(image, symbols=[ZBarSymbol.ZBAR_EAN13])
    

    (Code written from memory, sorry if eg the symbol isn't exactly right)

    Another library to test would be zxing. In my tests zxing was much slower than zbar but got better results.