环境:
操作系统: Windows 8.1中文版
tesseract: 4.00.00alpha
1.准备
(1)在工作目录下创建font_properties文件.内容为:
font 0 0 0 0 0
font_properties为字符配置文件.
(2)保存下面内容作为bat文件(如t.bat),生成训练数据文件并复制到tesseract训练数据文件目录.
代码来自网上,稍作了修改。
echo Run Tesseract for Training..
tesseract.exe %1.font.exp0.tif %1.font.exp0 -l eng -psm 7 nobatch box.train
echo Compute the Character Set..
unicharset_extractor.exe %1.font.exp0.box
rem shapeclustering -F font_properties -U unicharet %1.font.exp0.tr
mftraining -F font_properties -U unicharset -O %1.unicharset %1.font.exp0.tr
echo Clustering..
cntraining.exe %1.font.exp0.tr
echo Rename Files..
if exist %1.normproto del %1.normproto
if exist %1.inttemp del %1.inttemp
if exist %1.pffmtable del %1.pffmtable
if exist %1.shapetable del %1.shapetable
rename normproto %1.normproto
rename inttemp %1.inttemp
rename pffmtable %1.pffmtable
rename shapetable %1.shapetable
echo Create Tessdata..
combine_tessdata.exe %1.
echo Copy Tessdata...
copy /Y %1.traineddata TESSDATA_PREFIX
echo. & pause
其中,安装tesseract-ocr后的环境变量:
TESSDATA_PREFIX=E:\Program Files (x86)\Tesseract-OCR\tessdata
运行t.bat,需要提供语言名称参数.如本训练过程采用cbbs作为语言名称.
2.训练
(1)合并样本文件
用jTessBoxEditor合并样本文件(jpg).保存为cbbs.font.exp0.tif
(2)生成BOX文件
tesseract -psm 7 cbbs.font.exp0.tif cbbs.font.exp0 batch.nochop makebox
生成cbbs.font.exp0.box文件
.必须有-psm 7参数,否则报"Empty page!!"信息(与图片特点有关)
.-psm 7位置不是任意的,如不能放到命令的尾部
(3)字符矫正
用jTessBoxEditor对合并样本文件进行矫正
(4)执行t.bat
t.bat cbbs
3.资料
如何训练的官方资料
https://github.com/tesseract-ocr/tesseract/wiki/TrainingTesseract-4.00
权威资料。但无法短时间内理解,而且是针对linux平台。
图片识别,训练
https://www.cnblogs.com/cnlian/p/5765871.html
本文训练的参照对象。
Windows下Tesseract4.0识别与中文手写字体训练
https://blog.csdn.net/dcrmg/article/details/78233459
若识别中文可参考。
附:问题
执行生成训练数据过程的命令,出现过以下问题。
(1)tesseract.exe cbbs.font.exp0.tif cbbs.font.exp0 -l eng -psm 7 nobatch box.train
有提示信息:
Page 15
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
Boxes read from boxfile: 4
APPLY_BOXES: Unlabelled word at :Bounding box=(51,1)->(56,3)
APPLY_BOXES: Unlabelled word at :Bounding box=(82,0)->(96,28)
Found 4 good blobs.
Leaving 4 unlabelled blobs in 0 words.
2 remaining unlabelled words deleted.
Generated training data for 1 words
关于APPLY_BOXES: Unlabelled word at :Bounding box=(51,1)->(56,3)的错误,找到了2篇文章。
但都不符。第15张图片(从1开始),box并没有离得太近,而且box文件,(51,1)的坐标都没有找到。
有的建议废弃这种图片。
(2)mftraining -F font_properties -U unicharset -O cbbs.unicharset cbbs.font.exp0.tr
(2.1)很多下面的信息:
Bad properties for index 32, char N: 0,255 0,255 0,0 0,0 0,0
(2.2)程序崩溃
Error: Illegal malloc request size!
"Fatal error encountered!" == NULL:Error:Assert failed:in file ../../../../ccuti
l/globaloc.cpp, line 75
在mftraining命令前执行下面的命令之后就不崩溃了:
shapeclustering -F font_properties -U unicharet cbbs.font.exp0.tr
虽然不崩溃,但执行此命令在手写字符图片识别的测试中根本无法识别。这个命令不能用。
去掉后没有再生成异常,不知道是否和再次对字符矫正有关。
选用的图片有的人工识别都不确定,实在看不清,以上2个问题,可能和图片本身,矫正结果有关。
mftraining在以前的粗略测试中也曾出现过crash,当时可能是从box文件删除某些图片解决的。
网上相关资料:
Tesseract OCR 词库训练时一些错误的解决办法
http://blog.sina.com.cn/s/blog_603cc5600102v3i2.html
Tesseract OCR training gives 'APPLY_BOXES' errors
https://stackoverflow.com/questions/13394807/tesseract-ocr-training-gives-apply-boxes-errors
tesseract训练字符中遇到的问题总结