1 *
2 * 这个例子展示了深度学习在 OCR(文字检测)的用法:
3 * -第1部分:检测和识别图像中的单词。
4 * -第2部分:只识别单词。
5 * -第3部分:仅检测单词。
6 *重置窗口
7 dev_update_off ()
8 dev_close_window ()
9 *
10 *设置图像路径。
11 *获取HALCON系统参数的当前值。(所需的系统参数( 默认'init_new_image'),系统参数的当前值。)
12 PathExample := 'C:\\Users\\Public\\Documents\\MVTec\\HALCON-20.11-Steady\\examples'
13 get_system ('example_dir', PathExample)
14 PathExample := PathExample + '/images/ocr/'
15 ImagesPath := PathExample + ['airplane_ticket_01.png','blister_package_01.png','bottle_label_09.png','chip_01.png','dot_print_12.png','industrial_text_01.png','industrial_text_02.png','keys_01.png','metal_print_01.png','street_sign_01.png']
16 *
17 * *************************************************************
18 *第一部分:检测和识别图像中的单词。
19 * *************************************************************
20 *创建模型。
21 *创建一个深度字符检测模型(属性名、可为‘mode’,属性值(默认[],可为只识别、只检测、识别并检测),模型句柄)
22 create_deep_ocr ([], [], DeepOcrHandle)
23 *在OCR句柄中设置合适的设备
24 set_suitable_device_in_ocr_handle (DeepOcrHandle)
25 *
26 * 返回模型已经被训练的字符列表。
27 *返回模型的参数(输入:模型句柄,输入:识别字母,输出参数值:认识字母)
28 get_deep_ocr_param (DeepOcrHandle, 'recognition_alphabet', RecognitionAlphabet)
29 *打开显示字符识别的新窗口
30 dev_open_window (0, 0, 420, 320, 'black', WindowHandleAlphabet)
31 *显示识别字母
32 display_recognition_alphabet (RecognitionAlphabet, WindowHandleAlphabet)
33 stop ()
34 dev_close_window ()
35 * 返回模型的参数(输入:模型句柄,输入:检测图像的宽度,输出参数值:图像的宽度(模型宽度))
36 get_deep_ocr_param (DeepOcrHandle, 'detection_image_width', DetectionImageWidth)
37 * 返回模型的参数(输入:模型句柄,输入:检测图像的高度,输出参数值:图像的高度(模型高度))
38 get_deep_ocr_param (DeepOcrHandle, 'detection_image_height', DetectionImageHeight)
39 *为了更美观的视觉效果,预处理后的图像和分数以较小的尺寸显示。
40 DisplayWidth := 0.25 * DetectionImageWidth
41 DisplayHeight := 0.25 * DetectionImageHeight
42 *
43 *应用深度OCR对不同的图像。
44 for Index := 0 to 9 by 1
45 read_image (Image, ImagesPath[Index])
46 dev_open_window_fit_image (Image, 0, 0, 800, 600, WindowHandle)
47 *
48 *检测并识别单词。
49 *在图像上应用模型进行推断(输入:图像,输入:模型,输入:识别并检测模式,输出:推理结果元组)
50 apply_deep_ocr (Image, DeepOcrHandle, 'auto', DeepOcrResult)
51 *想象结果:检测显示为定向矩形,其中一个箭头表示读数方向。
52 *显示被识别的结果。
53 dev_display_deep_ocr_results (Image, WindowHandle, DeepOcrResult, [], [])
54 dev_disp_text ('按“Run (F5)”继续', 'window', 'bottom', 'right', 'black', [], [])
55 stop ()
56 dev_close_window ()
57 *
58 *将预处理后的图像与本地化的单词以及相应的字符和链接分数地图可视化。
59 *打开图像预处理窗口
60 dev_open_window (0, 0, DisplayWidth, DisplayHeight, 'black', WindowPreprocessedImage)
61 *从字典中检索与键关联的对象(输出:预处理对象,输入:字典即推理结果元组,输入:格式)
62 get_dict_object (PreprocessedImage, DeepOcrResult, 'image')
63 dev_display (PreprocessedImage)
64 *
65 *本地化的单词以相对于输入图像的定向矩形的形式给出。
66 *因此,它们被重新缩放,以便在预处理图像中显示它们。
67 get_image_size (Image, ImageWidth, ImageHeight)
68 *检测到预处理图像的缩放(输出:检测缩放比例后轮廓,输入:推理结果元组,输入:现在图像宽度,输入:现在图像高度
69 *输入:原来图像宽度,输入:原来图像高度,输出:单词检测)
70 scale_detections_to_preprocessed_image (DetectionsScaled, DeepOcrResult, ImageWidth, ImageHeight, DetectionImageWidth, DetectionImageHeight, WordsDetected)
71 dev_set_line_width (2)
72 if (WordsDetected)
73 dev_display (DetectionsScaled)
74 dev_disp_text ('预处理图像和检测', 'image', 10, 10, 'black', [], [])
75 else
76 dev_disp_text ('再加工的形象', 'image', 10, 10, 'black', [], [])
77 endif
78 *
79 dev_open_window (0, DisplayWidth + 10, 2 * DisplayWidth, DisplayHeight, 'black', WindowScoreMaps)
80 *深度OCR评分图的可视化。(输入:显示分数图的窗口句柄,输入:推理结果元组)
81 dev_display_deep_ocr_score_maps (WindowScoreMaps, DeepOcrResult, [], [])
82 dev_disp_text ('按“Run (F5)”继续', 'window', 'bottom', 'right', 'black', [], [])
83 stop ()
84 *设置“查阅表”(否)。
85 dev_set_lut ('default')
86 dev_close_window ()
87 dev_close_window ()
88 endfor
89 *
90 clear_handle (DeepOcrHandle)
91 *
92 * *************************************************************
93 *第2部分:只识别单词。
94 * *************************************************************
95 *创建模型。
96 create_deep_ocr ('mode', 'recognition', DeepOcrHandle)
97 set_suitable_device_in_ocr_handle (DeepOcrHandle)
98 *
99 dev_open_window (0, 0, 500, 100, 'black', WindowHandle)
100 *
101 *应用深度OCR对不同的图像。
102 for Index := 1 to 10 by 1
103 * 阅读紧密裁剪的单词图像
104 read_image (Image, PathExample + 'cropped_text_image_' + Index$'.2d')
105 *
106 * 认出这个单词。
107 apply_deep_ocr (Image, DeepOcrHandle, 'recognition', DeepOcrResult)
108 *
109 *可视化模型的结果。
110 dev_display_deep_ocr_results (Image, WindowHandle, DeepOcrResult, [], [])
111 dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
112 stop ()
113 endfor
114 *
115 *很长的单词可能会导致预处理后的图像部分,其中的文本过于固定,无法识别。
116 *这可以通过自适应识别_image_width处理,如下所示。
117
118 read_image (Image, 'ocr/medication_package_02_back.png')
119 *
120 *裁剪被识别组件识别的单词。
121 crop_rectangle1 (Image, ImageWord, 378, 176, 409, 605)
122 *
123 *应用默认的recotion_image_width识别。
124 apply_deep_ocr (ImageWord, DeepOcrHandle, 'recognition', DeepOcrResult)
125 *
126 *可视化模型的结果。
127 dev_display_deep_ocr_results (ImageWord, WindowHandle, DeepOcrResult, [], [])
128 dev_disp_text (['Original image.','The recognition result is wrong because','the width of the model is too small.'], 'window', 'bottom', 'right', 'black', [], [])
129 get_dict_object (ImagePreprocessed, DeepOcrResult, 'image')
130 change_format (ImagePreprocessed, ImagePreprocessedPadded, 500, 32)
131 dev_open_window (200, 0, 500, 120, 'black', WindowHandlePreprocessed1)
132 dev_display_deep_ocr_results (ImagePreprocessedPadded, WindowHandlePreprocessed1, DeepOcrResult, [], [])
133 get_deep_ocr_param (DeepOcrHandle, 'recognition_image_width', RecognitionImageWidth)
134 dev_disp_text (['Preprocessed image with','recognition_image_width = ' + RecognitionImageWidth,'','The characters are not recognizable.'], 'window', 'top', 'right', 'black', [], [])
135 *
136 *将recognition_image_width设置为更大的值,这样描述的单词在预处理后的图像中仍然可以识别。
137 RecognitionImageWidth := 250
138 set_deep_ocr_param (DeepOcrHandle, 'recognition_image_width', RecognitionImageWidth)
139 apply_deep_ocr (ImageWord, DeepOcrHandle, 'recognition', DeepOcrResult)
140 *
141 * 将预处理后的图像和相应的结果可视化。
142 get_dict_object (ImagePreprocessed, DeepOcrResult, 'image')
143 change_format (ImagePreprocessed, ImagePreprocessedPadded, 500, 32)
144 dev_open_window (390, 0, 500, 120, 'black', WindowHandlePreprocessed2)
145 dev_display_deep_ocr_results (ImagePreprocessedPadded, WindowHandlePreprocessed2, DeepOcrResult, [], [])
146 dev_disp_text (['Preprocessed image with','recognition_image_width = ' + RecognitionImageWidth,'','The characters are recognizable.'], 'window', 'top', 'right', 'black', [], [])
147 dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
148 *
149 stop ()
150 *
151 dev_close_window ()
152 dev_close_window ()
153 dev_close_window ()
154 *
155 clear_handle (DeepOcrHandle)
156 *
157 * *************************************************************
158 *第3部分:仅检测单词。
159 * *************************************************************
160 *创建模型。
161 create_deep_ocr ('mode', 'detection', DeepOcrHandle)
162 set_suitable_device_in_ocr_handle (DeepOcrHandle)
163 *
164 *对不同的图像应用深度OCR。
165 for Index := 0 to 2 by 1
166 read_image (Image, ImagesPath[Index])
167 dev_open_window_fit_image (Image, 0, 0, 800, 600, WindowHandle)
168 *
169 * 检测单词。
170 apply_deep_ocr (Image, DeepOcrHandle, 'detection', DeepOcrResult)
171 *
172 * 可视化模型的结果。
173 dev_display_deep_ocr_results (Image, WindowHandle, DeepOcrResult, [], [])
174 dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
175 stop ()
176 dev_close_window ()
177 endfor
178 *
179 clear_handle (DeepOcrHandle)
180