Instance Segmentation Example

Instance segmentation on HuskyLens 2 lets you detect object classes, draw their outlines, and read per‑instance data like ID, name, center X/Y, width, and height in real time. It’s like giving the camera sticky notes that label each object separately. Sample Python code shows how to count all instances, find the one closest to the image center, and display its coordinates, and another example focuses on a specific learned ID, returning how many similar instances are in view and the exact position of a chosen one.

1.Instance Segmentation Example

Before You Start:

1.1 Instance Segmentation - Output Relevant Data

Under the Instance Segmentation function, HUSKYLENS can recognize the class of objects in an image and mark the outline of each object. You can use a program to print the total number of instances recognized by HUSKYLENS, the instance closest to the center, and the Name, ID, center X/Y coordinates, Width, and Height of a specified ID instance.

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_SEGMENT)
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_SEGMENT)
    if (huskylens.available(ALGORITHM_SEGMENT)):
        line1.config(text=(str("Instances count:") + str((huskylens.getCachedResultNum(ALGORITHM_SEGMENT)))))
        line2.config(text=(str("Center instance ID:") + str((huskylens.getCachedCenterResult(ALGORITHM_SEGMENT).ID if huskylens.getCachedCenterResult(ALGORITHM_SEGMENT) else -1))))
        line3.config(text="First instance ID:")
        line4.config(text=(str("Coords:") + str((str((huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 1 - 1).xCenter if huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 1 - 1) else -1)) + str((str(",") + str((huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 1 - 1).yCenter if huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 1 - 1) else -1))))))))

Click 'Run' in Mind+ and wait for the program to upload. HuskyLens 2 will automatically enter the Instance Segmentation function. Point the HuskyLens 2 camera at the instance in the frame to learn it. For detailed instructions on how to learn instances, refer to: Instance Segmentation

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

Output Result: After the program is successfully uploaded, HUSKYLENS 2 will automatically switch to the Instance Segmentation function. Point HUSKYLENS 2 at the object to be recognized (must be one of the 80 classes) and observe the number of recognized instances, the ID of a specific instance, its center point coordinates, and other data.

Interface Diagram

1.2 Get Data for a Specific Instance

Under the Instance Segmentation function, after HUSKYLENS 2 learns, it can acquire data for a specific instance in the frame. For example, it can determine if a learned instance is in the frame, the name of the specified instance, the count of similar instances, and when multiple instances of the same class appear, it can be set to acquire parameters for a specific one, including 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_SEGMENT)
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_SEGMENT)
    if (huskylens.available(ALGORITHM_SEGMENT)):
        line1.config(text=(str("Instances ID1 count:") + str((huskylens.getCachedResultNumByID(ALGORITHM_SEGMENT, 1)))))
        line2.config(text=(str("Instance ID1 name:") + str((huskylens.getCachedResultByID(ALGORITHM_SEGMENT, 1).name if huskylens.getCachedResultByID(ALGORITHM_SEGMENT, 1) else -1))))
        line3.config(text="First instance ID1:")
        line4.config(text=(str("Coords:") + str((str((huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 1-1).xCenter if huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 1-1) else -1)) + str((str(",") + str((huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 1-1).yCenter if huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 1-1) else -1))))))))

Output Result: When multiple instances of the same class appear in the frame, it can recognize the count of instances of that class and output the specified instance's ID, center point coordinates, and other related data.

Interface Diagram

Was this article helpful?

TOP