Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orientation of QR using Zbar python

I am detecting QR code with zbar in python. Right now I am getting 4 points in detected qr code.

decodedObjects = pyzbar.decode(im)

However, I am getting 4 points but no information about orientation. I need fixed orientation.

My query images are:enter image description here

enter image description here

enter image description here

I want all of them to be like:enter image description here

How can i fix the orientation? Any suggestions? I am open to use anything other than zbar or use combination of zbar + other options to fix this problem.

like image 914
Hasan Iqbal Avatar asked Mar 21 '26 10:03

Hasan Iqbal


1 Answers

You can just use OpenCV and pyzbar to do this.

Use decoded.rect to crop the image and decoded.orientation to rotate the image.

decoded.rectreturns the bounding box of QR Code, and decoded.orientation returns 5 types of ZBarOrientation class:

  • UNKNOWN: when pyzbar are unable to determine orientation;
  • UP: upright, read left to right;
  • RIGHT: sideways, read top to bottom;
  • DOWN: upside-down, read right to left;
  • LEFT: sideways, read bottom to top.

In this example, we are using this image as input:

enter image description here

# Imports
import cv2 as cv
from pyzbar import pyzbar

# Open image with OpenCV
img_input = cv.imread(filename = 'image.jpg',
                      flags = cv.IMREAD_GRAYSCALE)

Note that when importing the images, we use the flags = cv.IMREAD_GRAYSCALE parameter, because in OpenCV the default color mode setting is BGR. Therefore, to work with pyzbar, we need to convert the color mode pattern from BGR to grayscale.

Now we will use pyzbar to decode the image into QR Code, if any.

And then, we'll use decoded.rect to crop the image and decoded.orientation to rotate the image:

decoder = pyzbar.decode(img_input)

if len(decoder) != 0:
    for decoded in decoder:
        if decoded.type == 'QRCODE':
            # Get the bounding box location of the QR Code
            left, top, width, height = decoded.rect

            # Crop input image (add 10 pixels to each side)
            qrcode = img_input[top - 10: top + height + 10, left - 10: left + width + 10]

            # Rotate QR Code, if necessary
            if decoded.orientation is not None and decoded.orientation != 'UNKNOWN':
                if decoded.orientation == 'LEFT':
                    # Rotate by 90 degrees clockwise
                    qrcode = cv.rotate(qrcode, cv.ROTATE_90_CLOCKWISE)
                elif decoded.orientation == 'RIGHT':
                    # Rotate by 270 degrees clockwise
                    qrcode = cv.rotate(qrcode, cv.ROTATE_90_COUNTERCLOCKWISE)
                elif decoded.orientation == 'DOWN':
                    # Rotate by 180 degrees clockwise
                    qrcode = cv.rotate(qrcode, cv.ROTATE_180)

Now we can simply save the QR Code image with the following command:

            # Save image
            cv.imwrite('cropped_qrcode.jpg', qrcode)

And the output will be:

enter image description here

Important information taken from the pyzbar documentation:

If you see orientation=None then your system has an older release of zbar that does not support orientation.

like image 91
whoisraibolt Avatar answered Mar 23 '26 23:03

whoisraibolt