1、安装tesseract.exe
https://github.com/tesseract-ocr/tesseract/releases/tag/3.05.02
直接解压到需要的安装目录,如E:\Tesseract-OCR
安装完后需要下载字库放入tessdata文件夹。如果需要训练中文,需要下载chi_sim.traineddata字库。
2、设置环境变量


注意:环境变量设置后需重启,否则会无法识别。
3、安装java(如果有则跳过)
4、安装jTessBoxEditor
注意这个工具有两个版本,jTessBoxEditor和jTessBoxEditorFX
后者方便识别中文,但是非常难用
5、pycharm里面安装pytesseract和PIL库
pip install pytesseract
pip install PIL
6、准备好待识别的图片,如

7、运行jTessBoxEditor

8、merge图片
Tools→MergeTIFF
可以直接merge png后缀或者jpg后缀的所有图片,
生成tiff后缀的合并后图片,将合并文件保存为yhj.font.exp0.tif
9、生成box文件
打开cmd,进入上面tiff所在的文件夹执行下面命令:
tesseract yhj.font.exp0.tif yhj.font.exp0 -l chi_sim -- psm 7 batch.nochop makebox
注:yhj.font.exp0.tif为第8步生成的文件
后面那个yhj.font.exp0是这个box文件名
-l :表示调用的语言库
chi_sim:表示中文语言库
--psm 7 :表示按整行识别。否则有可能无法识别,提示empty!
10、再次进入jTessBoxEditor,open刚才的yhj.font.exp0.tif
即可看到待矫正的图片,全部矫正好后保存。

11、在yhj.font.exp0.tif所在文件夹建立一个文本文件,名称为font_properties,注意不能带txt后缀,其内容为:font 0 0 0 0 0

12、开始训练
新建一个文本文件,后缀修改为.bat
内容为:
echo Run Tesseract for Training..
tesseract yhj.font.exp0.tif yhj.font.exp0 -l eng --psm 7 nobatch box.train
echo Compute the Character Set..
unicharset_extractor yhj.font.exp0.box
mftraining -F font_properties -U unicharset -O yhj.unicharset yhj.font.exp0.tr
echo Clustering..
cntraining yhj.font.exp0.tr
echo Rename Files..
rename normproto yhj.normproto
rename inttemp yhj.inttemp
rename pffmtable yhj.pffmtable
rename shapetable yhj.shapetable
echo Create Tessdata..
combine_tessdata yhj.
echo. & pause
执行这个文件后,就生成了yhj.traineddata文件
将这个文件放入E:\Tesseract-OCR\tessdata
后续就可以用python调用这个字库识别图片
13、多个tif、box文件如何训练?
例如存在5个,名字分为从yhj.font.exp1~5
则.bat文件修改为以下即可:
echo Run Tesseract for Training..
tesseract yhj.font.exp1.tif yhj.font.exp1 -l eng --psm 7 nobatch box.train
tesseract yhj.font.exp2.tif yhj.font.exp2 -l eng --psm 7 nobatch box.train
tesseract yhj.font.exp3.tif yhj.font.exp3 -l eng --psm 7 nobatch box.train
tesseract yhj.font.exp4.tif yhj.font.exp4 -l eng --psm 7 nobatch box.train
tesseract yhj.font.exp5.tif yhj.font.exp5 -l eng --psm 7 nobatch box.train
echo Compute the Character Set..
unicharset_extractor yhj.font.exp1.box yhj.font.exp2.box yhj.font.exp3.box yhj.font.exp4.box yhj.font.exp5.box
mftraining -F font_properties -U unicharset -O yhj.unicharset yhj.font.exp1.tr yhj.font.exp2.tr yhj.font.exp3.tr yhj.font.exp4.tr yhj.font.exp5.tr
echo Clustering..
cntraining yhj.font.exp1.tr yhj.font.exp2.tr yhj.font.exp3.tr yhj.font.exp4.tr yhj.font.exp5.tr
echo Rename Files..
rename normproto yhj.normproto
rename inttemp yhj.inttemp
rename pffmtable yhj.pffmtable
rename shapetable yhj.shapetable
echo Create Tessdata..
combine_tessdata yhj.
echo. & pause
网友评论