Optical Character Recognition Example
Use OCR mode to detect text blocks, then read and print the block closest to the center. It’s like a smart camera that highlights and reports the most central label. The sample Arduino code initializes HUSKYLENS 2, switches to ALGORITHM_OCR_RECOGNITION, counts all detected text areas, and via serial prints the nearest block’s ID, content, center coordinates, width and height, helping users quickly debug and align learned text IDs with what appears on screen.
1. Optical Character Recognition Code Example
Before You Start:
1.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.For detailed operations, please refer to Optical Character Recognition.
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.
Was this article helpful?
