驗證碼圖片識別訓練記錄
環境:
作業系統: Windows 8.1中文版
tesseract: 4.00.00alpha
1.準備
(1)在工作目錄下建立font_properties檔案.內容為:
font 0 0 0 0 0
font_properties為字元配置檔案.
(2)儲存下面內容作為bat檔案(如t.bat),生成訓練資料檔案並複製到tesseract訓練資料檔案目錄.
程式碼來自網上,稍作了修改。
echo Run Tesseract for Training.. tesseract.exe %1.font.exp0.tif %1.font.exp0 -l eng -psm 7 nobatch box.train echo Compute the Character Set.. unicharset_extractor.exe %1.font.exp0.box rem shapeclustering -F font_properties -U unicharet %1.font.exp0.tr mftraining -F font_properties -U unicharset -O %1.unicharset %1.font.exp0.tr echo Clustering.. cntraining.exe %1.font.exp0.tr echo Rename Files.. if exist %1.normproto del %1.normproto if exist %1.inttemp del %1.inttemp if exist %1.pffmtable del %1.pffmtable if exist %1.shapetable del %1.shapetable rename normproto %1.normproto rename inttemp %1.inttemp rename pffmtable %1.pffmtable rename shapetable %1.shapetable echo Create Tessdata.. combine_tessdata.exe %1. echo Copy Tessdata... copy /Y %1.traineddata TESSDATA_PREFIX echo. & pause
其中,安裝tesseract-ocr後的環境變數:
TESSDATA_PREFIX=E:\Program Files (x86)\Tesseract-OCR\tessdata
執行t.bat,需要提供語言名稱引數.如本訓練過程採用cbbs作為語言名稱.
2.訓練
(1)合併樣本檔案
用jTessBoxEditor合併樣本檔案(jpg).儲存為cbbs.font.exp0.tif
(2)生成BOX檔案
tesseract -psm 7 cbbs.font.exp0.tif cbbs.font.exp0 batch.nochop makebox
生成cbbs.font.exp0.box檔案
.必須有-psm 7引數,否則報"Empty page!!"資訊(與圖片特點有關)
.-psm 7位置不是任意的,如不能放到命令的尾部
(3)字元矯正
用jTessBoxEditor對合並樣本檔案進行矯正
(4)執行t.bat
t.bat cbbs
3.資料
如何訓練的官方資料
https://github.com/tesseract-ocr/tesseract/wiki/TrainingTesseract-4.00
權威資料。但無法短時間內理解,而且是針對linux平臺。
圖片識別,訓練
https://www.cnblogs.com/cnlian/p/5765871.html
本文訓練的參照物件。
Windows下Tesseract4.0識別與中文手寫字型訓練
https://blog.csdn.net/dcrmg/article/details/78233459
若識別中文可參考。
附:問題
執行生成訓練資料過程的命令,出現過以下問題。
(1)tesseract.exe cbbs.font.exp0.tif cbbs.font.exp0 -l eng -psm 7 nobatch box.train
有提示資訊:
Page 15
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
Boxes read from boxfile: 4
APPLY_BOXES: Unlabelled word at :Bounding box=(51,1)->(56,3)
APPLY_BOXES: Unlabelled word at :Bounding box=(82,0)->(96,28)
Found 4 good blobs.
Leaving 4 unlabelled blobs in 0 words.
2 remaining unlabelled words deleted.
Generated training data for 1 words
關於APPLY_BOXES: Unlabelled word at :Bounding box=(51,1)->(56,3)的錯誤,找到了2篇文章。
但都不符。第15張圖片(從1開始),box並沒有離得太近,而且box檔案,(51,1)的座標都沒有找到。
有的建議廢棄這種圖片。
(2)mftraining -F font_properties -U unicharset -O cbbs.unicharset cbbs.font.exp0.tr
(2.1)很多下面的資訊:
Bad properties for index 32, char N: 0,255 0,255 0,0 0,0 0,0
(2.2)程式崩潰
Error: Illegal malloc request size!
"Fatal error encountered!" == NULL:Error:Assert failed:in file ../../../../ccuti
l/globaloc.cpp, line 75
在mftraining命令前執行下面的命令之後就不崩潰了:
shapeclustering -F font_properties -U unicharet cbbs.font.exp0.tr
雖然不崩潰,但執行此命令在手寫字元圖片識別的測試中根本無法識別。這個命令不能用。
去掉後沒有再生成異常,不知道是否和再次對字元矯正有關。
選用的圖片有的人工識別都不確定,實在看不清,以上2個問題,可能和圖片本身,矯正結果有關。
mftraining在以前的粗略測試中也曾出現過crash,當時可能是從box檔案刪除某些圖片解決的。
網上相關資料:
Tesseract OCR 詞庫訓練時一些錯誤的解決辦法
http://blog.sina.com.cn/s/blog_603cc5600102v3i2.html
Tesseract OCR training gives 'APPLY_BOXES' errors
https://stackoverflow.com/questions/13394807/tesseract-ocr-training-gives-apply-boxes-errors
tesseract訓練字元中遇到的問題總結