1. 程式人生 > >Tesseract-OCR的Training簡明教程

Tesseract-OCR的Training簡明教程

引言: Tesseract雖然功能強大,但是依然由於圖片本身諸多的干擾因素和識別能力,存在諸多的識別率不高和資訊不準確的問題,Training的過程則可以提升起識別的準確率, 本文將介紹如何train tesseract-ocr。

1.  關於Training

   Tesseract雖然很強,且可以處理各類的文字轉換,但是我們的目標圖片並非如我們所期沒有各類的干擾因素;在實際的情況下,各類的干擾因素將導致我們的識別準確率和效果大幅度下降;針對這類問題,Tesseract提供了Training的各類工具和過程,以期提升其識別率和正確率,提供可定製化的靈活性。

  •  如何來提升輸出的質量(Improve the quality of output),   https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality
  •  Traing的介紹   https://github.com/tesseract-ocr/tesseract/wiki/Training-Tesseract#introduction
  •  Testdata    https://github.com/tesseract-ocr/tessdata
  •  Training Tesseract https://github.com/tesseract-ocr/tesseract/wiki/Training-Tesseract-3.00%E2%80%933.02

2.   jTessEditorBox

   jTessEditorBox是第三方提供了一個關於自定義使用者識別結合的工具,非常的好用強大,其本身是基於Java開發而成的,需要依賴JRE的執行環境,推薦使用JDK8 u40+以上版本,具體資訊可以參考其官網資訊。

   這裡做一個小說明: 直接從其官網下載非常慢,且非常容易斷線,故直接使用了baidu的離線下載,秒級下載成功;然後繞道從百度雲盤上下載。

3.  安裝好tesseract

    這裡假定已經安裝好了tesseract,如果安裝有問題,可以參考我之前寫的一篇文字,關於如何安裝tesseract的簡明教程。

4.  建立使用者自定義的原始圖片檔案

     這裡筆者自行使用window自帶的工具,建立以下4個圖片,分別寫了簡單的數字:

    

 5.  執行jTessEditorbox工具,merge圖片

      點選開啟Tool選單,點選merge,選擇多個檔案,另存在一個合成之後的檔案:

    

   在生成另外一個merged檔案之後,進行編輯和錯誤資訊的糾偏:

   

   另存檔案或者儲存檔案之後,儲存到當下目錄。

 6.. 執行tesseract命令,生成box檔案

   >>>>  tesseract num.font.exp0.tif num.font.exp0 batch.nochop makebox

E:\testdir>tesseract num.font.exp0.tif num.font.exp0 batch.nochop makebox
Tesseract Open Source OCR Engine v3.05.00dev with Leptonica
Page 1
Page 2
Page 3
Page 4
  結果會生成一個名稱為num.font.exp0的檔案,在命令執行的當下目錄。BOX檔案為Tessercat識別出的文字和其座標。

   注:Make Box File 檔名有一定的格式,不能隨便亂取名字,命令格式為:

  tesseract [lang].[fontname].exp[num].tif [lang].[fontname].exp[num] batch.nochop makebox 
   其中lang為語言名稱,fontname為字型名稱,num為序號,可以隨便定義。

 7. 定義字型特徵檔案。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
  這裡全取值為0,表示字型不是粗體、斜體等。。

8. 生成語言檔案。在樣本圖片所在目錄下建立一個批處理檔案 num_batch.bat,輸入如下內容:

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.
然後執行這個bat檔案,執行結果資訊如下:
E:\testdir>num_batch.bat

E:\testdir>rem 執行改批處理前先要目錄下建立font_properties檔案

E:\testdir>echo Run Tesseract for Training..
Run Tesseract for Training..

E:\testdir>tesseract.exe num.font.exp0.tif num.font.exp0 nobatch box.train
Tesseract Open Source OCR Engine v3.05.00dev with Leptonica
Page 1
APPLY_BOXES:
   Boxes read from boxfile:       8
   Found 8 good blobs.
Generated training data for 2 words
Page 2
APPLY_BOXES:
   Boxes read from boxfile:       8
   Found 8 good blobs.
Generated training data for 2 words
Page 3
APPLY_BOXES:
   Boxes read from boxfile:       8
   Found 8 good blobs.
Generated training data for 2 words
Page 4
APPLY_BOXES:
   Boxes read from boxfile:       8
   Found 8 good blobs.
Generated training data for 1 words

E:\testdir>echo Compute the Character Set..
Compute the Character Set..

E:\testdir>unicharset_extractor.exe num.font.exp0.box
Extracting unicharset from num.font.exp0.box
Wrote unicharset file ./unicharset.

E:\testdir>mftraining -F font_properties -U unicharset -O num.unicharset num.font.exp0.tr
Warning: No shape table file present: shapetable
Reading num.font.exp0.tr ...
Flat shape table summary: Number of shapes = 8 max unichars = 1 number with multiple unich
Warning: no protos/configs for Joined in CreateIntTemplates()
Warning: no protos/configs for |Broken|0|1 in CreateIntTemplates()
Done!

E:\testdir>echo Clustering..
Clustering..

E:\testdir>cntraining.exe num.font.exp0.tr
Reading num.font.exp0.tr ...
Clustering ...

Writing normproto ...

E:\testdir>echo Rename Files..
Rename Files..

E:\testdir>rename normproto num.normproto

E:\testdir>rename inttemp num.inttemp

E:\testdir>rename pffmtable num.pffmtable

E:\testdir>rename shapetable num.shapetable

E:\testdir>echo Create Tessdata..
Create Tessdata..

E:\testdir>combine_tessdata.exe num.
Combining tessdata files
TessdataManager combined tesseract data files.
Offset for type  0 (num.config                ) is -1
Offset for type  1 (num.unicharset            ) is 140
Offset for type  2 (num.unicharambigs         ) is -1
Offset for type  3 (num.inttemp               ) is 710
Offset for type  4 (num.pffmtable             ) is 130474
Offset for type  5 (num.normproto             ) is 130573
Offset for type  6 (num.punc-dawg             ) is -1
Offset for type  7 (num.word-dawg             ) is -1
Offset for type  8 (num.number-dawg           ) is -1
Offset for type  9 (num.freq-dawg             ) is -1
Offset for type 10 (num.fixed-length-dawgs    ) is -1
Offset for type 11 (num.cube-unicharset       ) is -1
Offset for type 12 (num.cube-word-dawg        ) is -1
Offset for type 13 (num.shapetable            ) is 131715
Offset for type 14 (num.bigram-dawg           ) is -1
Offset for type 15 (num.unambig-dawg          ) is -1
Offset for type 16 (num.params-model          ) is -1
Output num.traineddata created successfully.
需確認列印結果中的Offset 1、3、4、5、13這些項不是-1。這樣,一個新的語言檔案就生成了。
num.traineddata便是最終生成的語言檔案,將生成的num.traineddata拷貝到Tesseract-OCR-->tessdata目錄下。可以用它來進行字元識別了。

9. 基於新的字元集合,進行字元識別驗證

驗證新的測試集合

   建立新的測試圖片,我們還是使用輸入數字,重新做了一個圖片:

  

    開啟命令列,我們進行OCR轉換:

E:\testdir>tesseract newtestnum.png newresulttest -l num
Tesseract Open Source OCR Engine v3.05.00dev with Leptonica
Warning in fopenReadFromMemory: work-around: writing to a temp file

E:\testdir>
  注意: 這裡我們使用了一個新的語言型別 num,就是我們之前進行的使用者自定義的識別字符集合。

  我們檢視一下我們的識別結果集合:

12357 684
  效果還是非常棒的。

FAQ

  1.  在windows下安裝成功之後,進行tesseract的操作,碰到如下錯誤資訊:

E:\testdir>tesseract ttest1.png test1 -l eng
Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/eng.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.
Failed loading language 'eng'
Tesseract couldn't load any languages!
Could not initialize tesseract.
   錯誤資訊的關鍵詞是tesseract_prefix的環境變數設定。

    解決辦法:  找到testData所在的目錄,預設情況下是在tesseract安裝的目錄,在環境變數中設定TESSDATA_PREFIX的環境變數為testdata所在的目錄即可。 重新執行命令即可正常使用。

參考資料

  •  JTessBoxEditor   http://vietocr.sourceforge.net/training.html    Tesseract 訓練工具
  •  Tesseract Guide  https://github.com/tesseract-ocr/tesseract/wiki   使用指南
  •  Train Tesseract  http://blog.csdn.net/yasi_xi/article/details/8763385   訓練指南