[UNIHIKER K10][Arduino IDE][C++] Instance Segmentation
Last revision 2026/08/11
HUSKYLENS 2 instance segmentation lets UNIHIKER K10 display object count, center ID, coordinates and per‑ID stats, like a smart camera tagging each item on screen.
1. Instance Segmentation Code Example
Before You Start:
1.1 Instance Segmentation and Output Related Data
In instance segmentation mode, HUSKYLENS 2 can recognize object categories in an image and mark the contours of each individual object. The program can print the following information for HUSKYLENS 2 detected instances: total number of instances, the instance closest to the center, and for the specified ID instance: name, ID, center X/Y coordinates, width, and height.
The example program is as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: segment; UNIHIKER K10: screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10
Wire.begin(); // Initialize I2C communication
// Wait for Huskylens connection (retry every 100ms)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Init screen with set direction
k10.creatCanvas(); // Create drawing canvas
huskylens.switchAlgorithm(ALGORITHM_SEGMENT); // Set to segment (image segmentation) mode
}
void loop() {
huskylens.getResult(ALGORITHM_SEGMENT); // Get latest segment data
if (huskylens.available(ALGORITHM_SEGMENT)) {
// Simplify index: 1-1 → 0 (more intuitive, same meaning)
k10.canvas->canvasText(String("Total instances: ") + String(huskylens.getCachedResultNum(ALGORITHM_SEGMENT)), 1, 0x0000FF);
k10.canvas->canvasText(String("Center instance ID: ") , 2, 0x0000FF);
k10.canvas->canvasText( String(RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_SEGMENT), Result, ID)), 3, 0x0000FF);
k10.canvas->canvasText("1st instance coords", 4, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 0), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 0), Result, yCenter)), 5, 0x0000FF);
}
k10.canvas->updateCanvas(); // Refresh screen
delay(500); // 500ms delay for stable data reading
}
After the program is successfully uploaded, HUSKYLENS 2 will automatically switch to the Instance Segmentation function. Align HUSKYLENS 2 with the object to be recognized (must be one of the 80 predefined categories), and observe UNIHIKER K10's screen displaying data such as the number of recognized instances, the ID of the target instance, and the center coordinates. Instance Segmentation function instruction : Instance Segmentation.
1.2 Get Data of Specified Instance
In Instance Segmentation mode, after HUSKYLENS 2 has been trained, it can obtain relevant data about specified instances in the image. For example: determining if a trained instance exists in the image; getting the name of a specified hand instance; and counting the number of instances of the same category. When multiple instances of the same category appear in the image, specify to get the parameters of a specific instance, including name, X/Y coordinates, width, and height.
The example program is as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: segment; UNIHIKER K10: screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10
Wire.begin(); // Initialize I2C communication
// Wait for Huskylens connection (retry every 100ms)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Init screen with set direction
k10.creatCanvas(); // Create drawing canvas
huskylens.switchAlgorithm(ALGORITHM_SEGMENT); // Set to segment (image segmentation) mode
}
void loop() {
huskylens.getResult(ALGORITHM_SEGMENT); // Get latest segment data
if (huskylens.available(ALGORITHM_SEGMENT)) {
// Check if ID1 instance exists and display its info
if (huskylens.getCachedResultByID(ALGORITHM_SEGMENT, 1) != NULL) {
// Simplify index: 1-1 → 0 (more intuitive, same meaning)
k10.canvas->canvasText(String("ID1 instance count: ") , 1, 0x0000FF);
k10.canvas->canvasText(String(huskylens.getCachedResultNumByID(ALGORITHM_SEGMENT, 1)), 2, 0x0000FF);
k10.canvas->canvasText(String("ID1 instance name: "), 3, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_SEGMENT, 1), Result, name)), 4, 0x0000FF);
k10.canvas->canvasText("1st ID1 coords:", 5, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 0), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 0), Result, yCenter)), 6, 0x0000FF);
}
}
k10.canvas->updateCanvas(); // Refresh screen
delay(500); // 500ms delay for stable data
}
Running Result:After the program is successfully uploaded, HUSKYLENS 2 will automatically switch to the Instance Segmentation function. When multiple instances of the same category appear in the frame, it can recognize the quantity of instances of the same category, and UNIHIKER K10 will display relevant data such as the designated instance's ID, center coordinates, and confidence score.
Was this article helpful?
