Color Recognition Example
HUSKYLENS 2 color recognition reads color block IDs and positions over UART, like a smart color scanner for Arduino. It detects all outlined blocks, reports center ID, total count, and first ID, and can query a specific learned color’s name, quantity, and X/Y center coordinates using cached results APIs.
##1. Color Recognition Code Example
Before You Start:
1.1 Recognize Color and Output Related Data
This feature can recognize color blocks within the field of view of HUSKYLENS 2 and print related data via UART. Data that can be read includes: the ID number of the color block closest to the camera view center, the total number of detected color blocks, the ID number of the first detected color block, etc.
The example code is as follows.
#include "DFRobot_HuskylensV2.h"
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
huskylens.switchAlgorithm(ALGORITHM_COLOR_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_COLOR_RECOGNITION);
if ((huskylens.available(ALGORITHM_COLOR_RECOGNITION))) {
Serial.println((String("ID of the color block near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_COLOR_RECOGNITION), Result, ID)))));
Serial.println((String("Total number of detected color blocks: ") + String((huskylens.getCachedResultNum(ALGORITHM_COLOR_RECOGNITION)))));
Serial.println((String("ID of the first detected color block: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_COLOR_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
Align the crosshair of HUSKYLENS 2 with the color block on the screen to learn it. For detailed color learning operations, please refer to: Color Recognition Function.
Running Result: The total number of detected color blocks can be output, regardless of whether the color block has been learned or not; it will be counted as long as the box outlines it. The corresponding color block ID number can be output as required. Among these, the color block near the center is framed by a white box; that is, an unlearned color block, so its output ID number is 0.
1.2 Obtain the relevant data of the specified color
After HUSKYLENS 2 recognizes a color, it can retrieve relevant data about the specified color. For example, it can determine whether a learned color is present in the image, the name of the specified color, and the number of color blocks of the same color in the image. When multiple color blocks of the same color appear in the image, you can specify to retrieve the parameters of one of the color blocks, including name, X/Y coords, width, and height.
The example program is as follows:
#include "DFRobot_HuskylensV2.h"
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
huskylens.switchAlgorithm(ALGORITHM_COLOR_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_COLOR_RECOGNITION);
if ((huskylens.available(ALGORITHM_COLOR_RECOGNITION))) {
if (((huskylens.getCachedResultByID(ALGORITHM_COLOR_RECOGNITION, 1) != NULL))) {
Serial.println((String("Number of ID1 color blocks: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_COLOR_RECOGNITION, 1)))));
Serial.println((String("Name of ID1 color block: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_COLOR_RECOGNITION, 1), Result, name)))));
Serial.println((String("Center coordinates of the first detected ID1 color block: ") + String((String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 1-1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 1-1), Result, yCenter)))))))));
}
}
delay(500);
}
Running Result: As shown in the figure, Obtained are the total number of ID1 color blocks in the image, the name of the ID1 color block, and the coordinates of the first detected ID1 color block.
Was this article helpful?
