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 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 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
def captcha2text(image_list, 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('.')) 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] builder = tf.saved_model.builder.SavedModelBuilder("./saved_model/") builder.add_meta_graph_and_variables(sess, ['cnnCaptcha']) builder.save() return text_list
def captcha2text(image_list, height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): # disable the tensor flow 2 eager execution function tf.compat.v1.disable_eager_execution() 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('.')) print(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