1. Software and Hardware Introduction
1.1 Introduction to Arduino IDE
Arduino IDE (Integrated Development Environment) is a programming software specifically designed for the Arduino open-source hardware platform. It is used to write, compile, and upload programs to Arduino main control boards (such as UNO, MEGA, Nano, ESP32, etc.). It is the core tool for the development of all Arduino projects.
Arduino IDE official website: https://www.arduino.cc/en/software/
1.2 DFRobot Arduino Uno R3 Introduction
DFRduino UNO R3 is an open-source Simple I/O platform, with a development environment similar to Java and C. It allows you to quickly create interactive projects using Arduino language and software like Flash or Processing...
Purchase link: https://www.dfrobot.com/product-838.html
Hardware Connection
Required Materials
- Hardware
- DFRduino UNO R3 (or similar main board) x 1
- Gravity:IO Expansion Shield for Arduino V7.1 x 1
- USB Cable A-B for Arudino Uno/Mega x 1
- HUSKYLENS 2 x 1
- USB cable x 1
- 4-pin jumper cable (or DuPont wires) x 1
Connect the HUSKYLENS 2 to Arduino board's corresponding I2C pins, and connect the Arduino Uno to your computer via a USB cable. Power Supply Note: The voltage output from Arduino Uno's I2C pins is insufficient to power HUSKYLENS 2 properly. When connecting HUSKYLENS 2 to Arduino Uno, use an additional USB-C cable to connect its USB-C port to a power source to provide additional power. The I2C wiring diagram can be referenced in the following figure.
HUSKYLENS 2 uses I2C as its default communication method. If your connection is via UART, please manually switch the protocol type under Protocol Selection in the system settings.
3. Load HUSKYLENS 2 Library
Step 1: Download the HUSKYLENS Arduino Library first.
Click here to download the library: https://github.com/DFRobot/DFRobot_HuskylensV2.
Step 2: Unzip the downloaded file to the directory of the "libraries" folder where Arduino IDE is located, and rename the file to HUSKYLENS 2.
4. Face Recognition Code Example
4.1 Face Recognition Output Data
Under the face recognition function, when a face appears on the HUSKYLENS 2 screen, it can be detected and framed. We can obtain data about the face closest to the crosshair in the HUSKYLENS 2 image and print it via the serial port. The readable face data includes: face ID, face name, width, height, and the X and Y coordinates of the face center.
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_FACE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
Serial.println((String("Face ID") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, ID)))));
Serial.println((String("Face name") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, name)))));
Serial.println((String("Face center coordinates") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, yCenter)))))))));
Serial.println((String("Face width") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, width)))));
Serial.println((String("Face height") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, height)))));
delay(500);
}
}
Running Result:: After the program is successfully uploaded,
Point the camera at a learned face. For a previously learned face, the output ID will match the ID displayed on the HUSKYLENS 2 screen.
Please refer to the detailed operation guide for learning how to perform face recognition.:HUSKYLENS 2 WIKI
4.2 Counting Faces and Outputting Specified Data
When multiple faces appear in the frame, use the example program below to count the number of faces and retrieve data related to a specific face ID.
Example Program Below
#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_FACE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
Serial.println((String("Number of detected faces: ") + String((huskylens.getCachedResultNum(ALGORITHM_FACE_RECOGNITION)))));
}
if (((huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1) != NULL))) {
Serial.println((String("Total number of ID1 faces: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_FACE_RECOGNITION, 1)))));
Serial.println((String("Name of ID1 face: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, name)))));
Serial.println((String("Center coordinates of ID1 face: ") + String((String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, yCenter)))))))));
Serial.println((String("Width of ID1 face: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, width)))));
Serial.println((String("Height of ID1 face: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, height)))));
}
delay(500);
}
Running Result:: As shown in the figure, running the program, the serial port prints the number of faces in the current image. When a face with ID 1 appears in the image, it prints the relevant data of Face ID 1.
5. Object Recognition Code Example
5.1 Output Related Data for Object Recognition
When in Object Recognition mode, HUSKYLENS 2 can recognize objects within the field of view (80 fixed object categories, see HUSKYLENS 2 WIKI) and output relevant data via UART. The readable data includes: total number of recognizable objects in the frame, the object ID closest to the center of the HUSKYLENS 2 camera view, and the first detected object.
Example Program Below
#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_OBJECT_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_RECOGNITION);
if ((huskylens.available(ALGORITHM_OBJECT_RECOGNITION))) {
Serial.println((String("Total number of detected objects: ") + String((huskylens.getCachedResultNum(ALGORITHM_OBJECT_RECOGNITION)))));
Serial.println((String("Name of the object near the center") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_RECOGNITION), Result, name)))));
Serial.println((String("Name of the first detected object") + String((RET_ITEM_STR(huskylens.getCachedResultByIndex(ALGORITHM_OBJECT_RECOGNITION, 1 - 1), Result, name)))));
}
delay(500);
}
Running Result: The system will output the total number of recognized objects and corresponding object ID numbers as required.
5.2 Get Relevant Data of the Specified Object in the Image
After HUSKYLENS 2 recognizes an object, it can obtain relevant data of the specified object in the image. For example, determine if a learned object is present in the image, get the name of the specified object, obtain the count of the same type of objects in the image. When multiple objects of the same type appear in the image, you can specify to obtain parameters of one of them, including name, X/Y coordinates, width, and height.
Example program 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_OBJECT_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_RECOGNITION);
Serial.println((String("Total number of detected objects: ") + String((huskylens.getCachedResultNum(ALGORITHM_OBJECT_RECOGNITION)))));
if (((huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 1) != NULL))) {
Serial.println((String("Number of ID1 objects: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_OBJECT_RECOGNITION, 1)))));
Serial.println((String("Name of ID1 object: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 1), Result, name)))));
Serial.println((String("Center coordinates of the first detected object with ID 1: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 1, 1-1), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 1, 1-1), Result, yCenter)))))));
}
delay(500);
}
Running Result:: As shown in the figure, you can obtain the total number of objects in the image, the count of ID1 objects, their names, and the coordinates of the first detected ID1 object.
6. Object Tracking Code Example
6.1 Output Related Data When Tracking Objects
When HUSKYLENS 2 detects a trackable target object, it obtains related tracking data and outputs it via UART. The data that can be read includes: object ID, name, xy coordinates, width, and height.
The example program is shown below
#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_OBJECT_TRACKING);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_TRACKING);
if ((huskylens.available(ALGORITHM_OBJECT_TRACKING))) {
Serial.println((String("ID of the tracked object: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, ID)))));
Serial.println((String("Name of the tracked object: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, name)))));
Serial.println((String("Center coordinates of the tracked object: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, yCenter)))))));
}
delay(500);
}
Align the HUSKYLENS 2 camera with the object to be tracked in the frame (the target must be box-selected first, see HUSKYLENS 2 WIKI for details). After box-selecting the target, open the Serial Monitor to observe the output results.
Running Result:: The object ID, name, xy coordinates, width, and height of the tracked object will be output. The object name defaults to "Object".
7. Color Recognition Code Example
7.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: HUSKYLENS 2 WIKI.
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.
7.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.
8. Self-Learning Classifier Code Example
8.1 Output ID and Name of Learned Objects
During the Self-Learning Classifier function, HUSKYLENS 2 can learn and recognize any custom object. For detailed usage instructions on the custom classification function, please refer to the HUSKYLENS 2 WIKI
During the Self-Learning Classifier function, once an object has been learned, HUSKYLENS 2 will recognize it when encountered again. Use the following example program to obtain the corresponding ID and name of the learned object.
#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_SELF_LEARNING_CLASSIFICATION);
}
void loop() {
huskylens.getResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION);
if ((huskylens.available(ALGORITHM_SELF_LEARNING_CLASSIFICATION))) {
Serial.println((String("ID of the recognized object: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION), Result, ID)))));
Serial.println((String("Name of the recognized object: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION), Result, name)))));
}
}
Running Result:: As follows, when a previously learned object appears in the image, it will be framed and its ID and confidence level will be displayed simultaneously. Open the Serial Monitor to observe the corresponding ID and name in the output. If the name of the object is not set, the output name defaults to: Object.
9. Instance Segmentation Code Example
9.1 Instance Segmentation and Outputting Relevant Data
Under the Instance Segmentation function, HUSKYLENS can recognize the category of objects in the image and mark the contour of each object. You can use the program to print the total number of instances recognized by HUSKYLENS, the instance near the center (by number), and the name, ID, center point (X and Y) coordinates, as well as the width and height of the specified ID instance.
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_SEGMENT);
}
void loop() {
huskylens.getResult(ALGORITHM_SEGMENT);
if ((huskylens.available(ALGORITHM_SEGMENT))) {
Serial.println((String("Total number of recognized instances: ") + String((huskylens.getCachedResultNum(ALGORITHM_SEGMENT)))));
Serial.println((String("Center coordinates of the instance near the center: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_SEGMENT), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_SEGMENT), Result, yCenter)))))))));
Serial.println((String("ID of the 1st recognized instance: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 1 - 1), Result, ID)))));
Serial.println((String("Coordinates of the 1st recognized instance: ") + String((String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 1 - 1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_SEGMENT, 1 - 1), Result, yCenter)))))))));
}
delay(500);
}
Running Result:: After the program is successfully uploaded, HUSKYLENS 2 will automatically switch to the Instance Segmentation function. Align HUSKYLENS 2 with the target object (which must be among the 80 categories), and observe the terminal for data such as the number of recognized instances, the ID of the specified instance, and the center coordinates.
9.2 Getting Data of Specified Instances
Under the Instance Segmentation function, after HUSKYLENS 2 is trained, you can obtain data related to specified instances in the image. For example, determine if a trained instance is present in the image, get the name of a specified hand instance, and the quantity of instances of the same category. When multiple instances of the same category appear in the image, you can specify to obtain the parameters of one of the instances, including name, X/Y coordinates, width, and height.
Example program 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_SEGMENT);
}
void loop() {
huskylens.getResult(ALGORITHM_SEGMENT);
if ((huskylens.available(ALGORITHM_SEGMENT))) {
if (((huskylens.getCachedResultByID(ALGORITHM_SEGMENT, 1) != NULL))) {
Serial.println((String("Total number of ID1 instances in the frame: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_SEGMENT, 1)))));
Serial.println((String("Instance name of ID1: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_SEGMENT, 1), Result, name)))));
Serial.println((String("Coordinates of the 1st detected instance with ID 1: ") + String((String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 1-1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_SEGMENT, 1, 1-1), Result, xCenter)))))))));
}
}
delay(500);
}
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 image, it will recognize the count of instances of the same category and output relevant data such as the specified instance's ID, center coordinates, and confidence score.
10. Hand Recognition Code Example
10.1 Output Relevant Data for Hand Recognition
For Hand Recognition, HUSKYLENS 2 can detect hands, draw hand key points, learn gestures, and recognize learned gestures. Relevant data can be printed via the serial port using the program. The data that can be read includes: the hand gesture ID number located near the center of the HUSKYLENS 2 camera screen, the total number of hand gestures detected, and the ID number of the first detected hand gesture.
The example program is as follows.
/*!
* MindPlus
* uno
*
*/
#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_HAND_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_HAND_RECOGNITION);
if ((huskylens.available(ALGORITHM_HAND_RECOGNITION))) {
Serial.println((String("Gesture ID near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_HAND_RECOGNITION), Result, ID)))));
Serial.println((String("Total number of detected gestures: ") + String((huskylens.getCachedResultNum(ALGORITHM_HAND_RECOGNITION)))));
Serial.println((String("ID of the 1st detected gesture: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_HAND_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
After the program is uploaded, HUSKYLENS 2 enters Hand Recognition mode. Align its camera with the hand gesture on the screen to begin learning. For detailed learning procedures, refer to the HUSKYLENS 2 WIKI.
Open the Serial Monitor to observe the output results.
Running Result:: Output the total number of detected hand gestures (any detected gesture, including unlearned ones, will be counted, indicated by a bounding box). Output the ID number of the corresponding gesture as required: learned gestures are assigned IDs in learning order, while unlearned gestures have an ID of 0.
10.2 Obtain Relevant Data of Specified Gesture
After HUSKYLENS 2 recognizes a hand gesture, it can retrieve relevant data about the specified gesture in the image. For example, determine whether a learned gesture is present in the image, the name of the specified gesture, and the count of the same gesture detected in the image. When multiple identical gestures appear, you can specify to retrieve parameters of one of them, including name, X/Y coordinates, width, and height.
Example program:
/*!
* MindPlus
* uno
*
*/
#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_HAND_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_HAND_RECOGNITION);
if ((huskylens.available(ALGORITHM_HAND_RECOGNITION))) {
Serial.println((String("Gesture ID near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_HAND_RECOGNITION), Result, ID)))));
Serial.println((String("Total number of detected gestures: ") + String((huskylens.getCachedResultNum(ALGORITHM_HAND_RECOGNITION)))));
Serial.println((String("ID of the 1st detected gesture: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_HAND_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
Running Result:: As shown in the figure, the Running Result displays the number, name, and coordinates of the first detected ID1 gesture in the screen.
11. License Plate Recognition Code Example
11.1 Output Relevant Data When Recognizing a License Plate
In the License Plate Recognition function, when a license plate appears on the HUSKYLENS 2 screen, it can be recognized and framed. Obtain the total number of license plates in the HUSKYLENS 2 image, as well as data related to the license plate closest to the crosshair cursor, and print them over the serial port. The readable license plate data includes: License Plate ID, License Plate Name, License Plate Content (License Plate Number), Width, Height, and the X and Y coordinates of the license plate's center.
The sample 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_LICENSE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_LICENSE_RECOGNITION);
if ((huskylens.available(ALGORITHM_LICENSE_RECOGNITION))) {
Serial.println((String("Total number of license plates in the frame: ") + String((huskylens.getCachedResultNum(ALGORITHM_LICENSE_RECOGNITION)))));
Serial.println((String("ID of the license plate near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, ID)))));
Serial.println((String("License plate number near the center: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, content)))));
Serial.println((String("Center coordinates of the license plate near the center: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, yCenter)))))))));
}
delay(500);
}
Running Result:: As shown, for unlearned license plates, the default output ID is 0. For detailed learning operations, please refer to HUSKYLENS 2 WIKI.
11.2 Recognizing License Plates & Outputting Relevant Data
When multiple license plates appear in the frame, you can use the following sample program to count the number of specified ID license plates and obtain their relevant data.
The sample 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_LICENSE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_LICENSE_RECOGNITION);
if ((huskylens.available(ALGORITHM_LICENSE_RECOGNITION))) {
Serial.println((String("Total number of license plates in the frame: ") + String((huskylens.getCachedResultNum(ALGORITHM_LICENSE_RECOGNITION)))));
Serial.println((String("ID of the license plate near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, ID)))));
Serial.println((String("License plate number near the center: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, content)))));
Serial.println((String("Center coordinates of the license plate near the center: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, yCenter)))))))));
}
delay(500);
}
Running Result:: As shown, when multiple identical license plates appear in the frame, HUSKYLENS 2 can recognize all license plates with ID 1 (Note: A license plate must be learned first and assigned ID 1; unlearned license plates are all assigned ID 0). Through UART, you can observe and count the total number of ID-1 license plates in the current frame, and UART will also output the relevant data of the first ID-1 license plate.
12. Optical Character Recognition Code Example
12.1 Recognize Text and Output Relevant Data Near the Center
Under the Optical Character Recognition function, HUSKYLENS 2 can recognize and frame the area where text blocks appear in the field of view, and display the recognized text in the upper left corner. You can use the following sample program to count the total number of identifiable text areas in the screen, obtain the text block closest to the cross cursor in the screen, and print the relevant data through the serial port. The readable data includes: text block ID, name, content, center X and Y coordinates, text block width and height.
Sample program 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_OCR_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_OCR_RECOGNITION);
if ((huskylens.available(ALGORITHM_OCR_RECOGNITION))) {
Serial.println((String("Total number of text blocks in the frame: ") + String((huskylens.getCachedResultNum(ALGORITHM_OCR_RECOGNITION)))));
Serial.println((String("Text content near the center: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_OCR_RECOGNITION), Result, content)))));
Serial.println((String("ID of the 1st detected text block: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OCR_RECOGNITION), Result, ID)))));
Serial.println((String("Width of the 1st detected text block: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OCR_RECOGNITION), Result, width)))));
Serial.println((String("Height of the 1st detected text block: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OCR_RECOGNITION), Result, width)))));
}
delay(500);
}
Running Result: As follows, for text blocks that have been learned, ensure the output ID matches the ID displayed on the HUSKYLENS 2 screen.
Note: In Optical Character Recognition (OCR) mode, HUSKYLENS 2 can detect all text block areas in the image and mark them using bounding boxes. However, it only recognizes the content from the text block area closest to the cross cursor and displays it in the top-left corner of the bounding box.
13. Posture Recognition Code Example
13.1 Recognize Human Body and Output Relevant Data
HUSKYLENS 2 can recognize human bodies within its field of view and print the relevant data through the serial port. The readable data includes: the posture ID of the human body near the center of the HUSKYLENS 2 camera screen, name, width, height, and center X and Y coordinates, the total number of detected humans, and the relevant data of the first detected posture, etc.
Sample program 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_POSE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_POSE_RECOGNITION);
if ((huskylens.available(ALGORITHM_POSE_RECOGNITION))) {
Serial.println((String("Total number of detected human bodies: ") + String((huskylens.getCachedResultNum(ALGORITHM_POSE_RECOGNITION)))));
Serial.println((String("ID of the human body near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_POSE_RECOGNITION), Result, ID)))));
Serial.println((String("Name of the human body near the center: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_POSE_RECOGNITION), Result, name)))));
Serial.println((String("ID of the 1st detected human body: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_POSE_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
After the program is successfully uploaded, the HUSKYLENS 2 enters the Pose Recognition function. Align the camera of HUSKYLENS 2 with the human pose in the image for learning. For detailed steps on learning poses, please refer to: HUSKYLENS 2 WIKI
After learning is complete, open the Serial Monitor to observe the output results.
Running Result: The total number of detected humans will be output; the corresponding human pose ID will be output as required. The learned human poses will be assigned IDs in the order of learning, and unlearned ones will have an ID of 0.
13.2 Acquire Relevant Data of Specified Pose in Image
After HUSKYLENS 2 recognizes human poses, it can obtain relevant data of specified poses in the image. For example, it can determine whether a learned pose exists in the image, identify the name of the specified pose, and count the number of the same pose in the image. When multiple identical poses appear in the image, one can specify to obtain the parameters of one of them, including name, X/Y coordinates, width, and height.
Sample program 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_POSE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_POSE_RECOGNITION);
if ((huskylens.available(ALGORITHM_POSE_RECOGNITION))) {
Serial.println((String("Total number of detected human bodies: ") + String((huskylens.getCachedResultNum(ALGORITHM_POSE_RECOGNITION)))));
Serial.println((String("ID of the human body near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_POSE_RECOGNITION), Result, ID)))));
Serial.println((String("Name of the human body near the center: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_POSE_RECOGNITION), Result, name)))));
Serial.println((String("ID of the 1st detected human body: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_POSE_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
Running Result: As shown in the figure, you can retrieve the total number of poses, the count of ID1 poses, the names of the ID1 poses, and the coordinates of the first detected ID1 pose.
Line Tracking Code Example
14.1 Output Data for Intersection Recognition
In line tracking mode, HUSKYLENS 2 can mark the trajectory of the path in the image and obtain the current path's length, angle, and X/Y components. When the path branches, it can acquire the number of path junction branches and the corresponding data of each branch starting counterclockwise.
#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_LINE_TRACKING);
}
void loop() {
huskylens.getResult(ALGORITHM_LINE_TRACKING);
if ((huskylens.available(ALGORITHM_LINE_TRACKING))) {
Serial.println((String((String("X component of current path: ") + String((RET_ITEM_NUM(huskylens.getCurrentBranch(ALGORITHM_LINE_TRACKING), Result, xTarget))))) + String((String("; Y component: ") + String((RET_ITEM_NUM(huskylens.getCurrentBranch(ALGORITHM_LINE_TRACKING), Result, yTarget)))))));
Serial.println((String((String("Angle of current path: ") + String((RET_ITEM_NUM(huskylens.getCurrentBranch(ALGORITHM_LINE_TRACKING), Result, angle))))) + String((String("; Length: ") + String((RET_ITEM_NUM(huskylens.getCurrentBranch(ALGORITHM_LINE_TRACKING), Result, length)))))));
Serial.println((String("Number of upcoming junction branches: ") + String((huskylens.getUpcomingBranchCount(ALGORITHM_LINE_TRACKING)))));
Serial.println((String((String("X component of the 1st branch in counterclockwise direction") + String((RET_ITEM_NUM(huskylens.getBranch(ALGORITHM_LINE_TRACKING, 1-1), Result, xTarget))))) + String((String("; Y component") + String((RET_ITEM_NUM(huskylens.getBranch(ALGORITHM_LINE_TRACKING, 1-1), Result, yTarget)))))));
}
delay(500);
}
Running Result: As shown in the figure, align the HUSKYLENS camera with the map containing routes, and observe that the Serial Monitor can output data such as the current route length and angle. When the route consists of multiple branches, it can also output the number of branches.
15. Face Emotion Recognition Code Example
15.1 Recognize All Emotions in the Camera Frame
Under the Face Emotion Recognition function, HUSKYLENS 2 can recognize 7 specific emotions: Angry (ID 1), Disgust (ID 2), Fear (ID 3), Happy (ID 4), Neutral (ID 5), Sad (ID 6), and Surprised (ID 7). These emotions are pre-trained on HUSKYLENS 2 at the factory and do not require manual learning by users. For detailed functional instructions on face emotion recognition, please refer to HUSKYLENS 2 WIKI.
The following example code can count the number of each emotion recognized in the current HUSKYLENS 2 camera frame and output the relevant data for each emotion in sequence.
Example code as follows
#include "DFRobot_HuskylensV2.h"
// Dynamic variables
volatile float mind_n_EmotionTotal, mind_n_IndexVariable;
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
mind_n_EmotionTotal = 0;
mind_n_IndexVariable = 0;
}
void loop() {
huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);
if ((huskylens.available(ALGORITHM_EMOTION_RECOGNITION))) {
mind_n_EmotionTotal = (huskylens.getCachedResultNum(ALGORITHM_EMOTION_RECOGNITION));
Serial.println((String("Total number of emotions in the frame: ") + String(mind_n_EmotionTotal)));
while (!(mind_n_IndexVariable >= mind_n_EmotionTotal)) {
mind_n_IndexVariable += 1;
Serial.println((String((String("The ") + String((String(mind_n_IndexVariable) + String("th emotion is: "))))) + String((RET_ITEM_STR(huskylens.getCachedResultByIndex(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable - 1), Result, name)))));
}
mind_n_IndexVariable = 0;
}
delay(500);
}
When any of the above seven expressions appear in the camera view, the HUSKYLENS 2 screen will frame the expression and display its name and confidence score.
Running Result: Observe the serial port output for the total number of recognized expressions and their corresponding names.
15.2 Counting the Number of Each Emotion Type
Given that the face emotion recognition function outputs IDs in the range 1-7 (corresponding to 7 emotions), we can use the following example program to count the number of each emotion type recognized in the current HUSKYLENS 2 camera view.
#include "DFRobot_HuskylensV2.h"
// Dynamic variables
volatile float mind_n_IndexVariable;
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
huskylens.switchAlgorithm(ALGORITHM_EMOTION_RECOGNITION);
mind_n_IndexVariable = 1;
}
void loop() {
huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);
if ((huskylens.available(ALGORITHM_EMOTION_RECOGNITION))) {
if ((mind_n_IndexVariable <= 7)) {
if (((huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable) != NULL))) {
Serial.println((String((String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable), Result, name))) + String(" total count: "))) + String((huskylens.getCachedResultNumByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable)))));
}
mind_n_IndexVariable += 1;
}
else {
mind_n_IndexVariable = 1;
}
}
delay(500);
}
Running Result: Run the program, and when multiple faces with different expressions appear in the image, observe the UART output to see the name and corresponding count of each emotion category currently recognized.
16. Tag Recognition Blocks
16.1 Output Related Data of Recognized Tags
Under the Tag Recognition function, HUSKYLENS 2 can recognize AprilTag labels present in the image. By programming, you can count the total number of tags, obtain the relevant data of the tag closest to the cross cursor in the image, and print it via the serial port. Readable tag data includes: tag ID, tag content, tag width, tag height, as well as the X and Y coordinates of the tag center. When multiple tags are present in the image, you can retrieve data of specified tags.
For detailed operation on how to use the Tag Recognition function, please refer to: HUSKYLENS 2.
The Example Program:
#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_TAG_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_TAG_RECOGNITION);
if ((huskylens.available(ALGORITHM_TAG_RECOGNITION))) {
Serial.println((String("Total number of tags: ") + String((huskylens.getCachedResultNum(ALGORITHM_TAG_RECOGNITION)))));
Serial.println((String("ID of the tag near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION), Result, ID)))));
Serial.println((String("Center coordinates of the tag near the center: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION), Result, yCenter)))))));
Serial.println((String("ID of the first detected tag: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_TAG_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
When a tag is recognized, the HUSKYLENS 2 screen will frame the tag and display the parsed tag content in the top-left corner of the box. Open the Serial Monitor to observe the output data.
Running Result: As follows, unlearned tags default to ID 0. The tag content output via the serial port remains consistent with what is displayed on the HUSKYLENS 2 screen.
The following, for the learned tags, the tag ID and content from their serial port output must match the display on the HUSKYLENS 2 screen.
16.2 Output Data for a Specified Tag ID
When multiple tags with the same ID appear in the image (learned tag IDs are assigned in the order of learning, and unlearned ones have a unified ID of 0), use the following example program to count the number of tags with the same ID and obtain the relevant data for a specified ID tag.
Example Program
#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_TAG_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_TAG_RECOGNITION);
if ((huskylens.available(ALGORITHM_TAG_RECOGNITION))) {
if (((huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1) != NULL))) {
Serial.println((String("Total number of ID1 tags: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_TAG_RECOGNITION, 1)))));
Serial.println((String("Content of ID1 tag: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1), Result, content)))));
Serial.println((String("Center coordinates of the first ID1 tag: ") + String((String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1), Result, yCenter)))))))));
}
}
delay(500);
}
When multiple instances of the same tag are detected in the image, HUSKYLENS 2 can identify all tags with ID 1 (provided a tag has been learned and assigned ID 1). The serial port can monitor and count the total number of ID 1 tags in the current image, and simultaneously output data from the first detected ID 1 tag.
17. QR Code Recognition Module
17.1 Output Relevant Data for QR Code Recognition
With the QR code recognition feature enabled, HUSKYLENS 2 can identify QR codes within the captured image. By programming, you can count the total number of detected QR codes, retrieve data from the QR code closest to the crosshair in the image, and print this information via the serial port. The retrievable data includes: QR code ID, QR code name, decoded content, width, height, and the X/Y coordinate positions of its center point.**
The following is an example program
#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_QRCODE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_QRCODE_RECOGNITION);
if ((huskylens.available(ALGORITHM_QRCODE_RECOGNITION))) {
Serial.println((String("Total number of QR codes: ") + String((huskylens.getCachedResultNum(ALGORITHM_QRCODE_RECOGNITION)))));
Serial.println((String("ID of the QR code near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_QRCODE_RECOGNITION), Result, ID)))));
Serial.println((String("Center coordinates of the QR code near the center: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_QRCODE_RECOGNITION), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_QRCODE_RECOGNITION), Result, yCenter)))))));
Serial.println((String("ID of the first detected QR code: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_QRCODE_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
When a QR code is recognized, the HUSKYLENS 2 screen will frame it and display the parsed QR code content in the top-left corner of the box. Open the Serial Monitor to check the output data.
Running Result: Results are as follows: Unlearned QR codes will default to ID 0. The serial output of the QR code content remains consistent with the screen display on HUSKYLENS 2. For learned QR codes, the serial port outputs both the tag ID and tag content, which are consistent with the screen display on HUSKYLENS 2.
17.2 Output Relevant Data of a Specified QR Code
When multiple QR codes with the same ID appear in the image (learned tags are assigned IDs in the order of learning; unlearned tags are uniformly assigned ID 0), you can use the following example program to count the number of QR codes with the same ID in the image and obtain data from a specified QR code with that ID.
For details on how to use the QR code recognition function, please refer to: HUSKYLENS 2
The example program
#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_QRCODE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_QRCODE_RECOGNITION);
if ((huskylens.available(ALGORITHM_QRCODE_RECOGNITION))) {
if (((huskylens.getCachedResultByID(ALGORITHM_QRCODE_RECOGNITION, 1) != NULL))) {
Serial.println((String("Total number of ID1 QR codes: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_QRCODE_RECOGNITION, 1)))));
Serial.println((String("Content of ID1 QR code: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_QRCODE_RECOGNITION, 1), Result, content)))));
Serial.println((String("Center coordinates of the first ID1 QR code: ") + String((String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_QRCODE_RECOGNITION, 1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_QRCODE_RECOGNITION, 1), Result, yCenter)))))))));
}
}
delay(500);
}
Running Result: When multiple QR codes appear in the image, HUSKYLENS 2 can recognize all QR codes (both unlearned and learned). The serial port will display the total count of QR codes in the current frame, and simultaneously print the relevant data of the specified QR code with ID 1.
18.Barcode Recognition Code Example
18.1Identify Barcode and Output Relevant Data
Under the barcode recognition feature, HUSKYLENS 2 can detect barcodes present in the image. Through programming, you can count the total number of detected barcodes, retrieve data for the tag closest to the crosshair, and print this information via the serial port. The readable tag data includes: barcode ID, barcode content, barcode width, barcode height, and the X and Y coordinate positions of its center point.
The example program
#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_BARCODE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_BARCODE_RECOGNITION);
if ((huskylens.available(ALGORITHM_BARCODE_RECOGNITION))) {
Serial.println((String("Total number of barcodes: ") + String((huskylens.getCachedResultNum(ALGORITHM_BARCODE_RECOGNITION)))));
Serial.println((String("ID of the barcode near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION), Result, ID)))));
Serial.println((String("Center coordinates of the barcode near the center: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION), Result, yCenter)))))));
Serial.println((String("ID of the first detected barcode: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_BARCODE_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
When a barcode is recognized, the HUSKYLENS 2 screen will frame the tag and display the parsed barcode content in the top-left corner of the box. Open the serial port in Mind+ to observe the serial port output of the relevant data from the barcode closest to the crosshair.
Running Result: Unlearned barcodes, as shown, will have a default ID of 0, and the barcode content output via the serial port will be consistent with that displayed on the HUSKYLENS 2 screen.
18.2 Identify Barcode and Output Relevant Data
When multiple barcodes with the same ID appear in the image (learned tags are assigned IDs in the order of learning, while unlearned tags are uniformly assigned ID 0), you can use the following example program to count the number of barcodes with the same ID in the image and obtain relevant data of a specified barcode with that ID.
Here is the example program.
#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_BARCODE_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_BARCODE_RECOGNITION);
if ((huskylens.available(ALGORITHM_BARCODE_RECOGNITION))) {
if (((huskylens.getCachedResultByID(ALGORITHM_BARCODE_RECOGNITION, 1) != NULL))) {
Serial.println((String("Total number of ID1 barcodes: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_BARCODE_RECOGNITION, 1)))));
Serial.println((String("Content of ID1 barcode: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_BARCODE_RECOGNITION, 1), Result, content)))));
Serial.println((String("Center coordinates of the first ID1 barcode: ") + String((String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 1, 1-1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 1, 1-1), Result, yCenter)))))))));
}
}
delay(500);
}
Running Result: As follows, when multiple barcodes appear in the image, HUSKYLENS 2 can recognize all barcodes (both unlearned and learned), and the total number of barcodes in the current image can be observed through the serial port. At the same time, the serial port output will print the relevant data of the barcode with the specified ID 1.