Barcode Recognition Example
Barcode recognition reads count, ID, content, and position, like a smart scanner showing what and where each code is; HuskyLens 2 uses Python APIs to list all barcodes in view or query specific IDs, returning center coordinates, width, height, and text for multiple codes
1.Barcode Recognition
Before You Start:
1.1 Barcode Recognition - Output Relevant Data
HUSKYLENS 2 can recognize barcodes appearing in the frame. You can get data about detected barcodes through programming. Readable barcode data includes: the total number of detected barcodes, and data for a specific barcode, 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_BARCODE_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_BARCODE_RECOGNITION)
if (huskylens.available(ALGORITHM_BARCODE_RECOGNITION)):
line1.config(text=(str("Barcode count:") + str((huskylens.getCachedResultNum(ALGORITHM_BARCODE_RECOGNITION)))))
line2.config(text=(str("Learned barcode count:") + str((huskylens.getCachedResultMaxID(ALGORITHM_BARCODE_RECOGNITION)))))
line3.config(text=(str("Center barcode ID:") + str((huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION) else -1))))
line4.config(text=(str("1st barcode ID:") + str((huskylens.getCachedResultByIndex(ALGORITHM_BARCODE_RECOGNITION, 1-1).ID if huskylens.getCachedResultByIndex(ALGORITHM_BARCODE_RECOGNITION, 1-1) else -1))))
Click 'Run' in Mind+ and wait for the program to upload.
Point the HuskyLens 2 camera at the barcode in the frame to learn it. For detailed instructions on how to learn barcodes, refer to: Barcode Recognition
Point the HUSKYLENS 2 camera at the barcode and observe the results displayed on the M10 (User provided K10, but document context is M10) screen.
Output Result:As shown, it can output the number of detected barcodes (whether learned or not) and the specified barcode ID. Unlearned barcodes have an ID of 0.
1.2 Get Data for a Specific Barcode in the Frame
After HuskyLens 2 recognizes barcodes, it can acquire data for a specific barcode in the frame. For example, it can determine if a barcode with a specific ID is in the frame, get the count of barcodes with the same ID, and when multiple barcodes 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_BARCODE_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_BARCODE_RECOGNITION)
if (huskylens.available(ALGORITHM_BARCODE_RECOGNITION)):
line1.config(text=(str("Barcode ID0 count:") + str((huskylens.getCachedResultNumByID(ALGORITHM_BARCODE_RECOGNITION, 0)))))
line2.config(text="1th barcode ID0")
line3.config(text=(str("Content;") + str(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1-1).content if huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1-1) else -1)))
line4.config(text="2nd barcode ID0")
line4.config(text=(str("Coords:") + str((str(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1-1).xCenter if huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1-1) else -1) + str((str(",") + str(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1-1).yCenter if huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1-1) else -1)))))))
Output Result:As shown, there are two unlearned barcodes (ID 0) in the frame. The first ID 0 barcode is at the bottom right, and its content is '23'. The second ID 0 barcode is the top one, and its coordinates are (391, 345).
Was this article helpful?
