halcon深度学习4:深度学习在 OCR的用法-deep_ocr_workflow解析

1. 什么是OCR技术

OCR,全称是Optical Character Recognition, 即光学字符识别,面向扫描文件。但是由于现在数字图像的普及,这里泛指文字检测和识别,包括扫描文档和自然场景的文字识别。

2、deep_ocr_workflow

在深度学习中,只有一篇例子关于OCR就是这一篇,文中介绍了深度OCR模型的建立与使用(如果使用过计量模型的可以较好理解,就是建立模型→设置参数→导入图片→进行处理→显示结果),根据要检测的对象可以对模式进行具体的设置。

* 
* 这个例子展示了深度学习在 OCR(文字检测)的用法:
* -第1部分:检测和识别图像中的单词。
* -第2部分:只识别单词。
* -第3部分:仅检测单词。
*重置窗口 
dev_update_off ()
dev_close_window ()
* 
*设置图像路径。
*获取HALCON系统参数的当前值。(所需的系统参数( 默认'init_new_image'),系统参数的当前值。)
PathExample := 'C:\\Users\\Public\\Documents\\MVTec\\HALCON-20.11-Steady\\examples'
get_system ('example_dir', PathExample)
PathExample := PathExample + '/images/ocr/'
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']
* 
* *************************************************************
*第一部分:检测和识别图像中的单词。
* *************************************************************
*创建模型。
*创建一个深度字符检测模型(属性名、可为‘mode’,属性值(默认[],可为只识别、只检测、识别并检测),模型句柄)
create_deep_ocr ([], [], DeepOcrHandle)
*在OCR句柄中设置合适的设备
set_suitable_device_in_ocr_handle (DeepOcrHandle)
* 
* 返回模型已经被训练的字符列表。
*返回模型的参数(输入:模型句柄,输入:识别字母,输出参数值:认识字母)
get_deep_ocr_param (DeepOcrHandle, 'recognition_alphabet', RecognitionAlphabet)
*打开显示字符识别的新窗口
dev_open_window (0, 0, 420, 320, 'black', WindowHandleAlphabet)
*显示识别字母
display_recognition_alphabet (RecognitionAlphabet, WindowHandleAlphabet)
stop ()
dev_close_window ()
* 返回模型的参数(输入:模型句柄,输入:检测图像的宽度,输出参数值:图像的宽度(模型宽度))
get_deep_ocr_param (DeepOcrHandle, 'detection_image_width', DetectionImageWidth)
* 返回模型的参数(输入:模型句柄,输入:检测图像的高度,输出参数值:图像的高度(模型高度))
get_deep_ocr_param (DeepOcrHandle, 'detection_image_height', DetectionImageHeight)
*为了更美观的视觉效果,预处理后的图像和分数以较小的尺寸显示。
DisplayWidth := 0.25 * DetectionImageWidth
DisplayHeight := 0.25 * DetectionImageHeight
* 
*应用深度OCR对不同的图像。
for Index := 0 to 9 by 1
    read_image (Image, ImagesPath[Index])
    dev_open_window_fit_image (Image, 0, 0, 800, 600, WindowHandle)
    * 
    *检测并识别单词。
    *在图像上应用模型进行推断(输入:图像,输入:模型,输入:识别并检测模式,输出:推理结果元组)
    apply_deep_ocr (Image, DeepOcrHandle, 'auto', DeepOcrResult)
    *想象结果:检测显示为定向矩形,其中一个箭头表示读数方向。
    *显示被识别的结果。
    dev_display_deep_ocr_results (Image, WindowHandle, DeepOcrResult, [], [])
    dev_disp_text ('按“Run (F5)”继续', 'window', 'bottom', 'right', 'black', [], [])
    stop ()
    dev_close_window ()
    * 
    *将预处理后的图像与本地化的单词以及相应的字符和链接分数地图可视化。
    *打开图像预处理窗口
    dev_open_window (0, 0, DisplayWidth, DisplayHeight, 'black', WindowPreprocessedImage)
    *从字典中检索与键关联的对象(输出:预处理对象,输入:字典即推理结果元组,输入:格式)
    get_dict_object (PreprocessedImage, DeepOcrResult, 'image')
    dev_display (PreprocessedImage)
    * 
    *本地化的单词以相对于输入图像的定向矩形的形式给出。
    *因此,它们被重新缩放,以便在预处理图像中显示它们。
    get_image_size (Image, ImageWidth, ImageHeight)
    *检测到预处理图像的缩放(输出:检测缩放比例后轮廓,输入:推理结果元组,输入:现在图像宽度,输入:现在图像高度
    *输入:原来图像宽度,输入:原来图像高度,输出:单词检测)
    scale_detections_to_preprocessed_image (DetectionsScaled, DeepOcrResult, ImageWidth, ImageHeight, DetectionImageWidth, DetectionImageHeight, WordsDetected)
    dev_set_line_width (2)
    if (WordsDetected)
        dev_display (DetectionsScaled)
        dev_disp_text ('预处理图像和检测', 'image', 10, 10, 'black', [], [])
    else
        dev_disp_text ('再加工的形象', 'image', 10, 10, 'black', [], [])
    endif
    * 
    dev_open_window (0, DisplayWidth + 10, 2 * DisplayWidth, DisplayHeight, 'black', WindowScoreMaps)
    *深度OCR评分图的可视化。(输入:显示分数图的窗口句柄,输入:推理结果元组)
    dev_display_deep_ocr_score_maps (WindowScoreMaps, DeepOcrResult, [], [])
    dev_disp_text ('按“Run (F5)”继续', 'window', 'bottom', 'right', 'black', [], [])
    stop ()
    *设置“查阅表”(否)。
    dev_set_lut ('default')
    dev_close_window ()
    dev_close_window ()
endfor
* 
clear_handle (DeepOcrHandle)
* 
* *************************************************************
*第2部分:只识别单词。
* *************************************************************
*创建模型。
create_deep_ocr ('mode', 'recognition', DeepOcrHandle)
set_suitable_device_in_ocr_handle (DeepOcrHandle)
* 
dev_open_window (0, 0, 500, 100, 'black', WindowHandle)
* 
*应用深度OCR对不同的图像。
for Index := 1 to 10 by 1
    * 阅读紧密裁剪的单词图像
    read_image (Image, PathExample + 'cropped_text_image_' + Index$'.2d')
    * 
    * 认出这个单词。
    apply_deep_ocr (Image, DeepOcrHandle, 'recognition', DeepOcrResult)
    * 
    *可视化模型的结果。
    dev_display_deep_ocr_results (Image, WindowHandle, DeepOcrResult, [], [])
    dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
    stop ()
endfor
* 
*很长的单词可能会导致预处理后的图像部分,其中的文本过于固定,无法识别。
*这可以通过自适应识别_image_width处理,如下所示。

read_image (Image, 'ocr/medication_package_02_back.png')
* 
*裁剪被识别组件识别的单词。
crop_rectangle1 (Image, ImageWord, 378, 176, 409, 605)
* 
*应用默认的recotion_image_width识别。
apply_deep_ocr (ImageWord, DeepOcrHandle, 'recognition', DeepOcrResult)
* 
*可视化模型的结果。
dev_display_deep_ocr_results (ImageWord, WindowHandle, DeepOcrResult, [], [])
dev_disp_text (['Original image.','The recognition result is wrong because','the width of the model is too small.'], 'window', 'bottom', 'right', 'black', [], [])
get_dict_object (ImagePreprocessed, DeepOcrResult, 'image')
change_format (ImagePreprocessed, ImagePreprocessedPadded, 500, 32)
dev_open_window (200, 0, 500, 120, 'black', WindowHandlePreprocessed1)
dev_display_deep_ocr_results (ImagePreprocessedPadded, WindowHandlePreprocessed1, DeepOcrResult, [], [])
get_deep_ocr_param (DeepOcrHandle, 'recognition_image_width', RecognitionImageWidth)
dev_disp_text (['Preprocessed image with','recognition_image_width = ' + RecognitionImageWidth,'','The characters are not recognizable.'], 'window', 'top', 'right', 'black', [], [])
* 
*将recognition_image_width设置为更大的值,这样描述的单词在预处理后的图像中仍然可以识别。
RecognitionImageWidth := 250
set_deep_ocr_param (DeepOcrHandle, 'recognition_image_width', RecognitionImageWidth)
apply_deep_ocr (ImageWord, DeepOcrHandle, 'recognition', DeepOcrResult)
* 
* 将预处理后的图像和相应的结果可视化。
get_dict_object (ImagePreprocessed, DeepOcrResult, 'image')
change_format (ImagePreprocessed, ImagePreprocessedPadded, 500, 32)
dev_open_window (390, 0, 500, 120, 'black', WindowHandlePreprocessed2)
dev_display_deep_ocr_results (ImagePreprocessedPadded, WindowHandlePreprocessed2, DeepOcrResult, [], [])
dev_disp_text (['Preprocessed image with','recognition_image_width = ' + RecognitionImageWidth,'','The characters are recognizable.'], 'window', 'top', 'right', 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
* 
stop ()
* 
dev_close_window ()
dev_close_window ()
dev_close_window ()
* 
clear_handle (DeepOcrHandle)
* 
* *************************************************************
*第3部分:仅检测单词。
* *************************************************************
*创建模型。
create_deep_ocr ('mode', 'detection', DeepOcrHandle)
set_suitable_device_in_ocr_handle (DeepOcrHandle)
* 
*对不同的图像应用深度OCR。
for Index := 0 to 2 by 1
    read_image (Image, ImagesPath[Index])
    dev_open_window_fit_image (Image, 0, 0, 800, 600, WindowHandle)
    * 
    * 检测单词。
    apply_deep_ocr (Image, DeepOcrHandle, 'detection', DeepOcrResult)
    * 
    * 可视化模型的结果。
    dev_display_deep_ocr_results (Image, WindowHandle, DeepOcrResult, [], [])
    dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
    stop ()
    dev_close_window ()
endfor
* 
clear_handle (DeepOcrHandle)

3、部分示例图结果


在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值