Esempio n. 1
0
def run_predict():
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        parser = argparse.ArgumentParser()
        parser.add_argument('--checkpoint_dir',
                            type=str,
                            default='./captcha_train',
                            help='Directory where to restore checkpoint.')
        parser.add_argument('--captcha_dir',
                            type=str,
                            default='./captcha_dir',
                            help='Directory where to get captcha images.')
        FLAGS, unparsed = parser.parse_known_args()
        input_images, input_filenames = input_data(FLAGS.captcha_dir)
        images = tf.constant(input_images)
        logits = captcha.inference(images, keep_prob=1)
        result = captcha.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session()
        saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))

        recog_result = sess.run(result)
        sess.close()
        text = one_hot_to_texts(recog_result)
        total_count = len(input_filenames)
        true_count = 0.
        if total_count == 1:
            return str(text[0])
Esempio n. 2
0
def run_predict(img_data):
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        input_filenames = ''
        # input_images, input_filenames = input_dir_data(FLAGS.captcha_dir)
        input_images = input_img_data(img_data)
        images = tf.constant(input_images)
        logits = captcha.inference(images, keep_prob=1)
        result = captcha.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session()
        saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
        print(tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
        recog_result = sess.run(result)
        sess.close()
        text = one_hot_to_texts(recog_result)
        total_count = len(input_filenames)
        true_count = 0.
        if total_count != 0:
            for i in range(total_count):
                print('image ' + input_filenames[i] + " recognize ----> '" +
                      text[i] + "'")
                if text[i] in input_filenames[i]:
                    true_count += 1
            precision = true_count / total_count
            print('%s true/total: %d/%d recognize @ 1 = %.3f' %
                  (datetime.now(), true_count, total_count, precision))
        elif total_count == 0:
            print(text[0])
            return text[0]
Esempio n. 3
0
def predict(image_file):
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        input_images = input_data(image_file)
        images = tf.constant(input_images)
        logits = captcha.inference(images, keep_prob=1)
        result = captcha.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session()
        saver.restore(sess, tf.train.latest_checkpoint('./captcha_train'))
        recog_result = sess.run(result)
        sess.close()
        text = one_hot_to_texts(recog_result)
        return text[0]
Esempio n. 4
0
def run_predict(image_path):
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        images, files = input_data(image_path)
        images = tf.constant(images)
        logits = captcha_model.inference(images, keep_prob=1)
        result = captcha_model.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session()
        print(tf.train.latest_checkpoint('./models'))
        saver.restore(sess, tf.train.latest_checkpoint('./models'))
        recog_result = sess.run(result)
        sess.close()
        print("recog_result: %s" % recog_result)
        text = one_hot_to_text(recog_result[0])
        print("recog text is: %s" % text)
    return text
Esempio n. 5
0
def run_predict(image_base64):
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        input_images, input_filenames = input_data(image_base64)
        images = tf.constant(input_images)
        logits = captcha.inference(images, keep_prob=1)
        result = captcha.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session()
        saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
        # print(tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
        recog_result = sess.run(result)
        sess.close()
        text = one_hot_to_texts(recog_result)
        total_count = len(input_filenames)
        result = ""
        for i in range(total_count):
            result = text[i]
        return result
Esempio n. 6
0
def run_predict(url_yzm):
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        input_images = [getImage(url_yzm)]
        images = tf.constant(input_images)
        logits = captcha.inference(images, keep_prob=1)
        result = captcha.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session()
        saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir))
        print(tf.train.latest_checkpoint(checkpoint_dir))
        recog_result = sess.run(result)
        sess.close()
        text = one_hot_to_texts(recog_result)
        total_count = len(input_images)
        true_count = 0.
        for i in range(total_count):
            print('image ' + input_images[i] + " recognize ----> '" + text[i] +
                  "'")
Esempio n. 7
0
def run_predict():
    with tf.Graph().as_default():
        input_images, input_filenames = input_data(
            FLAGS.captcha_dir)  #得到文件夹内所有照片和文件名
        epoches = len(input_images) // Batch_size
        offset = len(input_images) - (epoches - 1) * Batch_size
        images = tf.placeholder(tf.float32,
                                [Batch_size, IMAGE_HEIGHT * IMAGE_WIDTH],
                                name='input')
        logits = captcha.inference(images, keep_prob=1, is_training=True)
        result = captcha.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session()
        saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
        for each in range(epoches):
            feed_dict = input_images[each * Batch_size:min(
                (each + 1) * Batch_size, len(input_images))]

            recog_result = sess.run(result, feed_dict={images: feed_dict})

            text = one_hot_to_texts(recog_result)
            total_count = len(feed_dict)
            true_count = 0.
            for i in range(total_count):
                print('image ' + input_filenames[i + each * Batch_size] +
                      " recognize ----> '" + text[i] + "'")
                with open('recognize.txt', 'a') as f:
                    f.write('image ' + input_filenames[i + each * Batch_size] +
                            'recognize ----> ' + text[i] + '\n')
                if text[i] in input_filenames[i + each * Batch_size]:
                    true_count += 1
            precision = true_count / total_count

            print(
                '%s epoch: %d ,true/total: %d/%d recognize @ = %.3f' %
                (datetime.now(), each + 1, true_count, total_count, precision))
        sess.close()
Esempio n. 8
0
def run_predict():
    with tf.Graph().as_default(), tf.device('/gpu:0'):
        input_images, input_filenames = input_data(FLAGS.captcha_dir)
        images = tf.constant(input_images)
        logits = captcha.inference(images, keep_prob=1.0)
        result = captcha.output(logits)
        saver = tf.train.Saver()
        sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
        print(tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
        saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
        recog_result = sess.run(result)
        sess.close()
        text = one_hot_to_texts(recog_result)
        print('text: ', text)
        total_count = len(input_filenames)
        true_count = 0
        for i in range(total_count):
            print('image ' + input_filenames[i] + " recognize ----> '" +
                  text[i] + "'")
            if text[i] in input_filenames[i]:
                true_count += 1
        precision = true_count / total_count
        print('%s true/total: %d/%d recognize @ 1 = %.3f' %
              (datetime.now(), true_count, total_count, precision))