一.前言:
目前新版的Halcon已经具备了DeepOcr的功能可以涵盖大部分的识别场景,缺点是有些特殊的应用场景依然需要大量的图片训练,而且Halcon22之前的版本DeepOCR是不支持训练的,我们都知道传统的OCR项目是通过Blob分析,把需要检测的字符位置在图像中割出来。然后利用MLP,SVM,GMM等等机器学习的方式训练OCR识别模型。其实对于标准印刷字体还有点阵字体,工业打印字体等等,Halcon预训练的现成的模型,可以拿来直接做字符识别,用起来也非常的方便。他的好处是应用速度很快,并且不需要做Blob分析图像分割,可以直接传图进行读取。目前Halcon主要是针对多层干感知机MLP和卷积神经网络CNN训练的一些模型。
例如可以利用算子read_ocr_class_mlp (Operator)读取MLP模型。MLP的模型名称如下:
Suggested values: ‘Document_0-9_NoRej.omc’, ‘Document_0-9_Rej.omc’, ‘Document_0-9A-Z_NoRej.omc’, ‘Document_0-9A-Z_Rej.omc’, ‘Document_A-Z+_NoRej.omc’, ‘Document_A-Z+_Rej.omc’, ‘Document_NoRej.omc’, ‘Document_Rej.omc’, ‘DotPrint_0-9_NoRej.omc’, ‘DotPrint_0-9_Rej.omc’, ‘DotPrint_0-9+_NoRej.omc’, ‘DotPrint_0-9+_Rej.omc’, ‘DotPrint_0-9A-Z_NoRej.omc’, ‘DotPrint_0-9A-Z_Rej.omc’, ‘DotPrint_A-Z+_NoRej.omc’, ‘DotPrint_A-Z+_Rej.omc’, ‘DotPrint_NoRej.omc’, ‘DotPrint_Rej.omc’, ‘HandWritten_0-9_NoRej.omc’, ‘HandWritten_0-9_Rej.omc’, ‘Industrial_0-9_NoRej.omc’, ‘Industrial_0-9_Rej.omc’, ‘Industrial_0-9+_NoRej.omc’, ‘Industrial_0-9+_Rej.omc’, ‘Industrial_0-9A-Z_NoRej.omc’, ‘Industrial_0-9A-Z_Rej.omc’, ‘Industrial_A-Z+_NoRej.omc’, ‘Industrial_A-Z+_Rej.omc’, ‘Industrial_NoRej.omc’, ‘Industrial_Rej.omc’, ‘OCRA_0-9_NoRej.omc’, ‘OCRA_0-9_Rej.omc’, ‘OCRA_0-9A-Z_NoRej.omc’, ‘OCRA_0-9A-Z_Rej.omc’, ‘OCRA_A-Z+_NoRej.omc’, ‘OCRA_A-Z+_Rej.omc’, ‘OCRA_NoRej.omc’, ‘OCRA_Rej.omc’, ‘OCRB_0-9_NoRej.omc’, ‘OCRB_0-9_Rej.omc’, ‘OCRB_0-9A-Z_NoRej.omc’, ‘OCRB_0-9A-Z_Rej.omc’, ‘OCRB_A-Z+_NoRej.omc’, ‘OCRB_A-Z+_Rej.omc’, ‘OCRB_NoRej.omc’, ‘OCRB_passport_NoRej.omc’, ‘OCRB_passport_Rej.omc’, ‘OCRB_Rej.omc’, ‘Pharma_0-9_NoRej.omc’, ‘Pharma_0-9_Rej.omc’, ‘Pharma_0-9+_NoRej.omc’, ‘Pharma_0-9+_Rej.omc’, ‘Pharma_0-9A-Z_NoRej.omc’, ‘Pharma_0-9A-Z_Rej.omc’, ‘Pharma_NoRej.omc’, ‘Pharma_Rej.omc’, ‘SEMI_NoRej.omc’, ‘SEMI_Rej.omc’
同时可以利用算子read_ocr_class_cnn (Operator)读取CNN模型,CNN的模型名称如下:
‘Universal_NoRej.occ’, ‘Universal_Rej.occ’, ‘Universal_0-9_NoRej.occ’, ‘Universal_0-9_Rej.occ’, ‘Universal_0-9+_NoRej.occ’, ‘Universal_0-9+_Rej.occ’, ‘Universal_0-9A-Z_NoRej.occ’, ‘Universal_0-9A-Z_Rej.occ’, ‘Universal_0-9A-Z+_NoRej.occ’, ‘Universal_0-9A-Z+_Rej.occ’, ‘Universal_A-Z+_NoRej.occ’, ‘Universal_A-Z+_Rej.occ’
模型读取完成后,利用算子create_text_model_reader (Operator),把刚才读取的模型传递给该算子,可以输出一个字符识别的句柄。然后利用该句柄和算子find_text (Operator)就可以做字符识别了。
二、上干货,OCR识别源码解析:
dev_update_off ()
- Acquire the image
read_image (Image, ‘numbers_scale’)
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, ‘black’, WindowID)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_set_line_width (2)
dev_set_color (‘yellow’)
dev_set_draw (‘margin’)
dev_display (Image)
set_display_font (WindowID, 12, ‘mono’, ‘true’, ‘false’)
stop ()
*读取预训练模型
read_ocr_class_mlp (‘Document_0-9_NoRej.omc’, OCRHandle)
-
根据读取到的模型句柄创建字符识别模型
create_text_model_reader (‘auto’, OCRHandle, TextModel) -
自动分割图像和OCR识别
find_text (Image, TextModel, TextResultID) -
获取识别到的字符区域
get_text_object (Characters, TextResultID, ‘all_lines’)
dev_display (Image)
dev_display (Characters)
stop () -
获取识别到的字符
get_text_result (TextResultID, ‘class’, Classes)
count_obj (Characters, Number)
for Index := 1 to Number by 1
dev_set_color (‘yellow’)
select_obj (Characters, SingleChar, Index)
dev_set_color (‘white’)
Class := Classes[Index - 1]
smallest_rectangle1 (SingleChar, Row1, Column1, Row2, Column2)
set_tposition (WindowID, Row1 - 17, (Column2 + Column1) * 0.5 - 5)
write_string (WindowID, Class[0])
endfor
stop () -
Free memory
clear_text_result (TextResultID)
clear_text_model (TextModel)