tesseract 安裝與訓練(圖像識別)

 

代碼託管:https://github.com/tesseract-ocr/tesseractjava

環境:win10 git

安裝版本:tesseract-ocr-setup-3.02.02.exegithub

基本使用命令:工具

tesseract number.jpg result -l eng -psm 7字體

訓練url

下載使用JtessBoxEditor,該工具須要安裝java vm運行。
1.合併圖像
將須要識別的圖片轉換爲tif格式,合併到一塊兒。
點擊tools——》merage tiff——》選中全部圖片保存爲LAN.new.exp0.tif
tesseract [lang].[fontname].exp[num].tif [lang].[fontname].exp[num] 
3.文字糾正(tiff,box須要在同一目錄)
用jtessboxeditor加載box文件,在box editor——》box coordinates裏修改文件.最後保存box文件.spa

一、tesseract image.MyFont.exp0.tif image.MyFont.exp0 -l chi_sim batch.nochop makebox
該步驟會生成一個image.MyFont.exp0.box文件
把tif文件和box文件放在同一目錄,用jTessBoxEditor.jar打開tif文件,而後根據實際狀況修改box文件
二、tesseract image.MyFont.exp0.tif image.MyFont.exp0 nobatch box.train
該步驟生成一個image.MyFont.exp0.tr文件
三、unicharset_extractor image.MyFont.exp0.box
該步驟生成一個unicharset文件
四、新建一個font_properties文件
裏面內容寫入MyFont 0 0 0 0 0 表示默認普通字體
五、運行命令
shapeclustering -F font_properties -U unicharset image.MyFont.exp0.tr
mftraining -F font_properties -U unicharset -O image.unicharset image.MyFont.exp0.tr
cntraining image.MyFont.exp0.tr
六、把目錄下的unicharset、inttemp、pffmtable、shapetable、normproto這五個文件前面都加上image.
七、執行combine_tessdata image.
而後把image.traineddata放到tessdata目錄
八、用新的字庫對圖片進行分析
tesseract test.tif output -l imagecode

批處理orm

echo 執行改批處理前先要目錄下建立font_properties文件  
echo Run Tesseract for Training..  
echo 該步驟生成一個image.MyFont.exp0.tr文件  
tesseract image.MyFont.exp0.tif image.MyFont.exp0 nobatch box.train  

echo 該步驟生成一個unicharset文件 
unicharset_extractor.exe num.font.exp0.box
rem 新建一個font_properties文件
echo MyFont 0 0 0 0 0 > font_properties
shapeclustering -F font_properties -U unicharset image.MyFont.exp0.tr  
mftraining -F font_properties -U unicharset -O image.unicharset image.MyFont.exp0.tr
  
echo Clustering..  
cntraining.exe image.MyFont.exp0.tr  
  
echo 重命名文件 
rename normproto image.normproto  
rename inttemp image.inttemp  
rename pffmtable image.pffmtable  
rename shapetable image.shapetable   
  
echo 合併文件 Tessdata..  
combine_tessdata.exe num.

 

 

# -*- coding:utf-8 -*-
import pytesseract
from PIL import Image
import requests
import os
# 驗證碼識別
# 下載驗證碼 @輸入下載數量

def code_down(num):
    imgurl = 'https/CImages'
    for i in range(num):
        data=requests.get(imgurl)
        name=str(i)
        download_img(data.content,name)

def download_img(imgdata,name):
    with open('./code_img/'+name+'.jpg','wb') as f:
        f.write(imgdata)

def code_ocr(num):
    for i in range(num):
        image=Image.open('./grey/'+"grey"+str(i)+'.jpg')
        code=pytesseract.image_to_string(image,config='-psm 7')
        print("index:%d code:%s"%(i,code))
def img_grey(num):
    for i in range(num):
        image=Image.open('./code_img/'+str(i)+'.jpg')
        grey=image.convert('L')
        grey.save("./grey/grey"+str(i)+'.jpg')

# 圖片批量下載
# code_down(100)
# 圖片識別
code_ocr(20)
# 圖片灰度處理
# img_grey(86)
相關文章
相關標籤/搜索