Custom-Trained Model Example
Custom-trained model on HUSKYLENS 2 lets UNIHIKER M10 read object IDs, names and coordinates in real time; it’s like giving your camera a custom label-reading brain. Users deploy a Product Recognition model, press A to learn objects and assign IDs, then use Python APIs such as getCachedResultNum, getCachedCenterResult and getCachedIndexResultByID to count total and learned targets, find the center object and query specific IDs, including multiple instances with the same label.
1.Custom-Trained Model
In addition to the visual recognition functions built into HUSKYLENS 2, users can also train their own models and deploy them to HUSKYLENS 2 to create their own unique visual recognition projects. To use this feature, please see the Deploy Custom-Trained Models
Note: Please ensure the current HUSKYLENS 2 firmware is version 1.15 or above. Firmware Update Tutorial)
The following will use a self-trained object detection model "Product Recognition" as an example to introduce how the UNIHIKER M10 reads the recognition results of the HUSKYLENS 2 self-trained model.
1.1 Detect Target and Output Relevant Data
HUSKYLENS 2 can recognize target objects appearing in the frame (it can only recognize object classes that were in the dataset when the user trained the model). You can get data about detected target objects through programming.
After the self-trained model is deployed, it can recognize objects of the same class that the user has labeled. Like other functions, the self-trained model application supports learning the detected objects. When a target object is framed, press the A button to learn the object and assign it an ID.
Readable target data includes: the total number of detected targets, the total number of learned targets, and data for a specific target, including its ID, Name, 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(129);
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(129);
if (huskylens.available(129)):
line1.config(text=(str("Objects count:") + str((huskylens.getCachedResultNum(129)))))
line2.config(text=(str("Learned objects count:") + str((huskylens.getCachedResultMaxID(129)))))
line3.config(text=(str("Center object ID:") + str((huskylens.getCachedCenterResult(129).ID if huskylens.getCachedCenterResult(129) else -1))))
line4.config(text=(str("1st object ID:") + str(huskylens.getCachedIndexResultByID(129, 1, 1-1).name if huskylens.getCachedIndexResultByID(129, 1, 1-1) else -1)))
Output Result: As shown, 2target objects are recognized in the frame, one of which are learned. The target closest to the center is ID 0 (mouse), and the first recognized target is named apple. Objects learned in HUSKYLENS 2 are assigned IDs in order; unlearned objects have an ID of 0.
1.2 Get Data for a Specific Target in the Frame
After HuskyLens 2 recognizes target objects, it can acquire data for a specific target in the frame. For example, it can determine if a target with a specific ID is in the frame, get the count of targets with the same ID, and when multiple targets with the same ID appear, it can be set to acquire parameters for a specific one, including ID, 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(129);
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")
line6=u_gui.draw_text(text="",x=0,y=200,font_size=15, color="#0000FF")
while True:
huskylens.getResult(129);
if (huskylens.available(129)):
if ((huskylens.getCachedResultByID(129, 1) is not None)):
line1.config(text=(str("Object ID1 count:") + str((huskylens.getCachedResultNumByID(129, 1)))))
line2.config(text=(str("Object ID1 name:") + str((huskylens.getCachedResultByID(129, 1).name if huskylens.getCachedResultByID(129, 1) else -1))))
line3.config(text="1st object ID1")
line4.config(text=(str("Coords:") + str((str(huskylens.getCachedIndexResultByID(129, 1, 1-1).xCenter if huskylens.getCachedIndexResultByID(129, 1, 1-1) else -1) + str((str(",") + str(huskylens.getCachedIndexResultByID(129, 1, 1-1).yCenter if huskylens.getCachedIndexResultByID(129, 1, 1-1) else -1)))))))
line5.config(text="2nd object ID1")
line6.config(text=(str("Coords:") + str((str(huskylens.getCachedIndexResultByID(129, 1, 2-1).xCenter if huskylens.getCachedIndexResultByID(129, 1, 2-1) else -1) + str((str(",") + str(huskylens.getCachedIndexResultByID(129, 1, 2-1).yCenter if huskylens.getCachedIndexResultByID(129, 1, 2-1) else -1)))))))
Output Result: As shown, there are 2 ID 1 target objects in the frame, named 'person' . The coordinates of the first recognized ID 3 target are (319, 237).
Was this article helpful?
