def random_captcha_text(char_set=captcha_params.get_char_set(), captcha_size=captcha_params.get_captcha_size()): captcha_text = [] for i in range(captcha_size): c = random.choice(char_set) captcha_text.append(c) return captcha_text
def decodeLabel2(resu): for i in range(len(resu)): text = '' predict2 = [] for j in range(MAX_CAPTCHA): char_index = get_max(predict[i,CHAR_SET_LEN*j:(j+1)*CHAR_SET_LEN]) char = captcha_params.get_char_set()[char_index] predict2.append(char) text = text.join(predict2) print(text)
def random_text(characters=captcha_params.get_char_set(), captcha_size=captcha_params.get_captcha_size()): captcha_text = [] for i in range(captcha_size): char = random.choice(characters) captcha_text.append(str(char)) captchaTextNoSpace = ''.join(captcha_text) count_char(captchaTextNoSpace) print('no space') print(captchaTextNoSpace) return captchaTextNoSpace
def predict_from_img(self, img): X_test = get_x_input_from_image(img) predict = self.model.predict(X_test) text = '' for i in range(X_test.shape[0]): predict2 = [] for j in range(MAX_CAPTCHA): char_index = get_max(predict[i, CHAR_SET_LEN * j:(j + 1) * CHAR_SET_LEN]) char = captcha_params.get_char_set()[char_index] predict2.append(char) text = text.join(predict2) return text
def predict_from_img(self, img): text = '' for part in range(captcha_params.get_captcha_nb_letters() - 1): # -1 since last is a border if (part == 0): continue # part °0 = border , ignored x_test = get_x_input_from_image(img, part) predict = self.model.predict(x_test) # print (predict) # decodeLabel(predict) for i in range(x_test.shape[0]): true = [] predict2 = [] for j in range(MAX_CAPTCHA): char_index = get_max(predict[i, CHAR_SET_LEN * j:(j + 1) * CHAR_SET_LEN]) char = captcha_params.get_char_set()[char_index] predict2.append(char) text = text + text.join(predict2) return text
from captcha.image import ImageCaptcha # pip install captcha import numpy as np import matplotlib.pyplot as plt # show captcha from PIL import Image import random import os import captcha_params import string import collections import plotly.plotly as py #ordered dict #0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ character_set = captcha_params.get_char_set() #d={} character_count = collections.OrderedDict() print(" Char Set: ", character_set) for c in character_set: character_count[c] = 0 #0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ #counts the occurences of all characters (in order) print("Char Count: ", character_count) def count_char(captcha): for char in captcha: character_count[char] += 1 def random_text(characters=captcha_params.get_char_set(), captcha_size=captcha_params.get_captcha_size()):
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)):
model = load_model.get_model(input_shape) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) score = model.evaluate(X_test, Y_test, verbose=0) predict = model.predict(X_test, batch_size=batch_size, verbose=0) # calculate the accuracy with the test data acc = 0 expected_str = [] predicted_str = [] labels = captcha_params.get_char_set() for i in range(X_test.shape[0]): # Verify old captch if i % 5 == 0 and i >= 5: if expected_str[i - 5:i] == predicted_str[i - 5:i]: acc += 1 if i % 5 == 0 and i >= 5 and i <= 50: print(i, ' true: ', expected_str[i - 5:i]) print(i, ' predict: ', predicted_str[i - 5:i]) # Find out the captcha size len_captcha = MAX_CAPTCHA for j in reversed(range(MAX_CAPTCHA)): char_vec_true = Y_test[i, CHAR_SET_LEN * j:(j + 1) * CHAR_SET_LEN] char_true_idx = get_max(char_vec_true)