Color Recognition Example

Color recognition on HuskyLens 2 tracks color blocks and streams IDs, names, XY, width and height to micro:bit via serial. It’s like a smart color counter that also measures size and position so kids can build games, sorting projects and interactive displays.

1. Color Recognition Project Example

Before You Start:

1.1 Recognize Colors and Output Related Data

When color recognition is enabled, color blocks appearing on the HuskyLens 2 screen will be detected and framed, allowing you to access key data: total detected color blocks, total trained colors, center-closest color block, nth detected color block, and specified ID color. This data includes the color's ID, name, center coordinates (XY), width, and height.

Example program:

Interface Diagram

Connect to micro:bit and click Download to transfer the program. After successful download, click Show Data to activate data output mode. Align the HuskyLens 2 camera with a color block to observe the output.
As shown, when HuskyLens 2 detects three colors named "Yellow" and "Blue", the following data will display:
How to learn colors, please refer to the Color Recognition.

Result: The serial port will output: total detected color blocks, ID of the center-closest color, name of the first detected color, and total the learned colors.

Interface Diagram

There are 3 detected color blocks, so the dot matrix display shows "3".

Interface Diagram

1.2 Obtaining Data of a Specific Color

When multiple color blocks with the same ID appear simultaneously, the program can count their occurrences and retrieve data for the nth color block in the detected sequence.

Example program:

Interface Diagram

Result: As shown, click "Download" to transfer the program. Align HuskyLens 2 with two color blocks labeled ID 1. After activation, the serial port will output data statistics for these two color blocks, including their individual positions and dimensions.

Interface Diagram

1.3 Color Recognition Blocks Description

block Description
Used to obtain one set of Color Recognition data under the Color Recognition function of HUSKYLENS 2.
Determine if a color block is present in the current frame.
Retrieves data of the color block closest to the screen cross cursor, including color ID, custom color name, center X/Y coordinates, width, and height.
Get the total number of color blocks detected on the screen, including both unregistered and registered colors.
When multiple color blocks are detected, retrieves data for a specified color block.
Get the total number of learned colors, regardless of whether they appear in the current frame.
Determine if a color with the specified ID exists in the frame. Unlearned IDs are uniformly set to 0, and learned IDs are sorted in the order they were learned.
Gets the total number of detected color blocks with a specified ID on the screen.
When multiple colors appear, obtain the relevant data of a specified face.
When multiple colors with the same ID appear, obtain the relevant data of a specified color.

Was this article helpful?

TOP