Object Recognition Example

HuskyLens 2 object recognition reads counts, IDs and positions, like a smart camera HUD overlaying stats on each object in view. It recognizes up to 80 preset categories, then via Python and Mind+ you can display total objects, learned ID range, center object ID and first object ID in real time. For a specific ID, the example shows how to check if it appears, read its name, count all similar targets and get the first one’s X/Y center plus bounding box width and height, turning raw vision into structured data for interactive STEM projects.

1.Object Recognition

Before You Start:

1.1 Object Recognition - Output Relevant Data

It can recognize objects within the HuskyLens 2 field of view (must be among the 80 fixed categories of recognizable objects, see Object Recognition function description for details), and acquire object-related data. Readable data includes: the total number of recognizable objects in the frame, the ID of the object closest to the center of the HuskyLens 2 camera frame, and the first detected object.

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_OBJECT_RECOGNITION)
line1=u_gui.draw_text(text="Total objects:",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="Learned object count:",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="Center object:",x=0,y=80,font_size=15, color="#0000FF")
line4=u_gui.draw_text(text="First objects ID:",x=0,y=120,font_size=15, color="#0000FF")
while True:
    huskylens.getResult(ALGORITHM_OBJECT_RECOGNITION)
    if (huskylens.available(ALGORITHM_OBJECT_RECOGNITION)):
        line1.config(text=(str("Total objects:") + str((huskylens.getCachedResultNum(ALGORITHM_OBJECT_RECOGNITION)))))
        line2.config(text=(str("Learned object count:") + str((huskylens.getCachedResultMaxID(ALGORITHM_OBJECT_RECOGNITION)))))
        line3.config(text=(str("Center object:") + str((huskylens.getCachedCenterResult(ALGORITHM_OBJECT_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_OBJECT_RECOGNITION) else -1))))
        line4.config(text=(str("First objects ID:") + str((huskylens.getCachedResultByIndex(ALGORITHM_OBJECT_RECOGNITION, 1 - 1).ID if huskylens.getCachedResultByIndex(ALGORITHM_OBJECT_RECOGNITION, 1 - 1) else -1))))

Click 'Run' in Mind+ and wait for the program to upload. HuskyLens 2 will automatically enter the Object Recognition function. Point the HuskyLens 2 camera at the objects in the frame to learn them. For detailed instructions on how to learn objects, refer to: Object Recognition

Once learning is complete, aim at the object and observe the output results on the screen.

Output Result:It can output corresponding data as required. For example, the first line outputs the total number of detected objects, and the second line outputs the total number of learned objects. The third and fourth lines output the specified gesture ID. Learned gestures are assigned IDs in learning order, while unlearned gestures have an ID of 0.

Interface Diagram

1.2 Get Data for a Specific Object in the Frame

After HuskyLens 2 recognizes an object, it can acquire data for a specific object in the frame. For example, it can determine if a specific object is in the frame, get the name of a specific object, get the count of similar objects in the frame, and when multiple similar objects 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_OBJECT_RECOGNITION)
line1=u_gui.draw_text(text="ID2 count:",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="ID2 name:",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="First ID2",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_OBJECT_RECOGNITION)
    if (huskylens.available(ALGORITHM_OBJECT_RECOGNITION)):
        line1.config(text=(str("ID2 count:") + str((huskylens.getCachedResultNumByID(ALGORITHM_OBJECT_RECOGNITION, 2)))))
        line2.config(text=(str("ID2 name:") + str((huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 2).name if huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 2) else -1))))
        line3.config(text="First ID2")
        line4.config(text=(str("Coords:") + str((str((huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 2, 1-1).xCenter if huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 2, 1-1) else -1)) + str((str(",") + str((huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 2, 1-1).yCenter if huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 2, 1-1) else -1))))))))

Output Result: As shown, it can acquire the total number of objects in the frame, the count and name of ID 2 objects, and the coordinate position of the first detected ID 2 object.

Interface Diagram

Was this article helpful?

TOP