def gen_captcha_text_and_image(iter): image = ImageCaptcha(width=captcha_params.get_width(), height=captcha_params.get_height(), font_sizes=[30]) #print(image) #captcha_text = random_text() captcha_text = "oO" + random_text() #proof that "o" and "O" looks the same path = '../images/' #go up one directory then create images folder if os.path.exists( path) == False: # if the folder is not existed, create it os.mkdir(path) captcha = image.generate(captcha_text) print('c: ', captcha) # naming rules: num(in order)+'_'+'captcha text'.include num is for avoiding the same name image.write(captcha_text, path + str(iter) + '_' + captcha_text + '.png') captcha_image = Image.open(captcha) captcha_image = np.array(captcha_image) return captcha_text, captcha_image
import h5py from keras.models import model_from_json from keras.utils import plot_model from keras.preprocessing.image import ImageDataGenerator import matplotlib from matplotlib import pyplot as plt batch_size = 512 nb_epoch = 100 step_epoch = 7 MAX_CAPTCHA = captcha_params.get_captcha_size() CHAR_SET_LEN = captcha_params.get_char_set_len() # input image dimensions img_rows, img_cols = captcha_params.get_height(), captcha_params.get_width() # number of convolutional filters to use nb_filters1 = 32 nb_filters2 = 64 nb_filters3 = 64 # size of pooling area for max pooling pool_size = (2, 2) # convolution kernel size kernel_size = (3, 3) # the data, shuffled and split between train and test sets (X_train, Y_train), (X_test, Y_test) = load_data(tol_num=3425, train_num=3082)
# The full neural network code! ############################### import keras from load_data import * import captcha_params from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.utils import np_utils from keras import backend as K (x_train, y_train), (x_test, y_test) = load_data(tol_num=captcha_params.TOTAL_NUM, train_num=captcha_params.TRAIN_NUM) # input image dimensions img_rows, img_cols = captcha_params.get_height(), int( captcha_params.get_width() / captcha_params.CAPTCHA_NB_LETTERS) MAX_CAPTCHA = captcha_params.get_captcha_size() CHAR_SET_LEN = captcha_params.get_char_set_len() batch_size = captcha_params.BATCH_SIZE nb_classes = MAX_CAPTCHA * CHAR_SET_LEN nb_epoch = captcha_params.EPOCH # number of convolutional filters to use nb_filters = 32 # size of pooling area for max pooling pool_size = (2, 2) # convolution kernel size kernel_size = (3, 3)
import os from PIL import Image import numpy as np import random import captcha_params np.random.seed(177) MAX_CAPTCHA = captcha_params.get_captcha_size() CHAR_SET_LEN = captcha_params.get_char_set_len() CHAR_SET = captcha_params.get_char_set() Y_LEN = captcha_params.get_y_len() height = captcha_params.get_height() width = captcha_params.get_width() # return the index of the max_num in the array def get_max(array): max_num = max(array) for i in range(len(array)): if array[i] == max_num: return i def get_text(array): text = [] max_num = max(array) for i in range(len(array)):
# The full neural network code! ############################### import keras from load_data import * import captcha_params from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.utils import np_utils from keras import backend as K (x_train, y_train), (x_test, y_test) = load_data(tol_num = captcha_params.TOTAL_NUM,train_num = captcha_params.TRAIN_NUM) # input image dimensions img_rows, img_cols = captcha_params.get_height(), int(captcha_params.get_width() / captcha_params.CAPTCHA_NB_LETTERS) MAX_CAPTCHA = captcha_params.get_captcha_size() CHAR_SET_LEN = captcha_params.get_char_set_len() batch_size = captcha_params.BATCH_SIZE nb_classes = MAX_CAPTCHA*CHAR_SET_LEN nb_epoch = captcha_params.EPOCH # number of convolutional filters to use nb_filters = 32 # size of pooling area for max pooling pool_size = (2, 2) # convolution kernel size kernel_size = (3, 3) if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)