1. 程式人生 > >驗證碼圖片的自動生成

驗證碼圖片的自動生成

    程式碼來自於騰訊雲的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)