PYZBAR how to get certain outputs from DECODE function

I'm using python PYZBAR to decode QR code captured by real time cam.

decode(input_frame)

Naturally the output I get is as follows

[Decoded(data=b'DATA', type='QRCODE', rect=Rect(left=50, top=179, width=255, height=254), polygon=[Point(x=50, y=433), Point(x=305, y=424), Point(x=302, y=180), Point(x=51, y=179)], quality=1, orientation='UP')]

the output shows:

  • The data stored in the QR Code.
  • Code type (QRCODE).
  • The position of each corner of the QR Code in the frame.
  • Quality.
  • And the orientation of the QR Code (whether it up, down, left or right).

I'm only concerned about the data stored in the QR Code and its Orientation

How can I define them as a variable to use them later, something like the following pseudo code:

x = orientation y = data stored in QR Code
if x == up && y == DATA:
...........

My goal is to read and decode QR Code data and orientation to navigate a robot like the video provided in the link.

https://www.youtube.com/watch?v=DZe27aKX0kY

2 answers

  • answered 2022-05-06 15:33 Omar Diaa

    solved the issue, this is the new code

    from pyzbar.pyzbar import decode
    import cv2
    import serial
    import time
    
    arduino = serial.Serial(port='COM6', baudrate=115200, timeout=1)
    
    
    cap = cv2.VideoCapture(0)
    
    
    def write_read(x):
        arduino.write(bytes(x, 'utf-8'))
        time.sleep(0.05)
        data = arduino.readline()
       return data
    
    
    def get_qr_data(input_frame):
    try:
        return decode(input_frame)
    except:
        return []
    
    while True:
    _, frame = cap.read()
    qr_obj = get_qr_data(frame)
    try:
     data = qr_obj[0].data
     angle = qr_obj[0].orientation
     print(data)
     print(angle)
    except IndexError:
     pass
    cv2.imshow("DD", frame)
    
    if qr_obj == get_qr_data(frame):
     arduino.write(b'O')
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
    
      break
    
    
    cap.release()
    cv2.destroyAllWindows()
    

  • answered 2022-05-06 19:40 Jeru Luke

    You figured out a solution to your problem. Let me show you something that exists within OpenCV

    OpenCV also has an in-built function to detect QR codes since version 4.x. There is a dedicated module for this purpose cv2.QRCodeDetector(). It gives you:

    • data associated with QR code
    • location of it

    You can also detect QR code on a curved surface using cv2.QRCodeDetector().decodeCurved()

    And multiple QR codes in an image using cv2.QRCodeDetector(). decodeMulti()

    To get more info on the same, type the following and execute

    help(cv2.QRCodeDetector())
    

    Also refer this useful blog post

    Also refer to this recent answer by Christoph Rackwitz

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum