def captcha2text(image_list, height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): """ 验证码图片转化为文本 :param image_list: :param height: :param width: :return: """ x = tf.placeholder(tf.float32, [None, height * width]) keep_prob = tf.placeholder(tf.float32) y_conv = cnn_graph(x, keep_prob, (height, width)) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('model/')) predict = tf.argmax( tf.reshape(y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={ x: image_list, keep_prob: 1 }) vector_list = vector_list.tolist() text_list = [vec2text(vector) for vector in vector_list] return text_list
def captcha2text(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): ''' 验证码图片转化为文本 :param image_list: :param height: :param width: :return: ''' x = tf.placeholder(tf.float32, [None, height * width]) keep_prob = tf.placeholder(tf.float32) y_conv = cnn_graph(x, keep_prob, (height, width)) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, RESTORE_PATH) success = 0 error = 0 for num in range(0, len(os.listdir(IMAGE_TEST_PATH))): text, image = read_dir_image_for_position(num, IMAGE_TEST_PATH) image = convert2gray(image) image = image.flatten() / 255 image = [image] predict = tf.argmax( tf.reshape( y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={x: image, keep_prob: 1}) vector_list = vector_list.tolist() pre_text = [vec2text(vector) for vector in vector_list] if (text == ''.join(pre_text)): success += 1 else: error += 1 print('Label:', text, ' Predict:', pre_text) print('total:', success + error, ',success:', success, ',error:', error)
def image2text(image, height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): pre_text = '1111' tf.reset_default_graph() x = tf.placeholder(tf.float32, [None, height * width]) keep_prob = tf.placeholder(tf.float32) y_conv = cnn_graph(x, keep_prob, (height, width)) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, RESTORE_PATH) image = convert2gray(image) image = image.flatten() / 255 image = [image] predict = tf.argmax( tf.reshape(y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={x: image, keep_prob: 1}) vector_list = vector_list.tolist() pre_text = [vec2text(vector) for vector in vector_list] return pre_text
def captcha2text(image_list, height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): if not isdir('./model'): print('Model directory does not exists.') return x = placeholder(float32, [None, height * width]) keep_prob = placeholder(float32) y_conv = cnn_graph(x, keep_prob, (height, width)) saver = Saver() with Session() as sess: saver.restore(sess, latest_checkpoint('./model/')) predict = argmax(reshape( y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={ x: image_list, keep_prob: 1 }) vector_list = vector_list.tolist() text_list = [vec2text(vector) for vector in vector_list] return text_list
# -*- coding:utf-8 -*- import numpy as np from PIL import Image from util import vec2text, text2vec import tensorflow as tf img = Image.open('./q7wy.jpg') img_data = np.array(img) # if len(img_data) > 2: # img_data = np.mean(img_data, -1) # # img = Image.fromarray(img_data) # img.show() # print(img_data.shape) code = '1234' vec2 = text2vec(code) print(vec2) y = vec2text(vec2) # t = tf.reshape(code, [2, 2]) print(y)