隶属于:
C:\Users\Public\Documents\MVTec\HALCON-23.05-Progress\examples\hdevelop\OCR\Convolutional-Neural-Networks
此例,本示例展示了如何通过使用正则表达式或词汇表限制允许的结果来提高OCR结果的准确性。几个核心算子read_ocr_class_cnn 和create_lexicon和do_ocr_word_cnn和partition_dynamic和var_threshold和text_line_orientation
读图,读入OCR的分类器句柄
read_ocr_class_cnn (‘Universal_NoRej’, OCRHandle)
获取包含字符区域
text_line_orientation得到字符的角度,并转正图
获取字符区域
识别字符,识别所有和识别单词
识别日期,识别所有和识别正则表达式
并显示所有
* 本示例展示了如何通过使用正则表达式或词汇表限制允许的结果来提高OCR结果的准确性
* 注意,分类结果是人为扭曲的,仅用于演示目的
dev_update_off ()
dev_close_window ()
read_image (Image, 'label/label_01')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 400, 300, 'black', WindowHandle)
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
*加载用于字符分类的预训练字体。
read_ocr_class_cnn ('Universal_NoRej', OCRHandle)
*创建一个包含3个允许单词的词汇表。
create_lexicon ('label', ['BEST', 'BEFORE', 'END'], LexiconHandle)
for I := 1 to 9 by 1
* 读图
read_image (Image, 'label/label_0' + I)
* 查找标签。
threshold (Image, Region, 128, 230)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, LabelRaw, 'width', 'and', 350, 450)
shape_trans (LabelRaw, Label, 'rectangle2')
*执行粗略的去斜操作(当然,文本行甚至都不是平行的)。
text_line_orientation (Label, Image, 25, -0.523599, 0.523599, OrientationAngle)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, 0, 0, Deskew)
affine_trans_image (Image, ImageDeskew, Deskew, 'constant', 'false')
affine_trans_region (Label, LabelDeskew, Deskew, 'nearest_neighbor')
smallest_rectangle1 (LabelDeskew, LabelTop, LabelLeft, LabelBottom, LabelRight)
reduce_domain (ImageDeskew, LabelDeskew, ImageOCR)
*提取字符区域。
var_threshold (ImageOCR, Foreground, 40, 40, 0.8, 10, 'dark')
connection (Foreground, Blobs)
partition_dynamic (Blobs, Split, 21, 40)
select_shape (Split, Characters, ['width', 'height'], 'and', [10, 20], [30, 50])
select_shape (Characters, CharactersWords, 'row', 'and', 0, LabelTop + 80)
select_shape (Characters, CharactersDate, 'row', 'and', LabelTop + 80, 600)
*人为扭曲分割结果,以便进行校正演示。
move_region (CharactersWords, CharactersWords, 0, 3)
move_region (CharactersDate, CharactersDate, -2, 0)
*处理单词文本。
sort_region (CharactersWords, SortedWords, 'character', 'true', 'row')
area_center (SortedWords, Area, Row, Column)
Column[|Column|] := 9999
gen_empty_obj (Word)
Text := ''
OriginalText := ''
for J := 1 to |Column| - 1 by 1
select_obj (SortedWords, Character, J)
concat_obj (Word, Character, Word)
*检查字符间距以确定单词的结尾。
if (J == |Column| or (Column[J] - Column[J - 1]) > 30)
*不加限制地对单词进行分类(用于对比)。
do_ocr_word_cnn (Word, ImageOCR, OCRHandle, '.*', 1, 5, Class, Confidence, WordText, WordScore)
OriginalText := OriginalText + ' ' + WordText
* 使用词汇表对单词进行分类。
do_ocr_word_cnn (Word, ImageOCR, OCRHandle, '<label>', 1, 5, Class, Confidence, WordText, WordScore)
Text := Text + ' ' + WordText
gen_empty_obj (Word)
endif
endfor
* 处理日期文本。
sort_region (CharactersDate, SortedDate, 'character', 'true', 'row')
*不加限制地对日期字符串进行分类(用于对比)。
do_ocr_word_cnn (SortedDate, ImageOCR, OCRHandle, '.*', 5, 5, Class, Confidence, OriginalDateText, DateScore)
* 使用正则表达式对日期字符串进行分类。
do_ocr_word_cnn (SortedDate, ImageOCR, OCRHandle, '^([0-2][0-9]|30|31)/(0[1-9]|10|11|12)/0[0-5]$', 10, 5, Class, Confidence, DateText, DateScore)
* 可视化
dev_clear_window ()
dev_display (ImageOCR)
dev_set_colored (6)
dev_display (Characters)
dev_set_color ('red')
dev_disp_text ('Distorted: ' + OriginalText + ' ' + OriginalDateText, 'image', 10, 10, 'red', 'box', 'false')
dev_disp_text ('Corrected: ' + Text + ' ' + DateText, 'image', 40, 10, 'green', 'box', 'false')
if (I < 9)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
endif
stop ()
endfor