Tesseract的OCR引擎最早由HP實驗室於1985年開始研發,至1995年時已經成爲OCR業內最準確的三款識別引擎之一。然而,HP不久便決定放棄OCR業務,Tesseract也今後塵封。linux
數年之後,HP意識到,與其將Tesseract束之高閣,不如貢獻給開源軟件業,讓其重煥新生--2005年,Tesseract由美國內華達州信息技術研究所得到,並求諸於Google對Tesseract進行改進、消除Bug、優化工做。git
Tesseract目前已做爲開源項目發佈在Google Project,其最新版本3.0已經支持中文OCR,並提供了一個命令行工具。github
Tesseract 能夠在 linux、windows 和 macOS 下使用。這裏以 macOS 爲例。windows
brew install --with-training-tools tesseract工具
雖然官方提供了多種語言的識別數據包,可是在不少狀況下都須要在其基礎上本身進行訓練,因此記得安裝訓練工具post
從官網地址下載對應的識別包 [官網地址](https://github.com/tesseract-...
) 好比中文識別選擇 chi_sim 這個包字體
再將識別包放置到系統的相應目錄中,好比我用 hoembrew 安裝,地址位於:優化
/usr/local/Cellar/tesseract/3.05.01/share/tessdataui
tesseract 的好處就在可以本身訓練須要識別的字符,若是隻是識別印刷字體這樣的需求仍是調用 API 比較方便,好比說百度雲的文字識別 API。.net
要進行訓練就下載 jTessBoxEditor 樣本訓練工具
查看版本
tesseract -v
查看幫助
tesseract --help
進行識別的命令
tesseract in.jpg out.txt -l chi_sim -psm 6
in.jpg:須要識別的圖片
out.jpg:圖片的結果輸出到的位置
-l:後跟用於進行識別的數據包,這裏是中文
-psm:識別方式
查看已有的語言識別包
➜libai tesseract --list-langs List of available languages (4): chi_sim eng libai number
語言識別的包是識別的基礎,下載以後默認自帶的是 eng.traineddata 英文識別包。他可以用來識別英文,可是不能識別中文。因此要下載中文的識別包 ch_sim.traineddata 這樣就能識別中文。可是發現準確率不高,不符合需求就要訓練本身的包。
總之核心就在 traineddata 文件。
psm 的參數很重要,表示 tesseract 識別圖像的方式,好比說是一行一行識別仍是逐字識別。但願逐字識別可使用 -psm 10,但願逐行識別可使用 -psm 6,其餘沒怎麼用之後有機會補充。總之,但願有更好的識別效果須要選擇合適的 psm。
tesseract --help-psm 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.
谷歌翻譯結果,本身嘗試一下會有體會
方向和腳本檢測(OSD)。 1自動頁面分割與OSD。 2自動頁面分割,但沒有OSD或OCR。 3全自動頁面分割,但沒有OSD。 (默認) 4假設單列可變大小的文本。 5假設一個垂直對齊的文本的統一塊。 6假設單個統一的文本塊。 7將圖像視爲單個文本行。 8將圖像視爲單個字。 9將圖像視爲一個單個的單詞。 10將圖像視爲單個字符。
tesseract libai.jpg libai -l chi_sim -psm 6
抹甫曰月閆充, 屁是訛上霜; 亭孰塑明汛 佃爽崽故歹o
顯然準確率不高,進行訓練。
1.生成字體文件
打開jTessBoxEditor工具,菜單欄:tools->Merge TIFF...,選中要合成的圖片並保存爲爲:libai.tif
2.生成 box 文件
tesseract libai.tif libai -l chi_sim -psm 6 batch.nochop makebox
3.利用 jTessBoxEditor 校訂
校訂以後記得保存。
4.生成.tr文件
tesseract libai.tif libai -psm 6 nobatch box.train
5.生成unicharset文件
unicharset_extractor libai.box
6.建立font_properties文件
echo 'font 0 0 0 0 0' > font_properties
7.training
shapeclustering -F font_properties -U unicharset libai.tr
mftraining -F font_properties -U unicharset -O l libai.tr
8.Clustering。產生字符形狀正常化特徵文件normproto
cntraining libai.tr
9.重命名文件
mv normproto libai.normproto mv inttemp libai.inttemp mv pffmtable libai.pffmtable mv shapetable libai.shapetable mv unicharset libai.unicharset
10.合併文件生成 traineddata 文件
➜ libai combine_tessdata libai. Combining tessdata files TessdataManager combined tesseract data files. Offset for type 0 (libai.config ) is -1 Offset for type 1 (libai.unicharset ) is 140 Offset for type 2 (libai.unicharambigs ) is -1 Offset for type 3 (libai.inttemp ) is 855 Offset for type 4 (libai.pffmtable ) is 140145 Offset for type 5 (libai.normproto ) is 140274 Offset for type 6 (libai.punc-dawg ) is -1 Offset for type 7 (libai.word-dawg ) is -1 Offset for type 8 (libai.number-dawg ) is -1 Offset for type 9 (libai.freq-dawg ) is -1 Offset for type 10 (libai.fixed-length-dawgs ) is -1 Offset for type 11 (libai.cube-unicharset ) is -1 Offset for type 12 (libai.cube-word-dawg ) is -1 Offset for type 13 (libai.shapetable ) is 141781 Offset for type 14 (libai.bigram-dawg ) is -1 Offset for type 15 (libai.unambig-dawg ) is -1 Offset for type 16 (libai.params-model ) is -1 Output libai.traineddata created successfully.
11.將文件拷貝到目標文件夾
cp libai.traineddata /usr/local/Cellar/tesseract/3.05.01/share/tessdata
12.再次識別
tesseract libai.jpg trained -l libai -psm 6
➜ libai cat trained.txt 牀前明月光, 疑是地上霜; 舉頭望明月, 低頭思故鄉.
tesseract 可以進行 OCR 識別,可是對中文的支持不夠好。因此若是要更好地識別中文字體須要進行大量的訓練,有時間精力固然是一個方案,沒有的話仍是調用現成的 API 比較好。至少對印刷的中文字識別效果是很好的。
我寫好的放在 Tesseract 和百度識別的 Demo
參考資料:
Tesseract 3 語言數據的訓練方法
使用Tesseract-OCR訓練文字識別記錄
如何使用Tesseract-OCR(v3.02.02)訓練字庫 - 做業部落 Cmd Markdown 編輯閱讀器