OCR2:tesseract字庫訓練

因爲tesseract的中文語言包「chi_sim」對中文字體或者環境比較複雜的圖片,識別正確率不高,所以須要針對特定狀況用本身的樣本進行訓練,提升識別率,經過訓練,也能夠造成本身的語言庫。html

工具:java

第一步:合成圖片集測試


  • 打開jTessBoxEditor,選擇 Tools->Merge TIFF,進入訓練樣本所在文件夾,選中要參與訓練的樣本圖片:進行訓練的樣本圖片數量越多越好
  • 點擊 「打開」 後彈出保存對話框,選擇保存在當前路徑下,文件命名爲: 「demo.test.exp0.tif」 ,格式只有一種 「TIFF」 可選。字體

    • tif文面命名格式:[lang].[fontname].exp[num].tif
    • lang:是語言,fontname:是字體,num:爲自定義數字。
    • 好比咱們要訓練自定義字庫 demo,字體名 test,那麼咱們把圖片文件命名爲 demo.test.exp0.tif

第二步:生成box文件spa


  • 在上一步驟生成的 .tif 文件所在目錄下打開命令行程序,執行下面命令,執行完以後會生成 .box文件, .BOX文件爲Tessercat識別出的文字和其座標
  • 命令:tesseract demo.test.exp0.tif demo.test.exp0 -l chi_sim -psm 7 batch.nochop makebox

第三步:矯正.box文件的錯誤.net


  • .box文件記錄了每一個字符在圖片上的位置和識別出的內容,訓練前須要使用jTessBoxEditor調整字符的位置和內容。(注:圖片dpi > 300 時效果更好)
  • 打開jTessBoxEditor點擊Box Editor ->Open,打開步驟2中生成的 .tif,會自動關聯到 .box 文件,這兩文件要求在同一目錄下。調整完點擊「save」保存修改。

第四步:生成font_properties文件(該文件沒有後綴名)命令行


  • 執行命令,會在當前目錄生成font_properties文件,命令:echo test 0 0 0 0 0 >font_properties
  • 執行完成以後,在當前文件夾下生成font_properties文件
  • 也能夠手動在該文件夾下創建一個名爲 「font_properties」 的文件,這個文件沒有後綴名稱,輸入內容 「font 0 0 0 0 0」 , 表示字體 font 的粗體、傾斜等共計5個屬性全都設置爲0。注意 : 這裏輸入的 「font」 名稱必須與 「demo.test.exp0.box」 中兩個點號之間的 「test」 名稱保持一致3d

第五步:生成.tr訓練文件


  • 執行生成 demo.test.exp0.tr 文件,命令:tesseract demo.test.exp0.tif demo.test.exp0 nobatch box.train

第六步:生成字符集文件


  • 執行命令,生成一個名爲「unicharset」的文件;命令:unicharset_extractor demo.test.exp0.box

第七步:生成shape文件


  • 執行命令,生成 shapetable 和 demo.unicharset 兩個文件。命令:shapeclustering -F font_properties -U unicharset -O demo.unicharset demo.test.exp0.tr

第八步:生成聚字符特徵文件


  • 執行命令,會生成 inttemp、pffmtable、shapetable和demo.unicharset四個文件。命令:mftraining -F font_properties -U unicharset -O demo.unicharset demo.test.exp0.tr

第九步:生成字符正常化特徵文件


  • 執行命令,生成 normproto 文件。命令:cntraining demo.test.exp0.tr

第十步:文件重命名


  • 從新命名inttemp、pffmtable、shapetable和normproto這四個文件的名字爲[lang].xxx。這裏修改成demo.inttemp、demo.pffmtable、demo.shapetable和demo.normproto

 

rename normproto demo.normproto rename inttemp demo.inttemp rename pffmtable demo.pffmtable rename shapetable demo.shapetable rename unicharset demo.unicharset

 

 

 

第十一步:合併訓練文件


  • 執行下面命令,會生成demo.traineddata文件。命令:combine_tessdata demo.
  • 將生成的「demo.traineddata」語言包文件複製到Tesseract-OCR 安裝目錄下的tessdata文件夾中,就可使用訓練生成的語言包進行圖像文字識別了。 

測試


import pytesseract from PIL import Image as img class Languages: CHS = 'chi_sim' CHT = 'chi_tra' ENG = 'eng' DM = 'demo' text = pytesseract.image_to_string(img.open('demo.jpg'), lang=Languages.DM) print(text)
  •  

參考資料:


相關文章
相關標籤/搜索