-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval.py
executable file
·158 lines (127 loc) · 5.44 KB
/
eval.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding:utf-8 -*-
import cv2
import os
import numpy as np
import tensorflow as tf
tf.app.flags.DEFINE_string('test_data_path', './images/', '')
tf.app.flags.DEFINE_string('output_dir', './results/', '')
tf.app.flags.DEFINE_bool('no_write_images', False, 'do not write images')
from pse import pse
FLAGS = tf.app.flags.FLAGS
def get_images():
'''
find image files in test data path
:return: list of files found
'''
files = []
exts = ['jpg', 'png', 'jpeg', 'JPG']
for parent, dirnames, filenames in os.walk(FLAGS.test_data_path):
for filename in filenames:
for ext in exts:
if filename.endswith(ext):
files.append(os.path.join(parent, filename))
break
return files
def resize_image(im, max_side_len=1200):
'''
resize image to a size multiple of 32 which is required by the network
:param im: the resized image
:param max_side_len: limit of max image size to avoid out of memory in gpu
:return: the resized image and the resize ratio
'''
h, w, _ = im.shape
resize_w = w
resize_h = h
# limit the max side
if max(resize_h, resize_w) > max_side_len:
ratio = float(max_side_len) / resize_h if resize_h > resize_w else float(max_side_len) / resize_w
else:
ratio = 1.
#ratio = float(max_side_len) / resize_h if resize_h > resize_w else float(max_side_len) / resize_w
resize_h = int(resize_h * ratio)
resize_w = int(resize_w * ratio)
resize_h = resize_h if resize_h % 32 == 0 else (resize_h // 32 + 1) * 32
resize_w = resize_w if resize_w % 32 == 0 else (resize_w // 32 + 1) * 32
im = cv2.resize(im, (int(resize_w), int(resize_h)))
ratio_h = resize_h / float(h)
ratio_w = resize_w / float(w)
return im, (ratio_h, ratio_w)
def detect(seg_maps, image_w, image_h, min_area_thresh=10, seg_map_thresh=0.9, ratio = 1):
'''
restore text boxes from score map and geo map
:param seg_maps:
:param min_area_thresh:
:param seg_map_thresh: threshhold for seg map
:param ratio: compute each seg map thresh
:return:
'''
if len(seg_maps.shape) == 4:
seg_maps = seg_maps[0, :, :, ]
#get kernals, sequence: 0->n, max -> min
kernals = []
one = np.ones_like(seg_maps[..., 0], dtype=np.uint8)
zero = np.zeros_like(seg_maps[..., 0], dtype=np.uint8)
thresh = seg_map_thresh
for i in range(seg_maps.shape[-1]-1, -1, -1):
kernal = np.where(seg_maps[..., i]>thresh, one, zero)
kernals.append(kernal)
thresh = seg_map_thresh*ratio
mask_res, label_values = pse(kernals, min_area_thresh)
mask_res = np.array(mask_res)
mask_res_resized = cv2.resize(mask_res, (image_w, image_h), interpolation=cv2.INTER_NEAREST)
boxes = []
for label_value in label_values:
#(y,x)
points = np.argwhere(mask_res_resized==label_value)
points = points[:, (1,0)]
rect = cv2.minAreaRect(points)
box = cv2.boxPoints(rect)
boxes.append(box)
return np.array(boxes), kernals
def main(argv=None):
try:
os.makedirs(FLAGS.output_dir)
except OSError as e:
if e.errno != 17:
raise
with tf.Session() as sess:
with tf.gfile.FastGFile('model/psenet.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
sess.run(tf.global_variables_initializer())
final_tensor = sess.graph.get_tensor_by_name('Sigmoid:0')
im_fn_list = get_images()
for im_fn in im_fn_list:
im = cv2.imread(im_fn)[:, :, ::-1]
im_resized, (ratio_h, ratio_w) = resize_image(im)
h, w, _ = im_resized.shape
seg_maps = sess.run(final_tensor, feed_dict={"input_images:0": [im_resized]})
boxes, kernels = detect(seg_maps=seg_maps, image_w=w, image_h=h)
if boxes is not None:
boxes = boxes.reshape((-1, 4, 2))
boxes[:, :, 0] /= ratio_w
boxes[:, :, 1] /= ratio_h
h, w, _ = im.shape
boxes[:, :, 0] = np.clip(boxes[:, :, 0], 0, w)
boxes[:, :, 1] = np.clip(boxes[:, :, 1], 0, h)
# save to file
if boxes is not None:
res_file = os.path.join(
FLAGS.output_dir,
'{}.txt'.format(os.path.splitext(
os.path.basename(im_fn))[0]))
with open(res_file, 'w') as f:
for i in range(len(boxes)):
# to avoid submitting errors
box = boxes[i]
if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3]-box[0]) < 5:
continue
f.write('{},{},{},{},{},{},{},{}\r\n'.format(
box[0, 0], box[0, 1], box[1, 0], box[1, 1], box[2, 0], box[2, 1], box[3, 0], box[3, 1]))
cv2.polylines(im[:, :, ::-1], [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=2)
if not FLAGS.no_write_images:
img_path = os.path.join(FLAGS.output_dir, os.path.basename(im_fn))
cv2.imwrite(img_path, im[:, :, ::-1])
if __name__ == '__main__':
tf.app.run()