def get_next_batch(batch_size=128): batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH]) batch_y = np.zeros([batch_size, CAPTCHA_TEXT_LEN * CHAR_SET_LEN]) for i in range(batch_size): text, image = gen_captcha() # log(i, text) image = convert2gray(image) batch_x[i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text) return batch_x, batch_y
def test_crack_captcha(count): output = crack_captcha_cnn() saver = tf.train.Saver() correct = 0 for i in range(count): text, image = gen_captcha() image = convert2gray(image) image = image.flatten() / 255 predict_text = crack_captcha(image, output, saver) if text.lower() == predict_text.lower(): correct = correct + 1 logger.info("正确: {} 预测: {}".format(text, predict_text)) logger.info('总数 ' + str(i) + " 正确 " + str(correct) + ' 准确率:' + str(correct / (i + 1)))
from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True from logger import log # 当前文件所在目录 root_path = os.path.split(os.path.realpath(__file__))[0] # 训练数据数字基本图片目录 MODEL_PATH = root_path + '/models/' CHAR_SET_LEN = len(CHAR_SET) log("验证码文本集合长度 {0} 集合:{1}".format(CHAR_SET_LEN, CHAR_SET)) CAPTCHA_TEXT, IMAGE = gen_captcha() log("验证码图像channel: {0}".format(IMAGE.shape)) # (60, 160, 3) # 图像大小 IMAGE_HEIGHT, IMAGE_WIDTH, a = IMAGE.shape CAPTCHA_TEXT_LEN = len(CAPTCHA_TEXT) log("验证码文本字符数 ".format(CAPTCHA_TEXT_LEN)) # 验证码最长4字符 #################################################################### X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH]) Y = tf.placeholder(tf.float32, [None, CAPTCHA_TEXT_LEN * CHAR_SET_LEN]) keep_prob = tf.placeholder(tf.float32) # dropout # 把彩色图像转为灰度图像(色彩对识别验证码没有什么用) def convert2gray(img): if len(img.shape) > 2: