Color Recognition Example
Color recognition on HUSKYLENS 2 lets you count, label and locate colored blocks in view. It’s like giving the camera colored stickers and asking it how many it sees and where they are. The examples show how to read total and learned blocks, center or first IDs, and per‑color counts, names and coordinates via Python APIs.
1.Color Recognition
Before You Start:
1.1 Color Recognition - Output Relevant Data
It can recognize color blocks within the HUSKYLENS 2 field of view and output color block-related data. Readable data includes: the total number of detected color blocks, the total number of learned color blocks, the ID of the color block closest to the center of the HUSKYLENS 2 camera frame, and the ID of the first detected color block.
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_FACE_RECOGNITION)
line1=u_gui.draw_text(text="Total faces:",x=0,y=0,font_size=20, color="#0000FF")
line2=u_gui.draw_text(text="Center face:",x=0,y=40,font_size=20, color="#0000FF")
line3=u_gui.draw_text(text="First face ID:",x=0,y=80,font_size=20, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_FACE_RECOGNITION)
if (huskylens.available(ALGORITHM_FACE_RECOGNITION)):
line1.config(text=(str("Total faces:") + str((huskylens.getCachedResultNum(ALGORITHM_FACE_RECOGNITION)))))
line2.config(text=(str("Center face:") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))
line3.config(text=(str("First face ID:") + str(huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1).ID if huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1) else -1)))
Click 'Run' in Mind+ and wait for the program to upload.
Point the HUSKYLENS 2's crosshair at a color block to learn it. For detailed instructions on how to learn colors, refer to: Color Recognition.
Once learning is complete, aim the HUSKYLENS 2 camera at the color blocks and observe the output results.
Output Result:It can output the total number of detected color blocks. All color blocks framed by a box will be counted, whether they have been learned or not. It can output corresponding data as required, such as the second line, which outputs the total number of learned color blocks. The third and fourth lines output the specified color block ID. The color block near the center of the frame is outlined in a white box, indicating it is an unlearned color block, so the "ID of color block near center" is 0.
1.2 Get Data for a Specific Color
After HUSKYLENS 2 recognizes a color, it can acquire data for a specific color in the frame. For example, it can determine if a specific color is in the frame, get the name of a specific color, or get the count of color blocks of the same specific color in the frame. When multiple color blocks of the same color appear, it can be set to acquire parameters for a specific one, including its Name, 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_COLOR_RECOGNITION)
line1=u_gui.draw_text(text="Color ID1 count:",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="Color ID1 name:",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="First color ID1",x=0,y=80,font_size=15, color="#0000FF")
line4=u_gui.draw_text(text="Coords:",x=0,y=120,font_size=15, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_COLOR_RECOGNITION)
if (huskylens.available(ALGORITHM_COLOR_RECOGNITION)):
if ((huskylens.getCachedResultByID(ALGORITHM_COLOR_RECOGNITION, 1) is not None)):
line1.config(text=(str("Color ID1 count:") + str((huskylens.getCachedResultNumByID(ALGORITHM_COLOR_RECOGNITION, 1)))))
line2.config(text=(str("Color ID1 name:") + str((huskylens.getCachedResultByID(ALGORITHM_COLOR_RECOGNITION, 1).name if huskylens.getCachedResultByID(ALGORITHM_COLOR_RECOGNITION, 1) else -1))))
line4.config(text=(str("Coords:") + str((str((huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 1-1).xCenter if huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 1-1) else -1)) + str((str(",") + str((huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 1-1).yCenter if huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 1-1) else -1))))))))
Output Result:As shown, it can acquire the total count and name of color block ID 1 in the frame, and the coordinate position of the first detected ID 1 color block. (Color names can be customized; the default is "Color".
Was this article helpful?
