def test_crack_captcha(test_step): output = crack_captcha_cnn() saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('./models')) predict = tf.argmax( tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) sum_correct = 0 # for _ in range(test_step): for g_image in gen_image('./captcha_image'): text_source, image = g_image # print(text_source, image) image = convert2gray(image) captcha_image = image.flatten() / 255 text_list = sess.run(predict, feed_dict={ X: [captcha_image], keep_prob: 1 }) # text_list = sess.run(predict, feed_dict={X: np.reshape(captcha_image,(-1,4000)), keep_prob: 1}) text = text_list[0].tolist() vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) i = 0 for n in text: vector[i * CHAR_SET_LEN + n] = 1 i += 1 predict_text = vec2text(vector) print("正确: {} 预测: {}".format(text_source, predict_text)) if text_source.lower() == predict_text.lower(): sum_correct += 1 print('sum_correct:%s' % sum_correct) print('成功率:%s' % (sum_correct / test_step))
def crack_captcha(): output = crack_captcha_cnn() saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('models')) predict = tf.argmax( tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) count_out = 0 right_number = 0 while (True): root_text, image = gen_captcha_text_and_image() image = convert2gray(image) captcha_image = image.flatten() / 255 text_list = sess.run(predict, feed_dict={ X: [captcha_image], keep_prob: 1 }) text = text_list[0].tolist() vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) i = 0 for n in text: vector[i * CHAR_SET_LEN + n] = 1 i += 1 predict_text = vec2text(vector) print("正确: {} 预测: {}".format(root_text, predict_text)) if count_out == 1000: break count_out += 1
def crack_captcha(captcha_image): output = crack_captcha_cnn() saver = tf.train.Saver() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) with tf.Session() as sess: saver.restore(sess, "./Model/model.ckpt") text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1}) text = text_list[0].tolist() return text
def crack_captcha(captcha_image): output = crack_captcha_cnn() saver = tf.train.Saver() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) with tf.Session() as sess: saver.restore(sess, "./Model/model.ckpt") text_list = sess.run(predict, feed_dict={ X: [captcha_image], keep_prob: 1 }) text = text_list[0].tolist() return text
def crack_captcha(captcha_image): output = crack_captcha_cnn() saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('./models/')) predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1}) text = text_list[0].tolist() vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) i = 0 for n in text: vector[i * CHAR_SET_LEN + n] = 1 i += 1 return vec_to_text(vector)
def predict_result(captcha_image): output = crack_captcha_cnn() saver = tf.train.Saver() # tf.reset_default_graph() with tf.Session() as sess: saver.restore(sess, "F:/CNN_2/model_3/crack_capcha.model-2000") predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1}) # text_list = sess.run(predict, feed_dict={X: [captcha_image]}) text = text_list[0].tolist() vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) i = 0 for n in text: vector[i * CHAR_SET_LEN + n] = 1 i += 1 return vec2text(vector)
from flask import Flask, request, json import tensorflow as tf from train import crack_captcha_cnn, convert2gray, X, keep_prop, vec2text import base64 import numpy as np import re app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' output = crack_captcha_cnn() pre = tf.argmax(tf.reshape(output, [-1, 6, 36]), 2) sess = tf.Session() saver = tf.train.Saver() saver.restore(sess, "../zf_yzm_train/capcha.model-5200") @app.route('/yzm', methods=['POST', 'GET']) def test(): img_base64 = request.form['img_base64'] # return img_base64 img_base64 = re.sub('^data:image/.+;base64,', '', img_base64) img_base64 = base64.b64decode(img_base64) buffer = cStringIO.StringIO(img_base64) img = Image.open(buffer) img = np.array(img)