tesseract-orc训练 结合python3图像识别验证码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chouzhou9701/article/details/82587833

前言

其实就是用到tesseract-ocr这个引擎来识别,只不过我们需要做一些在此之前的工作
将图片用pillow进行初步处理,将图片中的验证码显示的清晰一些,关于这些教程可以查看我的另一篇文章(现在还没写)
然后用tesseract-ocr将处理完的图片进行识别,当然不训练tesseract-ocr是不行的,还需要对其进行训练,后面我会说明怎么训练

windows 平台

1.安装

安装tesseract-ocr

地址: 点我点我!
注意:如果你要识别中文的需要在安装时把中文包勾选上

安装python第三方库

我这里用的是python3,python3怎么安装我就不多说了。
安装必要的 python第三方库:

pip install Pillow pytesseract -i https://mirrors.aliyun.com/pypi/simple/

Pillow是一个python图像处理的第三方库
pytesseract 我的理解是调用刚才安装的tesseract-ocr的一个接口吧

安装训练矫正的工具(jTessBoxEditorFX)

地址:点我点我!

2.编写python处理图片函数

二值化
from PIL import Image
from pytesseract import *
im = Image.open('1.jpg') #用pil打开这个图片

im = im.convert('L')
im = im.point(lambda x: 0 if x<100 else x>=100, '1') # 二值化 100为分割灰度的点(阀值),二值化就是将图片的颜色转换成非黑即白的图片
im.show() #查看图片    

图片资源
这里写图片描述
经过上述处理应该这样的生成图片
这里写图片描述

如果显示的图片中线没有去掉,可以调整阀值来去掉线

去除噪点

把图片中的黑点去掉

def getPixel(image,x,y):
    L = image.getpixel((x,y)) #获取当前像素点的像素
    if L == 0:  #判读此像素点是否为黑,因为如果是白的就没必要处理了
        nearDots = 0  #初始化记录周围有没有黑像素数量的值
        #判断周围像素点
        if L - image.getpixel((x - 1,y - 1)):
            nearDots += 1
        if L - image.getpixel((x - 1,y)):
            nearDots += 1
        if L - image.getpixel((x - 1,y + 1)):
            nearDots += 1
        if L - image.getpixel((x,y - 1)):
            nearDots += 1
        if L - image.getpixel((x,y + 1)):
            nearDots += 1
        if L - image.getpixel((x + 1,y - 1)):
            nearDots += 1
        if L - image.getpixel((x + 1,y)):
            nearDots += 1
        if L - image.getpixel((x + 1,y + 1)):
            nearDots += 1
        if nearDots ==8: #这里如果周围八个全是白点那么就返回一个白点,实现去黑点的操作
            return  1
         #这里主要是有俩个黑点连在一起,所有周围会有七个黑点扩大范围进一步判断
        elif nearDots ==7:
            nearDots = 0
            if L - image.getpixel((x - 2,y - 2)):
                nearDots += 1
            if L - image.getpixel((x - 2,y)):
                nearDots += 1
            if L - image.getpixel((x - 2,y + 2)):
                nearDots += 1
            if L - image.getpixel((x,y - 2)):
                nearDots += 1
            if L - image.getpixel((x,y + 2)):
                nearDots += 1
            if L - image.getpixel((x + 2,y - 2)):
                nearDots += 1
            if L - image.getpixel((x + 2,y)):
                nearDots += 1
            if L - image.getpixel((x + 2,y + 2)):
                nearDots += 1
            if nearDots == 8:
                return 1 #返回白点
            else:
                return 0 #返回黑点
    else:
        return 1

def clearNoise(image):
    draw = ImageDraw.Draw(image)
    #循环遍历每个像素点
    for x in range(0,image.size[0]):
        for y in range(0,image.size[1]):
            color = getPixel(image,x,y)
            draw.point((x,y),color)


#将上一步处理完成的im对象传给clearNoise()函数
im = clearNoise(im)
im.show()

代码挺简单的应该可以看懂
不出意外的话处理完成的图片变成这样:
这里写图片描述

处理到这一步就已经可以了,当然你也可以进行进一步处理,例如局部放大

调用 tesseract-ocr识别图片

在上面的代码中加入

# lang只用哪个库来识别 默认有个eng库,config 指代识别单行还是多行-psm 7只的是单行
result = pytesseract.image_to_string(im,lang='eng',config="-psm 7")
print(result)

不出意外的话识别结果应该是:o 3 o 4
但是我图像都已经处理到这一步了,怎么还识别出错??
答:因为你没有训练!

3.训练tesseract-ocr

合成tif文件

打开安装的 Jtessboxedit ,点击tools——Merge TiFF然后选中经过处理的验证码图片(因为最终识别的是经过python处理完的,所以也拿处理完成的验证码来训练在原函数中加入im.save(保存路径)即可保存),随便找到另一个地方生成tif,命名为num.font.exp0.tif保存
注意:这里的文件名不是瞎起的,文件名有要求的,取和我相同的名字就行了,因为后面运行的命令都是依靠这个文件名的

生成box文件

生成好后,进入tif文件所在的文件夹
运行命令

tesseract num.font.exp0.tif num.font.exp0 -psm 7 batch.nochop makebox

在文件夹就会生成一个 .box的文件

训练

然后在jtessboxedit中Box Editor选项卡中点击open按钮打开上一步生成的tif文件
这里写图片描述
如果位置不对就调整x y,w,h的值,字符不对就修改字符,这里我把俩个o修改成0
然后save

生成训练的库文件

进入到tif和box所在的文件夹
运行以下命令

tesseract.exe num.font.exp0.tif num.font.exp0 -psm 7 nobatch box.train 
unicharset_extractor.exe num.font.exp0.box 
echo font 0 0 0 0 0 > font_properties.txt
mftraining -F font_properties.txt -U unicharset -O num.unicharset num.font.exp0.tr 
cntraining.exe num.font.exp0.tr 
rename normproto num.normproto 
rename inttemp num.inttemp 
rename pffmtable num.pffmtable 
rename shapetable num.shapetable  
combine_tessdata.exe num. 

把生成的num.traineddata这个文件拷贝到 你的C:\Program Files (x86)\Tesseract-OCR\tessdata下,大工告成!

4.用训练好的库来识别python处理后的图片

只需修改一下代码即可

asd = pytesseract.image_to_string(image,lang='num',config="-psm 7")#将eng修改为你训练好的库,也就是num

猜你喜欢

转载自blog.csdn.net/chouzhou9701/article/details/82587833