QR Code Recognition Example

HuskyLens 2 QR example shows how to read QR count, learned IDs and per‑code content, size and center coordinates via Python on UNIHIKER. It’s like a smart camera that tags each QR in view, letting you query specific IDs, count duplicates and track their positions in real time.

1.QR Code Recognition

Before You Start:

1.1 QR Code Recognition - Output Relevant Data

HUSKYLENS 2 can recognize QR codes appearing in the frame. You can get data about detected QR codes through programming. Readable QR code data includes: the total number of detected QR codes, the total number of learned QR codes, and data for a specific QR code, including its ID, Content, Width, Height, and X/Y coordinates of the center point.

Example Program:

from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *


u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_I2C()
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_QRCODE_RECOGNITION)
line1=u_gui.draw_text(text="",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="",x=0,y=80,font_size=15, color="#0000FF")
line4=u_gui.draw_text(text="",x=0,y=120,font_size=15, color="#0000FF")
while True:
    huskylens.getResult(ALGORITHM_QRCODE_RECOGNITION)
    if (huskylens.available(ALGORITHM_QRCODE_RECOGNITION)):
        line1.config(text=(str("QR code count:") + str((huskylens.getCachedResultNum(ALGORITHM_QRCODE_RECOGNITION)))))
        line2.config(text=(str("Learned QR code count:") + str((huskylens.getCachedResultMaxID(ALGORITHM_QRCODE_RECOGNITION)))))
        line3.config(text=(str("Center QR code ID:") + str((huskylens.getCachedCenterResult(ALGORITHM_QRCODE_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_QRCODE_RECOGNITION) else -1))))
        line4.config(text=(str("1st QR code ID:") + str((huskylens.getCachedResultByIndex(ALGORITHM_QRCODE_RECOGNITION, 1-1).ID if huskylens.getCachedResultByIndex(ALGORITHM_QRCODE_RECOGNITION, 1-1) else -1))))

Click 'Run' in Mind+ and wait for the program to upload.

Point the HuskyLens 2 camera at the QR code in the frame to learn it. For detailed instructions on how to learn QR codes, refer to: QR Code Recognition.

Point the HUSKYLENS 2 camera at the QR code and observe the results displayed on the UNIHIKER M10 screen.

Output Result:As shown, it can output the number of detected QR codes (whether learned or not), the number of learned QR codes, and the specified QR code ID. Unlearned QR codes have an ID of 0.

Interface Diagram

1.2 Get Data for a Specific QR Code in the Frame

After HuskyLens 2 recognizes QR codes, it can acquire data for a specific QR code in the frame. For example, it can determine if a QR code with a specific ID is in the frame, get the count of QR codes with the same ID, and when multiple QR codes with the same ID appear, it can be set to acquire parameters for a specific one, including Name, Content, X/Y coordinates, Width, and Height.

Example Program:

from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *


u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_I2C()
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_QRCODE_RECOGNITION)
line1=u_gui.draw_text(text="",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="",x=0,y=80,font_size=15, color="#0000FF")
line4=u_gui.draw_text(text="",x=0,y=120,font_size=15, color="#0000FF")
line5=u_gui.draw_text(text="",x=0,y=160,font_size=15, color="#0000FF")
while True:
    huskylens.getResult(ALGORITHM_QRCODE_RECOGNITION)
    if (huskylens.available(ALGORITHM_QRCODE_RECOGNITION)):
        line1.config(text=(str("QR code  ID0 count:") + str((huskylens.getCachedResultNumByID(ALGORITHM_QRCODE_RECOGNITION, 0)))))
        line2.config(text="1st QR code ID0:")
        line3.config(text=(str("Content:") + str((huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 1-1).content if huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 1-1) else -1))))
        line4.config(text="2nd QR ID0")
        line5.config(text=(str("Coords:") + str((str((huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 1-1).xCenter if huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 1-1) else -1)) + str((str(",") + str((huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 1-1).yCenter if huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 1-1) else -1))))))))

Output Result: As shown, there are two unlearned QR codes (ID 0) in the frame. The first ID 0 QR code is on the right, and its content is 'abc' . The second ID 0 QR code is on the left, and its coordinates are (188, 150).

Interface Diagram

Was this article helpful?

TOP