License Plate Recognition Example
License plate recognition reads plate count and nearest ID; it’s like a smart camera that labels every plate in view and streams ID, text and position via UART for learned IDs
1. License Plate Recognition Code Example
Before You Start:
1.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 LPR Function Description.
1.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.
Was this article helpful?
