-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_demo.py
executable file
·140 lines (115 loc) · 4.25 KB
/
run_demo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
import os
import time
import datetime
import cv2
import numpy as np
import uuid
import json
import functools
import logging
import collections
import argparse
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def get_predictor(checkpoint_path):
logger.info('loading model')
import tensorflow as tf
import model
from icdar import restore_rectangle
import lanms
from eval import resize_image, sort_poly, detect
input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images')
global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
f_score, f_geometry = model.model(input_images, is_training=False)
variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step)
saver = tf.train.Saver(variable_averages.variables_to_restore())
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
ckpt_state = tf.train.get_checkpoint_state(checkpoint_path)
model_path = os.path.join(checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path))
logger.info('Restore from {}'.format(model_path))
saver.restore(sess, model_path)
def predictor(img):
"""
:return: {
'text_lines': [
{
'score': ,
'x0': ,
'y0': ,
'x1': ,
...
'y3': ,
}
],
'rtparams': { # runtime parameters
'image_size': ,
'working_size': ,
},
'timing': {
'net': ,
'restore': ,
'nms': ,
'cpuinfo': ,
'meminfo': ,
'uptime': ,
}
}
"""
start_time = time.time()
rtparams = collections.OrderedDict()
rtparams['start_time'] = datetime.datetime.now().isoformat()
rtparams['image_size'] = '{}x{}'.format(img.shape[1], img.shape[0])
timer = collections.OrderedDict([
('net', 0),
('restore', 0),
('nms', 0)
])
im_resized, (ratio_h, ratio_w) = resize_image(img)
rtparams['working_size'] = '{}x{}'.format(
im_resized.shape[1], im_resized.shape[0])
start = time.time()
score, geometry = sess.run(
[f_score, f_geometry],
feed_dict={input_images: [im_resized[:,:,::-1]]})
timer['net'] = time.time() - start
boxes, timer = detect(score_map=score, geo_map=geometry, timer=timer)
logger.info('net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'.format(
timer['net']*1000, timer['restore']*1000, timer['nms']*1000))
if boxes is not None:
scores = boxes[:,8].reshape(-1)
boxes = boxes[:, :8].reshape((-1, 4, 2))
boxes[:, :, 0] /= ratio_w
boxes[:, :, 1] /= ratio_h
duration = time.time() - start_time
timer['overall'] = duration
logger.info('[timing] {}'.format(duration))
text_lines = []
if boxes is not None:
text_lines = []
for box, score in zip(boxes, scores):
box = sort_poly(box.astype(np.int32))
if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3]-box[0]) < 5:
continue
tl = collections.OrderedDict(zip(
['x0', 'y0', 'x1', 'y1', 'x2', 'y2', 'x3', 'y3'],
map(float, box.flatten())))
tl['score'] = float(score)
text_lines.append(tl)
return text_lines
return predictor
import pickle
checkpoint_path = "./east_icdar2015_resnet_v1_50_rbox/"
def main():
global checkpoint_path
parser = argparse.ArgumentParser()
parser.add_argument('--checkpoint_path', default=checkpoint_path)
parser.add_argument('--img', default="demo_images/20000-leagues-006.jpg")
args = parser.parse_args()
checkpoint_path = args.checkpoint_path
img = cv2.imread(args.img)
#print("img", img)
rst = get_predictor(checkpoint_path)(img)
pickle.dump(rst,open("result.pkl","wb"))
if __name__ == '__main__':
main()