0

I am using pyzbar to decode barcodes on Raspberry Pi 3 using Pi Camera v1 (resolution 1296x972). Qr codes are decoded very well. When decoding two dimensional barcodes (CODABAR), the success rate is very low.

I have tried saving one frame from the video stream and decode it with pyzbar on the Raspberry and it fails. When i try to decode the same image on Ubuntu, and decodes it successfully.

from pyzbar import pyzbar
from PIL import Image
img = Image.open('sampleImage.png')
d = pyzbar.decode(img)

print (d)

Any thoughts what may be the problem?

UPDATE:

The following image is my specific use case. eBarcode image Because I am using Pi Camera v1 to take images, I tried to do adjustment to image sharpness:

from picamera import PiCamera
self.camera = PiCamera()
self.camera.sharpness = 100

The following image is with sharpness 100. However, pyzbar still fails to decode it on the Raspberry Pi.

enter image description here

vg34
  • 91
  • 2
  • 10

1 Answers1

0

You need to remove the black border from your image. According to this answer, you can simply crop your image then feed the image to pyzbar.decode() function.

import cv2
from pyzbar import pyzbar
import numpy as np


def autocrop(image, threshold=0):
    """Crops any edges below or equal to threshold
    Crops blank image to 1x1.
    Returns cropped image.
    """
    if len(image.shape) == 3:
        flatImage = np.max(image, 2)
    else:
        flatImage = image
    assert len(flatImage.shape) == 2

    rows = np.where(np.max(flatImage, 0) > threshold)[0]
    if rows.size:
        cols = np.where(np.max(flatImage, 1) > threshold)[0]
        image = image[cols[0]: cols[-1] + 1, rows[0]: rows[-1] + 1]
    else:
        image = image[:1, :1]

    return image


if __name__ == "__main__":
    image = cv2.imread('sampleImage.png')
    crop = autocrop(image, 165)
    d = pyzbar.decode(crop)
    print(d)
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • I did that and it still behaves the same: decoding on RaspberryPi fails, and on Ubuntu decodes successfully. – vg34 Jun 12 '19 at 19:51
  • Are you using this example for images read from the camera? I put the example for the provided image, not on any captured image from Pi camera. – Masoud Rahimi Jun 13 '19 at 07:18
  • The provided image was captured from Pi camera as well. It was just an example image which fully described the problem. – vg34 Jun 13 '19 at 07:51