【基本用法】

1、tesseract基本语法:

Usage:tesseract.exep_w_picpathnameoutputbase[-llang][-psmpagesegmode][configfile...]pagesegmodevaluesare:0=Orientationandscriptdetection(OSD)only.1=AutomaticpagesegmentationwithOSD.2=Automaticpagesegmentation,butnoOSD,orOCR3=Fullyautomaticpagesegmentation,butnoOSD.(Default)4=Assumeasinglecolumnoftextofvariablesizes.5=Assumeasingleuniformblockofverticallyalignedtext.6=Assumeasingleuniformblockoftext.7=Treatthep_w_picpathasasingletextline.8=Treatthep_w_picpathasasingleword.9=Treatthep_w_picpathasasinglewordinacircle.10=Treatthep_w_picpathasasinglecharacter.-llangand/or-psmpagesegmodemustoccurbeforeanyconfigfile.Singleoptions:-v--version:versioninfo--list-langs:listavailablelanguagesfortesseractenginetesseractp_w_picpathnameoutputbase[-llang][-psmpagesegmode][configfile...]tesseract图片名输出文件名-l字库文件-psmpagesegmode配置文件

2、示例:

(1)、

tesseractcode.jpgcode-lchi_sim-psm7digitscode生成code.txt的结果文件-lchi_sim表示用简体中文字库-psm7表示告诉tesseractcode.jpg图片是一行文本,默认为3configfile参数值为tessdata\configs和tessdata\tessconfigs目录下的文件名digits内容为tessedit_char_whitelist0123456789-.表示数字

(2)、白名单

tesseractcode.jpgcode-leng-psm7-ctessedit_char_whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

(2)、黑名单

tesseractcode.jpgcode-leng-psm7-ctessedit_char_blacklist="abcdefghijklmnopqrstuvwxy"


【训练】(以训练arial字体为例)

1、准备一张字体图片如下。

2、用jTessBoxEditor 将图片转为tif文件,将tif文件命名为 eng.arial.exp0.tif。注意这里其实可以选多张图片。

Tools->MergeTIFF...

3、生成坐标文件(.box)。

tesseract.exeeng.arial.exp0.tifeng.arial.exp0batch.nochopmakebox

【语法】:tesseract [lang].[fontname].exp[num].tif [lang].[fontname].exp[num] batch.nochop makebox

lang为语言名称,fontname为字体名称,num为序号;在tesseract中,一定要注意格式。

4、在当前目录创建font_properties 文件,内容如下。

eng.arial.exp0.box11100

【语法】:<fontname> <italic> <bold> <fixed> <serif> <fraktur>

fontname为字体名称,italic为斜体,bold为黑体字,fixed为默认字体,serif为衬线字体,fraktur德文黑字体,1和0代表有和无,精细区分时可使用。

5、字符校正。

打开jTessBoxEditor,BOX Editor -> Open,打开 eng.arial.exp0.tif,注意多页时页面切换。

6、执行批处理文件(arial.bat),生成.traineddata文件。

echoRunTesseractforTraining..tesseracteng.arial.exp0.tifeng.arial.exp0nobatchbox.trainechoComputetheCharacterSet..unicharset_extractoreng.arial.exp0.boxmftraining-Ffont_properties-Uunicharset-Oarial.unicharseteng.arial.exp0.trechoClustering..cntrainingeng.arial.exp0.trechoRenameFiles..renamenormprotoarial.normprotorenameinttemparial.inttemprenamepffmtablearial.pffmtablerenameshapetablearial.shapetableechoCreateTessdata..combine_tessdataarial.echo.&pause

7、将生成文件中的arial.traineddata 文件拷贝到相应tessdata目录就可以使用啦!

tesseractcode.jpgcode-larial


【参考文献】

1、啥都不懂也能识别验证码

2、Tesseract-OCR的简单使用与训练

3、Adding New Fonts to Tesseract 3 OCR Engine

4、Python做简单的验证码识别(ocr)


*** walker ***