-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy patheast.py
277 lines (214 loc) · 7.83 KB
/
east.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import sys, os
import time
import argparse
import numpy as np
import cv2
import json
import ailia
# import original modules
sys.path.append('../../util')
from arg_utils import get_base_parser, update_parser, get_savepath # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from detector_utils import load_image # noqa: E402C
from webcamera_utils import get_capture # noqa: E402
from east_utils import *
# logger
from logging import getLogger # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_PATH = 'east.onnx'
MODEL_PATH = 'east.onnx.prototxt'
REMOTE_PATH = \
'https://storage.googleapis.com/ailia-models/east/'
IMAGE_PATH = 'img_2.jpg'
SAVE_IMAGE_PATH = 'output.png'
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'EAST: An Efficient and Accurate Scene Text Detector model',
IMAGE_PATH,
SAVE_IMAGE_PATH,
)
parser.add_argument(
'-w', '--write_json',
action='store_true',
help='Flag to output results to json file.'
)
args = update_parser(parser)
# ======================
# Secondaty Functions
# ======================
def preprocess(img, max_side_len=2400):
'''
resize image to a size multiple of 32 which is required by the network
:param img: 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, _ = img.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.
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
resize_h = max(32, resize_h)
resize_w = max(32, resize_w)
img = cv2.resize(img, (int(resize_w), int(resize_h)))
ratio_h = resize_h / float(h)
ratio_w = resize_w / float(w)
img = np.expand_dims(img, axis=0).astype(np.float32)
return img, (ratio_h, ratio_w)
def post_processing(
score_map, geo_map,
score_map_thresh=0.8, box_thresh=0.1, nms_thres=0.2):
'''
restore text boxes from score map and geo map
:param score_map:
:param geo_map:
:param timer:
:param score_map_thresh: threshhold for score map
:param box_thresh: threshhold for boxes
:param nms_thres: threshold for nms
:return:
'''
if len(score_map.shape) == 4:
score_map = score_map[0, :, :, 0]
geo_map = geo_map[0, :, :, ]
# filter the score map
xy_text = np.argwhere(score_map > score_map_thresh)
# sort the text boxes via the y axis
xy_text = xy_text[np.argsort(xy_text[:, 0])]
# restore
text_box_restored = restore_rectangle(xy_text[:, ::-1] * 4, geo_map[xy_text[:, 0], xy_text[:, 1], :]) # N*4*2
logger.info('{} text boxes before nms'.format(text_box_restored.shape[0]))
boxes = np.zeros((text_box_restored.shape[0], 9), dtype=np.float32)
boxes[:, :8] = text_box_restored.reshape((-1, 8))
boxes[:, 8] = score_map[xy_text[:, 0], xy_text[:, 1]]
# nms part
boxes = nms_locality(boxes.astype(np.float64), nms_thres)
if boxes.shape[0] == 0:
return None
# here we filter some low score boxes by the average score map, this is different from the orginal paper
for i, box in enumerate(boxes):
mask = np.zeros_like(score_map, dtype=np.uint8)
cv2.fillPoly(mask, box[:8].reshape((-1, 4, 2)).astype(np.int32) // 4, 1)
boxes[i, 8] = cv2.mean(score_map, mask)[0]
boxes = boxes[boxes[:, 8] > box_thresh]
return boxes
def sort_poly(p):
min_axis = np.argmin(np.sum(p, axis=1))
p = p[[min_axis, (min_axis + 1) % 4, (min_axis + 2) % 4, (min_axis + 3) % 4]]
if abs(p[0, 0] - p[1, 0]) > abs(p[0, 1] - p[1, 1]):
return p
else:
return p[[0, 3, 2, 1]]
def draw_box(img, boxes):
if boxes is not None:
for box in boxes:
# to avoid submitting errors
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
logger.info('({},{}),({},{}),({},{}),({},{})'.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(
img, [box.astype(np.int32).reshape((-1, 1, 2))], True,
color=(255, 255, 0), thickness=1)
return img
def save_result_json(json_path, boxes):
results = []
if boxes is not None:
for box in boxes:
box = sort_poly(box)
if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3] - box[0]) < 5:
continue
results.append(box.tolist())
with open(json_path, 'w') as f:
json.dump({"boxes": results}, f, indent=2)
# ======================
# Main functions
# ======================
def predict(img, net):
img, (ratio_h, ratio_w) = preprocess(img)
# feedforward
net.set_input_shape(img.shape)
output = net.predict({'import/input_images:0': img})
score, geometry = output
boxes = post_processing(score_map=score, geo_map=geometry)
if boxes is not None:
boxes = boxes[:, :8].reshape((-1, 4, 2))
boxes[:, :, 0] /= ratio_w
boxes[:, :, 1] /= ratio_h
return boxes
def recognize_from_image(filename, net):
# input image loop
for image_path in args.input:
# prepare input data
logger.info(image_path)
# prepare input data
img = load_image(image_path)
logger.info(f'input image shape: {img.shape}')
img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
for i in range(5):
start = int(round(time.time() * 1000))
boxes = predict(img, net)
end = int(round(time.time() * 1000))
logger.info(f'\tailia processing time {end - start} ms')
else:
boxes = predict(img, net)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
res_img = draw_box(img, boxes)
# plot result
savepath = get_savepath(args.savepath, image_path)
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, res_img)
if args.write_json:
json_file = '%s.json' % savepath.rsplit('.', 1)[0]
save_result_json(json_file, boxes)
logger.info('Script finished successfully.')
def recognize_from_video(video, net):
capture = get_capture(video)
frame_shown = False
while (True):
ret, frame = capture.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if frame_shown and cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) == 0:
break
if not ret:
continue
boxes = predict(frame, net)
# plot result
res_img = draw_box(frame, boxes)
# show
cv2.imshow('frame', res_img)
frame_shown = True
capture.release()
cv2.destroyAllWindows()
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
# initialize
memory_mode = ailia.get_memory_mode(reduce_constant=True, reduce_interstage=True)
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, memory_mode=memory_mode)
if args.video is not None:
recognize_from_video(args.video, net)
else:
recognize_from_image(args.input, net)
if __name__ == '__main__':
main()