License Plate Recognition Example
License plate recognition on HUSKYLENS 2 reads plate IDs, text, size and center coordinates and shows them on the UNIHIKER K10 display, like a smart camera overlaying live labels on each car plate so you can debug results in real time
1. License Plate Recognition Example
Before You Start:
1.1 Recognize License Plates and Output Relevant Data
When License Recognition is enabled, a license plate appearing on the HUSKYLENS 2 screen will be recognized, framed with a bounding box, and its relevant data will be output. Readable license plate data includes: specified license plate ID, name, content (license plate number), width, height, X/Y coordinate positions of the center point, and the total number of license plates in the image.
Example program follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: license recognition; 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_LICENSE_RECOGNITION); // Set to license recognition mode
}
void loop() {
huskylens.getResult(ALGORITHM_LICENSE_RECOGNITION); // Get latest license recognition data
if (huskylens.available(ALGORITHM_LICENSE_RECOGNITION)) {
// Simplify index: 1-1 → 0 (more intuitive, same meaning)
k10.canvas->canvasText(String("Center license ID: ") + String(RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_LICENSE_RECOGNITION), Result, ID)), 1, 0x0000FF);
k10.canvas->canvasText(String("Total licenses: ") + String(huskylens.getCachedResultNum(ALGORITHM_LICENSE_RECOGNITION)), 3, 0x0000FF);
k10.canvas->canvasText(String("First license ID: ") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_LICENSE_RECOGNITION, 0), Result, ID)), 5, 0x0000FF);
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data reading
}
Upload the program and wait for the upload to complete.
To learn license plate recognition, aim the HuskyLens 2 camera at the license plate in the camera view. For detailed operation on learning the license plate, please refer to LPR Function Description.
Aim the HUSKYLENS 2 camera at the license plate and observe the data on the UNIHIKER K10 screen.
Running result:As follows, the total number of license plates includes both learned and unlearned ones. If the specified license plate is an unlearned one, the ID number is 0; if it is a learned one, it outputs its corresponding ID number.
1.2 Output Data of License Plates with Specified ID
When multiple license plates with the same ID appear in the frame, you can use the sample program below to count the relevant data of license plates with that specific ID.
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: license recognition; 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_LICENSE_RECOGNITION); // Set to license recognition mode
}
void loop() {
huskylens.getResult(ALGORITHM_LICENSE_RECOGNITION); // Get latest license recognition data
if (huskylens.available(ALGORITHM_LICENSE_RECOGNITION)) {
// Check if ID1 license exists and display its info
if (huskylens.getCachedResultByID(ALGORITHM_LICENSE_RECOGNITION, 1) != NULL) {
// Simplify index: 1-1 → 0 (more intuitive, same meaning)
k10.canvas->canvasText(String("ID1 license count: ") + String(huskylens.getCachedResultNumByID(ALGORITHM_LICENSE_RECOGNITION, 1)), 1, 0x0000FF);
k10.canvas->canvasText("First ID1 license", 3, 0x0000FF);
k10.canvas->canvasText(String("number: ") + String(RET_ITEM_STR(huskylens.getCachedIndexResultByID(ALGORITHM_LICENSE_RECOGNITION, 1, 0), Result, content)), 4, 0x0000FF);
k10.canvas->canvasText("First ID1 license", 6, 0x0000FF);
k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_LICENSE_RECOGNITION, 1, 0), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_LICENSE_RECOGNITION, 1, 0), Result, yCenter)), 7, 0x0000FF);
}
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data reading
}
Running Result:As follows, when multiple license plates with the specified ID appear in the image, the total count of such license plates, the license plate numbers of the specified ones under this ID, and their coordinates can be obtained.
Was this article helpful?
