tesseract 安装与训练(图像识别)

代码托管:https://github.com/tesseract-ocr/tesseract

环境:win10 

安装版本:tesseract-ocr-setup-3.02.02.exe

基本使用命令:

tesseract number.jpg result -l eng -psm 7

训练

下载使用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文件.

1、tesseract image.MyFont.exp0.tif image.MyFont.exp0 -l chi_sim batch.nochop makebox
该步骤会生成一个image.MyFont.exp0.box文件
把tif文件和box文件放在同一目录,用jTessBoxEditor.jar打开tif文件,然后根据实际情况修改box文件
2、tesseract image.MyFont.exp0.tif image.MyFont.exp0 nobatch box.train
该步骤生成一个image.MyFont.exp0.tr文件
3、unicharset_extractor image.MyFont.exp0.box
该步骤生成一个unicharset文件
4、新建一个font_properties文件
里面内容写入MyFont 0 0 0 0 0 表示默认普通字体
5、运行命令
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
6、把目录下的unicharset、inttemp、pffmtable、shapetable、normproto这五个文件前面都加上image.
7、执行combine_tessdata image.
然后把image.traineddata放到tessdata目录
8、用新的字库对图片进行分析
tesseract test.tif output -l image

批处理

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)
原文地址:https://www.cnblogs.com/maoxianfei/p/7257577.html