代码来自于腾讯云的AI实验,由于上面的实验所用的的验证码图片很少,所以用其中的图片来测试时准确率很高,但是新的不同类型的图片准确率低的吓人,于是自己就加了个for循环增加图片数量,希望可以得到更多不同的验证码图片以此来提高对新验证码的识别准确率。
代码如下:在原来的代码上加上一个for循环就可以了,循环的次数就是你得到的图片的数量
首先要先安装captcha库才能使用,安装captcha库直接pip install captcha就行了
#-*- coding:utf-8 -*-
from captcha.image import ImageCaptcha
from PIL import Image
import numpy as np
import random
import string
class generateCaptcha():
def __init__(self,
width = 160,#验证码图片的宽
height = 60,#验证码图片的高
char_num = 4,#验证码字符个数
characters = string.digits + string.ascii_uppercase + string.ascii_lowercase):#验证码组成,数字+大写字母+小写字母
self.width = width
self.height = height
self.char_num = char_num
self.characters = characters
self.classes = len(characters)
def gen_captcha(self,batch_size = 50):
# X: 一个mini-batch的训练数据,其shape[batch_size,heright,width,1],
X = np.zeros([batch_size,self.height,self.width,1])
img = np.zeros((self.height,self.width),dtype=np.uint8)
# Y: X中的每个训练数据属于哪一类验证码,其形状为[batch_size,class],
# 对验证码中每个字符进行one-hot编码,所以class大小为4*64
Y = np.zeros([batch_size,self.char_num,self.classes])
image = ImageCaptcha(width = self.width,height = self.height)
while True:
for i in range(batch_size):
captcha_str = ''.join(random.sample(self.characters,self.char_num))
img = image.generate_image(captcha_str).convert('L')
img = np.array(img.getdata())
X[i] = np.reshape(img,[self.height,self.width,1])/255.0
for j,ch in enumerate(captcha_str):
Y[i,j,self.characters.find(ch)] = 1
Y = np.reshape(Y,(batch_size,self.char_num*self.classes))
yield X,Y
def decode_captcha(self,y):
y = np.reshape(y,(len(y),self.char_num,self.classes))
return ''.join(self.characters[x] for x in np.argmax(y,axis = 2)[0,:])
def get_parameter(self):
return self.width,self.height,self.char_num,self.characters,self.classes
def gen_test_captcha(self):
image = ImageCaptcha(width = self.width,height = self.height)
captcha_str = ''.join(random.sample(self.characters,self.char_num))
img = image.generate_image(captcha_str)
img.save(captcha_str + '.jpg')
X = np.zeros([1,self.height,self.width,1])
Y = np.zeros([1,self.char_num,self.classes])
#convert函数用于不同模式之间的转换(L)为灰色图像
img = img.convert('L')
img = np.array(img.getdata())
X[0] = np.reshape(img,[self.height,self.width,1])/255.0
for j,ch in enumerate(captcha_str):
Y[0,j,self.characters.find(ch)] = 1
Y = np.reshape(Y,(1,self.char_num*self.classes))
return X,Y
for i in range(2):
g = generateCaptcha()
X,Y = g.gen_test_captcha()
print(X.shape)
print(Y.shape)