def captcha_to_text(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('.')) 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[0]
def multi_test(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): 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('.')) while 1: text, image = get_random_captcha_text_and_image() image = convert2gray(image) image = image.flatten() / 255 image_list = [image] 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] pre_text = text_list[0] flag = u'错误' if text == pre_text: flag = u'正确' print u"实际值(actual):%s, 预测值(predict):%s, 预测结果:%s" % ( text, pre_text, flag, )
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 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) img = convert2gray(img) img = img.flatten() / 255 text_list = sess.run(pre, feed_dict={X: [img], keep_prop: 1}) text = text_list[0].tolist() vector = np.zeros(6 * 36) i = 0 for n in text: vector[i * 36 + n] = 1 i += 1 text = vec2text(vector) res = {"code": 200, "data": text, "msg": None} return json.dumps(res)
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)
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(model_path)) 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 vec2text(vector)