Optical Character Recognition Example
OCR on HuskyLens 2 reads text blocks via Arduino code, then shows their ID, content, position and size on a UNIHIKER K10 screen. It’s like a camera that turns printed words into structured data. The sketch initializes I2C, sets HuskyLens to OCR mode, polls results, counts all detected text blocks, and focuses on the block nearest the crosshair, caching its center coordinates and dimensions for display. Untrained text areas return ID 0, while learned text uses auto-recognition so the on-screen ID matches the trained label. HuskyLens draws boxes around every text region but only decodes and prints the one under the crosshair in the upper-left of its bounding box.
1. Optical Character Recognition Example
Before You Start:
1.1 Recognize Text and Output Relevant Data
Under the Optical Character Recognition (OCR) function, HuskyLens 2 can recognize and frame the regions where text blocks appear in the field of view, and display the recognized text on the screen. Use the following sample program to count the total number of recognizable text blocks in the frame, and obtain the relevant data of the text block closest to the crosshair. The readable data includes: text block ID, name, content, center X and Y coordinates, and text block width and height.
The example program is as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: OCR 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_OCR_RECOGNITION); // Set to OCR text recognition mode
}
void loop() {
huskylens.getResult(ALGORITHM_OCR_RECOGNITION); // Get latest OCR recognition data
if (huskylens.available(ALGORITHM_OCR_RECOGNITION)) {
// Store center text block data to simplify repeated calls
auto centerText = huskylens.getCachedCenterResult(ALGORITHM_OCR_RECOGNITION);
// Display OCR info (blue color: 0x0000FF)
k10.canvas->canvasText(String("Text block count: ") + String(huskylens.getCachedResultNum(ALGORITHM_OCR_RECOGNITION)), 1, 0x0000FF);
k10.canvas->canvasText(String("Center text ID: ") + String(RET_ITEM_NUM(centerText, Result, ID)), 3, 0x0000FF);
k10.canvas->canvasText("Center text content:", 5, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_STR(centerText, Result, content)), 6, 0x0000FF);
k10.canvas->canvasText("Center text block", 8, 0x0000FF);
k10.canvas->canvasText(String("Center coords: ") , 9, 0x0000FF); // 修复参数:坐标用xCenter/yCenter
k10.canvas->canvasText(String(RET_ITEM_NUM(centerText, Result, xCenter)) + String(",") + String(RET_ITEM_NUM(centerText, Result, yCenter)), 10, 0x0000FF); // 修复参数:坐标用xCenter/yCenter
k10.canvas->canvasText("Center text block", 11, 0x0000FF);
k10.canvas->canvasText(String("Size (w,h): ") + String(RET_ITEM_NUM(centerText, Result, width)) + String(",") + String(RET_ITEM_NUM(centerText, Result, height)), 12, 0x0000FF); // 修复参数:宽高用width/height
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data reading
}
Upload the program and wait for completion.
Align the camera of HUSKYLENS 2 with any optical character and observe the result on the K10 screen.
Running Result:As follows, text blocks that have not been trained will output ID 0 by default.
To align with a learned text block, use the auto-recognition function. For details on how to learn optical characters, please refer to: Optical Character Recognition - Learning Text:Optical Character Recognition.
Operation Result: As follows, the output ID for the learned text block matches the ID displayed on the HUSKYLENS 2 screen.
Note: In the Optical Character Recognition function, HUSKYLENS 2 can detect all text block areas in the image and frame them with squares. However, it only recognizes the content of the text block area where the crosshair is positioned and displays the text content in the top-left corner of the square.
Was this article helpful?
