原文地址:http://blog.csdn.net/whatday/article/details/38493551
1.Tesseract-OCR引擎簡介
OCR(Optical Character Recognition):光學字符識別,是指對圖片文件中的文字進行分析識別,獲取的過程。
Tesseract的OCR引擎最先由HP實驗室於1985年開始研發,至1995年時已經成為OCR業內最准確的三款識別引擎之一。然而,HP不久便決定放棄OCR業務,Tesseract也從此塵封。
數年以后,HP意識到,與其將Tesseract束之高閣,不如貢獻給開源軟件業,讓其重煥新生--2005年,Tesseract由美國內華達州信息技術研究所獲得,並求諸於Google對Tesseract進行改進、消除Bug、優化工作。
2.Tesseract-OCR的下載安裝
google下載地址:http://code.google.com/p/tesseract-ocr
需要下載安裝程序(tesseract-ocr-setup-3.02.02.exe)和語言庫,默認有英文的語言庫只能識別英文,所以還需要下載中文語言庫,
由於google經常打不開,所以通過代理下載后,上傳到了CSDN,
安裝程序下載地址:http://download.csdn.net/detail/whatday/7740469
簡體中文語言庫下載地址:http://download.csdn.net/detail/whatday/7740531
繁體中文語言庫下載地址:http://download.csdn.net/detail/whatday/7740429
安裝tesseract-ocr-setup-3.02.02.exe 默認安裝就可以了,因為如果選擇了其他項,安裝程序會自動從網上下載,語言庫和其他文件,有很大一部分是從google下載,由於不能打開所以會出錯,先默認安裝上,需要什么文件再下載就行。
我系統是windows8.1 默認安裝好后目錄如下:
其中tesseract.exe是主程序, tessdata目錄是存放語言文件 和 配置文件的,下載或自己生成的語言文件放到此目錄里就可以了。
3.Tesseract-OCR的命令行使用
打開DOS界面,輸入tesseract:

如果出現如上輸出,表示安裝正常。
我准備了一張驗證碼1.png放在D盤根目錄下
其中 1.png是驗證碼圖片 result是結果文件的名稱 默認是.TXT文件 執行成功后會在驗證碼圖片所在位置生成result.txt 打開結果為:

命令詳解:
Usage:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]
pagesegmode values are:
0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.
-l lang and/or -psm pagesegmode must occur before anyconfigfile.
tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]
tesseract 圖片名 輸出文件名 -l 字庫文件 -psm pagesegmode 配置文件
例如:
tesseract code.jpg result -l chi_sim -psm 7 nobatch
-l chi_sim 表示用簡體中文字庫(需要下載中文字庫文件,解壓后,存放到tessdata目錄下去,字庫文件擴展名為 .raineddata 簡體中文字庫文件名為: chi_sim.traineddata)
-psm 7 表示告訴tesseract code.jpg圖片是一行文本 這個參數可以減少識別錯誤率. 默認為 3
configfile 參數值為tessdata\configs 和 tessdata\tessconfigs 目錄下的文件名
4.Tesseract-OCR的QA合集
A.ImageMagick是什么?
ImageMagick是一個用於查看、編輯位圖文件以及進行圖像格式轉換的開放源代碼軟件套裝
我在這里之所以提到ImageMagick是因為某些圖片格式需要用這個工具來轉換。
B.Leptonica 是什么?
Leptonica 是一圖像處理與圖像分析工具,tesseract依賴於它。而且不是所有的格式(如jpg)都能處理,所以我們需要借助imagemagick做格式轉換。
Here's a summary of compression support and limitations:
- All formats except JPEG support 1 bpp binary.
- All formats support 8 bpp grayscale (GIF must have a colormap).
- All formats except GIF support 24 bpp rgb color.
- All formats except PNM support 8 bpp colormap.
- PNG and PNM support 2 and 4 bpp images.
- PNG supports 2 and 4 bpp colormap, and 16 bpp without colormap.
- PNG, JPEG, TIFF and GIF support image compression; PNM and BMP do not.
- WEBP supports 24 bpp rgb color.
C.提高圖片質量?
識別成功率跟圖片質量關系密切,一般拿到后的驗證碼都得經過灰度化,二值化,去噪,利用imgick就可以很方便的做到.
convert -monochrome foo.png bar.png #將圖片二值化
D.我只想識別字符和數字?
結尾僅需要加digits
命令實例:tesseract imagename outputbase digits
E.訓練你的tesseract
不得不說,tesseract英文識別率已經很不錯了(現有的tesseract-data-eng),但是驗證碼識別還是太雞肋了。但是請別忘記,tesseract的智能識別是需要訓練的.
F.命令執行出現empty page!!錯誤
嚴格來說,這不是一個bug(tesseract 3.0),出現這個錯誤是因為tesseract搞不清圖像的字符布局
-psm N
Set Tesseract to only run a subset of layout analysis and assume a certain form of image. The options for N are:
0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR.
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.
對於我們的驗證碼a.tif排列來說,采用-psm 7(single text line)比較合適。
5.Tesseract-OCR的訓練方法
A.使用jTessBoxEditor工具
1.下載地址:http://download.csdn.net/detail/whatday/7740739
這個工具是用來訓練樣本用的,由於該工具是用Java開發的,需要安裝JAVA虛擬機1.6才能運行。
2. 獲取樣本圖像。用畫圖工具繪制了5張0-9的文樣本圖像(當然樣本越多越好),如下圖所示:
4.生成Box File文件。打開命令行,執行命令:
- tesseract.exe num.font.exp0.tif num.font.exp0 batch.nochop makebox
生成的BOX文件為num.font.exp0.box,BOX文件為Tessercat識別出的文字和其坐標。
注:Make Box File的命令格式為:
- tesseract [lang].[fontname].exp[num].tif [lang].[fontname].exp[num] batch.nochop makebox
其中lang為語言名稱,fontname為字體名稱,num為序號,可以隨便定義。
5.文字校正。運行jTessBoxEditor工具,打開num.font.exp0.tif文件(必須將上一步生成的.box和.tif樣本文件放在同一目錄),如下圖所示。可以看出有些字符識別的不正確,可以通過該工具手動對每張圖片中識別錯誤的字符進行校正。校正完成后保存即可。
6.定義字體特征文件。Tesseract-OCR3.01以上的版本在訓練之前需要創建一個名稱為font_properties的字體特征文件。
font_properties不含有BOM頭,文件內容格式如下:
- <fontname> <italic> <bold> <fixed> <serif> <fraktur>
其中fontname為字體名稱,必須與[lang].[fontname].exp[num].box中的名稱保持一致。<italic> 、<bold> 、<fixed> 、<serif>、 <fraktur>的取值為1或0,表示字體是否具有這些屬性。
這里在樣本圖片所在目錄下創建一個名稱為font_properties的文件,用記事本打開,輸入以下下內容:
- font 0 0 0 0 0
7.生成語言文件。在樣本圖片所在目錄下創建一個批處理文件,輸入如下內容。
- rem 執行改批處理前先要目錄下創建font_properties文件
- echo Run Tesseract for Training..
- tesseract.exe num.font.exp0.tif num.font.exp0 nobatch box.train
- echo Compute the Character Set..
- unicharset_extractor.exe num.font.exp0.box
- mftraining -F font_properties -U unicharset -O num.unicharset num.font.exp0.tr
- echo Clustering..
- cntraining.exe num.font.exp0.tr
- echo Rename Files..
- rename normproto num.normproto
- rename inttemp num.inttemp
- rename pffmtable num.pffmtable
- rename shapetable num.shapetable
- echo Create Tessdata..
- combine_tessdata.exe num.
將批處理通過命令行執行。執行后的結果如下:
需確認打印結果中的Offset 1、3、4、5、13這些項不是-1。這樣,一個新的語言文件就生成了。
num.traineddata便是最終生成的語言文件,將生成的num.traineddata拷貝到Tesseract-OCR-->tessdata目錄下。可以用它來進行字符識別了。
訓練前:

2. 打開命令行,定位到Tesseract-OCR目錄,輸入命令:
- tesseract.exe number.jpg result -l eng
其中result表示輸出結果文件txt名稱,eng表示用以識別的語言文件為英文。
3. 打開Tesseract-OCR目錄下的result.txt文件,看到識別的結果為7542315857,有3個字符識別錯誤,識別率還不是很高,那有沒有什么方法來提供識別率呢?Tesseract提供了一套訓練樣本的方法,用以生成自己所需的識別語言庫。下面介紹一下具體訓練樣本的方法。
訓練后:
用訓練后的語言庫識別number.jpg文件, 打開命令行,定位到Tesseract-OCR目錄,輸入命令:
- tesseract.exe number.jpg result -l eng
識別結果如如圖所示,可以看到識別率提高了不少。通過自定義訓練樣本,可以進行圖形驗證碼、車牌號碼識別等。感興趣的朋友可以研究研究。
B.使用 CowBoxer工具
下載地址為:http://download.csdn.net/detail/whatday/7740815
第一步生成第一個 box 文件演示中將 Tesseract 解壓到了 E:\tesseract-ocr 目錄。然后在該目錄中建立了一個 build 目錄用於存放原始數據和訓練過程中生成的文件。原始圖片數據一個有 3 個 (test.001.tif - test.003.tif):



首先生成第一個圖片 test.001.tif 的 box 文件,這里使用官方的 eng 語言數據進行文字識別:
Tesseract Open Source OCR Engine with Leptonica
Number of found pages: 1.
執行完這個命令之后,build 目錄下就生成了一個 test.001.box。使用 CowBoxer 打開這個 box 文件,CowBoxer 會自動找到同名的 tif 文件顯示出來。

CowBoxer 的使用方法可以看 Help -> About 中的說明。修改完成之后 File -> Save box file 保存文件。
生成初始的 traineddata
接下來使用這一個 box 文件先生成一個 traineddata,在接下來生成其他圖片的 box 文件時,使用這個 traineddata 有利於提高識別的正確率,減少修改次數。
..\training\unicharset_extractor test.001.box
..\training\mftraining -U unicharset -O test.unicharset test.001.tr
..\training\cntraining test.001.tr
rename normproto test.normproto
rename Microfeat test.Microfeat
rename inttemp test.inttemp
rename pffmtable test.pffmtable
..\training\combine_tessdata test.
在 build 目錄下執行完這一系列命令之后,就生成了可用的 test.traineddata。
生成其余 box 文件
將上一步生成的 test.traineddata 移動到 tesseract-ocr\tessdata 目錄中,接下來生成其他 box 文件時就可以通過 -l test 參數使用它了。
..\tesseract test.003.tif test.003 -l test batch.nochop makebox
這里僅僅是使用 3 個原始文件作為例子。實際制作訓練文件時,什么時候生成一個 traineddata 根據情況而定。中途生成 traineddata 的目的只是為了提高文字識別的准確率,使后面生成的 box 文件能少做修改。
生成最終的 traineddata
在所有的 box 都制作完成后,就可以生成最終的 traineddata 了。
..\tesseract test.002.tif test.002 nobatch box.train
..\tesseract test.003.tif test.003 nobatch box.train
..\training\unicharset_extractor test.001.box test.002.box test.003.box
..\training\mftraining -U unicharset -O test.unicharset test.001.tr test.002.tr test.003.tr
..\training\cntraining test.001.tr test.002.tr test.003.tr
rename normproto test.normproto
rename Microfeat test.Microfeat
rename inttemp test.inttemp
rename pffmtable test.pffmtable
..\training\combine_tessdata test.
在文件較多時可以用程序生成這種腳本執行。
未完。。。