在halcon软件中,除了可以做传统的视觉分析、物体测量之外,另外一个重要的功能就是ocr。说到ocr,也就是光学字符识别,就不得不谈到机器学习和深度学习。虽然深度学习有着很好的识别效果,但是本身对样本的要求也很多,比如就需要足够多的训练样本。而传统的机器学习,识别的原理、方法具有很强的物理意义,也就是说方法本身是可以说清楚为什么会出现这个识别效果的,在特定的应用场景也同样发挥着重要的作用。
目前halcon软件中本身提供了很多的模型,当然这些模型不能覆盖所有的场景,所以本身也提供了模型训练的方法,这边可以找个例子看一下。事实上,开源软件中,比如python的sklearn同样提供了基础的机器学习算法,大家也可以学习一下。
这里不失一般性,可以选用train_characters_ocr.hdev这个文件来进行分析,
* This example program trains a font that consists
* of the characters A to G. The font is used in
* the example program classify_characters_ocr.hdev
* to read the corresponding characters.
*
read_image (Image, 'ocr/chars_training_01.png')
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (2)
dev_display (Image)
dev_update_window ('off')
dev_update_pc ('off')
dev_update_var ('off')
*
* Create an SVM classifier for the characters A to G
ClassNames := ['A','B','C','D','E','F','G']
create_ocr_class_svm (8, 10, 'constant', ['convexity','num_holes','projection_horizontal','projection_vertical'], ClassNames, 'rbf', 0.02, 0.05, 'one-versus-one', 'normalization', 10, OCRHandle)
*
* Read the training images and store the regions
* of the characters to a training file
for I := 1 to 7 by 1
read_image (Image, 'ocr/chars_training_' + I$'.2d')
dev_display (Image)
get_regions (Image, SortedRegions)
count_obj (SortedRegions, NumberObjects)
for J := 1 to NumberObjects by 1
select_obj (SortedRegions, ObjectSelected, J)
if (I == 1 and J == 1)
write_ocr_trainf (ObjectSelected, Image, ClassNames[J - 1], 'train_characters_ocr.trf')
else
append_ocr_trainf (ObjectSelected, Image, ClassNames[J - 1], 'train_characters_ocr.trf')
endif
dev_set_color ('gray')
dev_display (ObjectSelected)
disp_message (WindowHandle, ClassNames[J - 1], 'window', 10, 10 + (J * 20), 'black', 'true')
* stop ()
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endfor
*
* Check if the content of the training file is correct
read_ocr_trainf (Characters, 'train_characters_ocr.trf', CharacterNames)
count_obj (Characters, NumberCharacters)
for I := 1 to NumberCharacters by 1
select_obj (Characters, CharacterSelected, I)
dev_clear_window ()
dev_display (CharacterSelected)
disp_message (WindowHandle, CharacterNames[I - 1], 'window', 10, 10, 'black', 'true')
* stop ()
endfor
*
* Train the font, write the font to file,
* and clear the classifier from memory
trainf_ocr_class_svm (OCRHandle, 'train_characters_ocr.trf', 0.001, 'default')
reduce_ocr_class_svm (OCRHandle, 'bottom_up', 2, 0.001, OCRHandleReduced)
write_ocr_class_svm (OCRHandleReduced, 'font_characters_ocr')
*
clear_ocr_class_svm (OCRHandle)
clear_ocr_class_svm (OCRHandleReduced)
因为涉及到的代码较多,这里分析的主要是涉及svm的几个函数,
第20行,create_ocr_class_svm,准备创建一个svm模型,
第32行,write_ocr_trainf,将训练文件写入train_characters_ocr.trf,
第34行,append_ocr_trainf,将训练文件添加到append_ocr_trainf.trf,
第58行,trainf_ocr_class_svm,开始训练svm模型,
第59行,reduce_ocr_class_svm,优化裁剪svm模型,
第60行,write_ocr_class_svm,保存生成的svm模型,模型名为font_characters_ocr,
第62行,clear_ocr_class_svm,关闭初始生成svm模型句柄,
第63行,clear_ocr_class_svm,关闭优化裁剪svm模型句柄。
初始学习的时候,主要有两个方面需要注意下。第一个,就是训练svm模型的流程。一开始的时候可能难以理解,个中的参数也不知道什么意义,这个可以在后续的学习过程中不断演进。第二个,就是了解append_ocr_trainf.trf、font_characters_ocr这两个最重要的参数,前者是为了准备训练文件,后者是生成的模型名。模型生成之后,就不需要再次训练了,只需要每次加载font_characters_ocr这个模型,就可以开始分类预测了。以上就是需要注意的两个地方。
阅读量:1800
点赞量:0
收藏量:0