OCR识别详细步骤


一、总体概述 
1 Halcon 例子 里面其实自带字符训练和识别 
2 Halcon OCR训练分SVM和MLP两种 
3  Halcon提供了一些识别模型,但是毕竟自己的项目跟他的不一样,所以需要自己训练自己的模型 
二、详细流程 
4 下面先以SVM训练和识别开始(创建训练文件,训练,识别) 
SVM训练和识别(训练自己的0-9和A-Z) 
第一步:准备图片 
              每个字符对应一个文件夹,为了后期遍历文件夹方便,文件夹名字以字符直接命名,见下图。 

 

 


第二步:创建训练文件 

* 声明一个字符数组,并且将0-9和A-Z赋值此数组 

CharH := []   
for i :0 to 9 by 1   
  CharH:chr(round(i + ord('0')))   
endfor   
for i :10 to 36-1 by 1   
  CharH:chr(round(i-10 + ord('A')))   
endfor   
NumChar := |CharH| 

*声明一个训练文件.trf
 
trainFile :'ZHANG-Num0-9A-Z.trf'   
dev_set_check ('~give_error')   
delete_file (TrainFile)   
dev_set_check ('give_error') 


*遍历每个文件夹以及每个文件夹里面的字符图片,将每个文件夹与一个字符关联起来(这里每个文件夹里面的图片对应文件夹名“字符” 

此帖售价 10 金币,已有 106 人购买 [记录]

for  Indexfile: = 0  to |CharH| - 1 by 1   
list_files ('Z:\\00Trainlate\\TRIAN20150909\\02pictureTrain_2015-10-26_V1.0\\blackwitewordfirstsub\\checkimage\\test\\char\\'+CharH[Indexfile], ['files','follow_links'], ImageFiles)   
tuple_regexp_select (ImageFiles, ['\\.(bmp|jpg)$','ignore_case'], ImageFiles) 
for Index  : 0  to |ImageFiles| - 1 by 1   
   read_image (ImageSige, ImageFiles[Index])   
   append_ocr_trainf(ImageSige,ImageSige,CharH[Indexfile],TrainFile)   
endfor   
endfor

第三步:训练文件(可以选择SVM训练或者MLP训练,根据自己选择的训练函数决定),获得最终模型文件.omc 

* ****   
* step: read training data   
* ****   
read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)   
stop ()   
* ****   
* step: create and train classifier   
* ****   
create_ocr_class_svm (8, 10, 'constant', 'default', CharacterNames, 'rbf', 0.02, 0.001, 'one-versus-one', 'normalization', 0, OCRHandle)   
* Train the classifier   
trainf_ocr_class_svm (OCRHandle, TrainFile, 0.001, 'default')   
stop ()   
* ****   
* step: save classifier   
* ****   
FontFile  : 'ZHANG-Num0-9A-Z_SVM.omc'    
write_ocr_class_svm(OCRHandle,FontFile)   
* free memory   
clear_ocr_class_svm (OCRHandle) 



第四步 用自己训练的.omc 文件进行识别要识别的图片 

<pre name="code" class="cpp">* Read the SVM font file from file 读取刚刚自己创建的识别模型文件   
read_ocr_class_svm ('C:/Users/Public/Documents/MVTec/HALCON-11.0/examples/solution_guide/zhang/ZHANG-Num0-9A-Z_SVM.omc', OCRHandle)   
*读取待识别的图片   
read_image(ImageSige,'C:/Users/CQU/Desktop/QQ截图20160327192542.jpg')     
*有两个识别函数,他们之间的区别看帮助文档</span>   
do_ocr_single_class_svm(ImageSige, ImageSige, OCRHandle, 1, Class)   
* Clear the classifier from memory   
clear_ocr_class_svm (OCRHandle)   


第五步:检验无误就可以随意使用.omc 文件了 
<span style="font-family: Arial, Helvetica, sans-serif;"> 
*MLP跟SVM一样,把对应的函数替换即可,具体教程看其提供的案例</span>  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM