用pytesser3识别验证码
一、安装
1、先安装Tesseract-OCR
用于图片文本识别的Tesseract-OCR的安装说明(windows10)
2、安装pytesser3包
用于图片文本识别的pytesser3的安装说明(windows10)
二、分析与代码
代码参考:https://blog.****.net/nwpulei/article/details/8457738
In [1]:
from PIL import Image
import pytesser3
import numpy as np
import matplotlib.pyplot as plt
加载显示验证码图像
In [2]:
img = Image.open('test4.PNG')
plt.imshow(img)
Out[2]:
<matplotlib.image.AxesImage at 0x7fd7b00>
将图像转化灰度图
In [3]:
img_grey = img.convert('L')
plt.imshow(img_grey)
Out[3]:
<matplotlib.image.AxesImage at 0x806d080>
In [4]:
print(img_grey.size)
(60, 22)
分析图像像素值分布
In [5]:
image_np=np.array(img_grey)
print("图像shaep:"+repr(image_np.shape))
print("图像第10行像素值;"+repr(image_np[10]))
图像shaep:(22, 60) 图像第10行像素值;array([227, 195, 184, 247, 240, 122, 81, 80, 249, 245, 98, 108, 226, 194, 188, 245, 247, 243, 237, 169, 250, 251, 242, 188, 88, 191, 202, 168, 198, 245, 241, 238, 192, 198, 94, 88, 179, 203, 227, 172, 202, 179, 180, 251, 70, 85, 70, 183, 185, 79, 58, 190, 180, 205, 180, 195, 234, 188, 227, 252], dtype=uint8)
In [6]:
image_np=image_np.reshape(1,-1) #变换维度,以便后面统计
print("变换后的维度:"+repr(image_np.shape))
#tmp=image_np[(image_np>140) & (image_np<200)]
#print(tmp.shape)
变换后的维度:(1, 1320)
In [7]:
i=0
step=10 #统计步长
tong_ji={} #字典以存放像素值分布统计结果
while i<=255:
tong_ji[str(i)+'-'+str(i+step-1)]=image_np[(image_np>=i) & (image_np<i+step)].shape[0]
i+=10
print("统计结果字典:"+repr(tong_ji))
print("像素总数:"+str(sum(tong_ji.values())))
统计结果字典:{'0-9': 0, '10-19': 0, '20-29': 0, '30-39': 0, '40-49': 0, '50-59': 3, '60-69': 12, '70-79': 35, '80-89': 29, '90-99': 33, '100-109': 21, '110-119': 4, '120-129': 3, '130-139': 0, '140-149': 0, '150-159': 3, '160-169': 33, '170-179': 117, '180-189': 229, '190-199': 148, '200-209': 27, '210-219': 10, '220-229': 44, '230-239': 155, '240-249': 171, '250-259': 243} 像素总数:1320
In [8]:
#画直方图统计
#设置坐标轴名称
plt.xlabel('pixel value')
plt.ylabel('count')
plt.bar(range(0,256,10),tong_ji.values())
Out[8]:
<BarContainer object of 26 artists>
根据统计分析:像素值140左右是一条分水岭,区分出噪点和文字;
然后以140做为阙值,建立过滤索引表,进行图像噪点过滤;
In [9]:
threshold = 140 #阙值设为140
table = [] #转换索引表
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
img_out = img_grey.point(table, '1')
plt.imshow(img_out)
Out[9]:
<matplotlib.image.AxesImage at 0x8168cf8>
最后将处理过的图进行ocr
In [10]:
text = pytesser3.image_to_string(img_grey) # 将图片转成字符串
print(text)
6716