-
Notifications
You must be signed in to change notification settings - Fork 9
/
run_inference.py
executable file
·78 lines (61 loc) · 2.47 KB
/
run_inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Generate transcription for images."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import time
import cv2
import tensorflow as tf
from autocorrect import spell
import configuration
import inference_wrapper
FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_string("checkpoint_path", "checkpoints/full_2_dropout",
"Model checkpoint file or directory containing a "
"model checkpoint file.")
tf.flags.DEFINE_string("input_files", "test_images/*.jpg",
"File pattern or comma-separated list of file patterns "
"of image files.")
def main(_):
# Build the inference graph.
g = tf.Graph()
with g.as_default():
infer = inference_wrapper.InferenceWrapper()
restore_fn = infer.build_graph_from_config(configuration.ModelConfig(),
FLAGS.checkpoint_path)
g.finalize()
filenames = []
for file_pattern in FLAGS.input_files.split(","):
filenames.extend(tf.gfile.Glob(file_pattern))
tf.logging.info("Running caption generation on %d files matching %s",
len(filenames), FLAGS.input_files)
with tf.Session(graph=g) as sess:
# Load the model from checkpoint.
restore_fn(sess)
# Initialize the vocabulary lookup table
infer.model.vocab_table.init.run(session=sess)
filenames.sort()
# Predict
for filename in filenames:
with tf.gfile.GFile(filename, "r") as f:
# Predict transcription
tic = time.time()
image = f.read()
pred_chars = infer.inference_step(sess, image)[0][0]
pred_word = "".join([item for item in pred_chars])
auto_correct_word = spell(pred_word)
toc = time.time()
# Print out the result
print("Prediction for image %s in %.3f ms" %
(os.path.basename(filename), (toc - tic) * 1000))
print("predicted word: %s" % pred_word)
print("auto correct word: %s" % auto_correct_word)
print("*" * 50)
# Show image
cv_img = cv2.imread(filename)
cv2.imshow('image', cv_img)
k = cv2.waitKey(0)
if k == ord('n'):
cv2.destroyAllWindows()
if __name__ == "__main__":
tf.app.run()