Công nghệ nhận diện ký tự quang học (OCR) cho phép trích xuất văn bản từ hình ảnh hoặc tài liệu số hóa như PDF, ảnh chụp hay bản scan. Đây là nền tảng thiết yếu trong các hệ thống tự động hóa văn phòng, quản lý dữ liệu và ứng dụng trí tuệ nhân tạo.
Thiết kế giao diện DLL cho OCR
Để dễ dàng tích hợp vào nhiều ngôn ngữ lập trình, chức năng OCR được đóng gói dưới dạng thư viện liên kết động (DLL). Thư viện hỗ trợ:
- C++
- Python
- Yi Language (易语言)
Giao diện C++
#ifndef __TEXT_RECOGNIZER_H__
#define __TEXT_RECOGNIZER_H__
#include <windows.h>
typedef struct RecognitionStatus {
int error_code;
char description[4096];
} RecognitionStatus;
int WINAPI InitializeRecognizer(const char* model_path, RecognitionStatus* status);
HANDLE WINAPI CreateRecognizerHandle(const char* license_key, const char* model_path, RecognitionStatus* status);
char* WINAPI GetLicenseExpiry(HANDLE handle, RecognitionStatus* status);
char* WINAPI RecognizeFromImagePath(HANDLE handle, const char* image_path, RecognitionStatus* status);
int WINAPI ReleaseRecognizerHandle(HANDLE handle);
#endif
Tích hợp với Python
import ctypes
from ctypes import Structure, c_char_p, POINTER
class RecognitionStatus(Structure):
_fields_ = [
("error_code", ctypes.c_int),
("description", c_char_p * 4096)
]
# Tải thư viện
ocr_lib = ctypes.CDLL('D://TextRecognizer.dll')
# Khai báo kiểu tham số
ocr_lib.InitializeRecognizer.argtypes = [c_char_p, POINTER(RecognitionStatus)]
ocr_lib.CreateRecognizerHandle.argtypes = [c_char_p, c_char_p, POINTER(RecognitionStatus)]
ocr_lib.RecognizeFromImagePath.restype = c_char_p
# Khởi tạo
status = RecognitionStatus()
license = b"SNKJe9xffLhdFY7r3TcffXq44ThDVcE3BQFQFfVA9VG4"
model_file = b"D://TextModel.onnx"
image_file = b"D://sample.jpg"
if ocr_lib.InitializeRecognizer(model_file, ctypes.byref(status)) != 0:
print("Khởi tạo thất bại:", status.description[0].decode('utf-8'))
exit()
handle = ocr_lib.CreateRecognizerHandle(license, model_file, ctypes.byref(status))
if not handle:
print("Tạo handle thất bại:", status.description[0].decode('utf-8'))
exit()
# Nhận diện
result_json = ocr_lib.RecognizeFromImagePath(handle, image_file, ctypes.byref(status))
if result_json:
try:
print("Kết quả:", result_json.decode('utf-8'))
except UnicodeDecodeError:
print("Kết quả:", result_json.decode('gbk'))
else:
print("Lỗi nhận diện:", status.description[0].decode('utf-8'))
ocr_lib.ReleaseRecognizerHandle(handle)
input("Nhấn Enter để thoát...")
Kết quả nhận diện mẫu
Với hình ảnh thẻ ngân hàng:
{
"type": 0,
"ocr_result": {
"text_blocks": [
{
"confidence": 0.939,
"bbox": [102.2, 41.8, 329.8, 67.8],
"text": "中国建设银行"
},
{
"confidence": 0.966,
"bbox": [104.4, 68.4, 309.9, 84.6],
"text": "China Construction Bank"
}
]
}
}
Với hình ảnh danh thiếp doanh nghiệp:
{
"ocr_result": {
"text_blocks": [
{
"confidence": 0.996,
"bbox": [40.6, 324.3, 541.5, 371.8],
"text": "广州利驰服装有限公司"
},
{
"confidence": 0.991,
"bbox": [624.0, 351.5, 925.0, 374.9],
"text": "热线:400-688-7260"
}
]
}
}
Hỗ trợ đa luồng và cập nhật gần đây
Thư viện hỗ trợ xử lý song song qua đa luồng. Các cải tiến nổi bật gần đây bao gồm:
- Tối ưu thời gian xác thực giấy phép
- Giảm độ trễ nhận diện văn bản
- Sửa lỗi đường dẫn có ký tự tiếng Trung
- Hỗ trợ tọa độ tương đối trong mô phỏng di chuyển chuột