OCR之find_text_bottle_label.hdev
* 这个例子中用到find_text去分割文字,最后用OCR算子去识别
*This example uses the operator find_text to segment
* text on a bottle label.
* First, a text model is created and some parameters
* are set. Before the actual segmentation can be
* performed, the image has to be aligned horizontally,
* which is done by the operators text_line_orientation
* and rotate_image. Then, find_text is called and OCR
* is performed on the results.
*
* Initialization
*关闭更新
dev_update_off ()
*读取图像
read_image (Image, 'ocr/bottle_label_01')
*关闭窗体
dev_close_window ()
*打开适合图像尺寸大小的窗体
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
*设置显示字体
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
*读取CNN分类器句柄
read_ocr_class_cnn ('Universal_Rej.occ', OCRHandle)
* 创建一个文字读取器模型
* Create text model and specify parameter settings
create_text_model_reader ('manual', [], TextModel)
* 设置一些限定文字特征的参数,字符宽高,文字描边厚度,是否是点打印,是否返回分隔符,有几行文字,是否消除下划线
*以下这些参数设置好以后,就可以排除
set_text_model_param (TextModel, 'manual_char_width', 18)
set_text_model_param (TextModel, 'manual_char_height', 40)
set_text_model_param (TextModel, 'manual_stroke_width', 3)
set_text_model_param (TextModel, 'manual_is_dotprint', 'true')
set_text_model_param (TextModel, 'manual_return_separators', 'true')
set_text_model_param (TextModel, 'manual_max_line_num', 1)
set_text_model_param (TextModel, 'manual_uppercase_only', 'true')
set_text_model_param (TextModel, 'manual_eliminate_horizontal_lines', 'true')
*‘/’有时候被认为是字符,有时候被认为是分隔符,所以这里定义两种文字结构
* The character '/' may be classified as a character or as a
* separator. Therefore, we specify two different text line
* structures to ensure a robust detection of the text line.
*定义两种文字结构分别为:5 11两段组成(此时'/'被当作字符,中间只有一个空白分开);2 2 11 三段组成(此时'/'被当作分隔符
*那么中间就有'/'和空白把文字分为三段
set_text_model_param (TextModel, 'manual_text_line_structure_0', '5 11')
set_text_model_param (TextModel, 'manual_text_line_structure_1', '2 2 11')
* 图像处理,总共有八张图片
* Process images
NumImages := 8
for ImageIndex := 1 to NumImages by 1
*读取每张图片中的文字
* Read the text in every image
*读取图片
read_image (ImageIn, 'ocr/bottle_label_' + ImageIndex$'02')
* find_text这个算子在识别文字的时候,必须把文字矫正水平所以首先得先矫正图像
* Because find_text only works for horizontally aligned
* text lines, the orientation of the input image has to
* be corrected first
*获取图像信息
get_image_time (ImageIn, MSecond, Second, Minute, Hour, Day, YDay, Month, Year)
*本地函数,矫正目标文字到水平位置
correct_text_orientation (ImageIn, Image, TextModel, Phi)
*查询文字
find_text (Image, TextModel, TextResult)
*
* Perform OCR for all segmented text lines and
* display the results in a loop
*获取识别道德文字行数
get_text_result (TextResult, 'manual_num_lines', NumLines)
dev_display (Image)
for LineIndex := 0 to NumLines - 1 by 1
*获取识别的文字区域
get_text_object (Line, TextResult, ['manual_line',LineIndex])
*执行CNN文字识别
do_ocr_multi_class_cnn (Line, Image, OCRHandle, Class, Confidence)
* 获取一行文字个数
* Display results
count_obj (Line, NumberOfCharacters)
*设置填充方式
dev_set_draw ('margin')
*求每一个文字的最小外接矩形
smallest_rectangle1 (Line, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
dev_display (Rectangle)
*设置显示颜色为6种颜色
dev_set_colored (6)
*显示文字
dev_display (Line)
*设置颜色为深绿色
dev_set_color ('dark green')
*在每一个文字区域下方显示对应的识别到的字符
for CharacterIndex := 1 to NumberOfCharacters by 1
select_obj (Line, Character, CharacterIndex)
set_tposition (WindowHandle, Row2[0] + 10, Column1[CharacterIndex - 1])
write_string (WindowHandle, Class[CharacterIndex - 1])
endfor
endfor
*如果图片没是识别完,就继续识别
if (ImageIndex < NumImages)
*右下角显示'Press F5 to continue'
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
* Clean up memory
*清除读取结果
clear_text_result (TextResult)
endfor
* Clean up memory
*清除文字模型
clear_text_model (TextModel)
*清除CNN分类器句柄,释放内存
clear_ocr_class_cnn (OCRHandle)

浙公网安备 33010602011771号