一、下载
1. tesseract-ocr
配置环境变量
path: 需要tesseract-ocr(安装路径) Tesseract-OCR\tessdata(字体路径)
TESSDATA_PREFIX: Tesseract-OCR\tessdata(字体路径)
检测:
tesseract -v
使用:
// tesseract 图片名称 生成的结果文件的名称 字库
tesseract test.png result -l chi_sim
2. 字体库
chi_sim 简体中文
chi_tra 繁体中文
3. jTessBoxEditor 训练
3.1. 准备
准备图片,原图加上转TIF(可用画图工具) 或者在线转换
tif文件命名格式[lang].[fontname].exp[num].tif
tif命名规则:lang为语言名称,fontname为字体名称,num为图片序号;
比如我们要训练自定义字库 testlang、字体名normal,则命名为testlang.normal.exp0.tif
合并:
jTessBoxEditor,Tools->Merge TIFF,将样本文件全部选上,并将合并文件保存为testlang.font.exp0.tif
3.2. 生成box文件
使用训练过的字库生成.box文件 :
# tesseract tif文件名.tif -l 字体库 box文件名 batch.nochop makebox 一定要注意格式。
tesseract testlang.normal.exp0.tif -l chi_sim testlang.normal.exp0 batch.nochop makebox
#tesseract znzd.list.exp1.tif -l znzd znzd.list.exp1 batch.nochop makebox
3.3. 快速脚本(使用后可忽略3.4-3.10)
filename的名字替换自己的取的名字
set filename=testlang.normal.exp0
:: 生成字符集文件
echo Generate unicharset...
unicharset_extractor %filename%.box
:: 生成训练文件
echo Run Tesseract for Training...
tesseract %filename%.tif %filename% nobatch -l chi_sim box.train
:: 生成font_properties
echo Generate font_properties...
echo normal 0 0 0 0 0 >font_properties
:: 生成聚集字符特征文件
echo Generate Character...
mftraining -F font_properties -U unicharset -O unicharset %filename%.tr
:: 生成字符正常化特征文件
echo Clustering...
cntraining %filename%.tr
:: 重新命名
echo Rename Files...
rename normproto testlang.normproto
rename inttemp testlang.inttemp
rename pffmtable testlang.pffmtable
rename shapetable testlang.shapetable
rename unicharset testlang.unicharset
:: 合并训练文件
echo Create Tessdata...
combine_tessdata testlang.
pause
3.4. 生成font_properties文件
【语法】<fontname> <italic> <bold> <fixed> <serif> <fraktur>
fontname为字体名称,italic为斜体,bold为黑体字,fixed为默认字体,serif为衬线字体,fraktur德文黑字体,1和0代表有和无,精细区分时可使用。
echo normal 0 0 0 0 0 >font_properties
【注意】这里输入的 “normal” 名称必须与 “testlang.normal.exp0.box” 中两个点号之间的 “normal” 名称保持一致。
3.5. 生成训练文件
#语法:tesseract tif文件名.tif tr文件名 -l 字库名 nobatch box.train
tesseract testlang.normal.exp0.tif testlang.normal.exp0 -l chi_sim nobatch box.train
3.6. 生成字符集文件
#语法: unicharset_extractor xxx.box(之前生成的box文件)
unicharset_extractor testlang.normal.exp0.box
3.7. 生成字符集文件
#语法: mftraining -F font_properties -U unicharset -O unicharset xxx.tr(之前生成的tr文件)
mftraining -F font_properties -U unicharset -O unicharset testlang.normal.exp0.tr
3.8. 生成字符正常化特征文件
#语法:cntraining tr文件名.tr
cntraining testlang.normal.exp0.tr
3.9. 重新命名(把之前的步骤生成的文件重写命名)
#语法:rename 旧文件名(固定) 新文件名 -----语言名称.旧文件名
rename normproto testlang.normproto
rename inttemp testlang.inttemp
rename pffmtable testlang.pffmtable
rename unicharset testlang.unicharset
rename shapetable testlang.shapetable
3.10. 合并训练文件
#语法: combine_tessdata 合成文件名. (注意小数点)
combine_tessdata testlang.
3.11. 使用
testlang.traineddata文件拷贝到tesseract安装目录的tessdata目录下