-
Notifications
You must be signed in to change notification settings - Fork 8
/
demo.py
255 lines (214 loc) · 9.73 KB
/
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
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
#!/usr/bin/env python
# coding: utf-8
import os
os.environ['GLOG_minloglevel'] = '2' ## ignore the caffe log
import warnings
warnings.filterwarnings('ignore') ## ignore Warning log
import numpy as np
import cv2 ## 3.4.5+ or 4.0 +
import math
import argparse
from tqdm import tqdm
############ Add argument parser for command line arguments ############
parser = argparse.ArgumentParser(description='Use this script to run EAST-caffe')
parser.add_argument('--input', default='imgs/ic15_train/img_896.jpg',
help='Path to input image for single demo')
parser.add_argument('--input_dir', default='imgs/ic15_test',
help='Path to input image for batch demo')
parser.add_argument('--output_dir', default='results',
help='Path to input image for batch demo')
parser.add_argument('--model_def', default='models/mbv3/deploy.prototxt',
help='prototxt file')
parser.add_argument('--model_weights', default='snapshot/ic15_iter_32000.caffemodel',
help='caffemodel file')
parser.add_argument('--thr',type=float, default=0.95,
help='Confidence threshold.')
parser.add_argument('--nms',type=float, default=0.1,
help='Non-maximum suppression threshold.')
parser.add_argument('--infer', default='dnn',
help='Inference API, dnn or caffe, recommand dnn inference')
parser.add_argument('--gpu',type=int, default=5,
help='GPU id (only set when inference API is caffe)')
args = parser.parse_args()
############ Utility functions ############
def get_images(img_path):
'''
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(img_path):
for filename in filenames:
for ext in exts:
if filename.endswith(ext):
files.append(os.path.join(parent, filename))
break
print('Find {} images'.format(len(files)))
return files
def decode(scores, geometry, scoreThresh):
detections = []
confidences = []
############ CHECK DIMENSIONS AND SHAPES OF geometry AND scores ############
assert len(scores.shape) == 4, "Incorrect dimensions of scores"
assert len(geometry.shape) == 4, "Incorrect dimensions of geometry"
assert scores.shape[0] == 1, "Invalid dimensions of scores"
assert geometry.shape[0] == 1, "Invalid dimensions of geometry"
assert scores.shape[1] == 1, "Invalid dimensions of scores"
assert geometry.shape[1] == 5, "Invalid dimensions of geometry"
assert scores.shape[2] == geometry.shape[2], "Invalid dimensions of scores and geometry"
assert scores.shape[3] == geometry.shape[3], "Invalid dimensions of scores and geometry"
height = scores.shape[2]
width = scores.shape[3]
for y in range(0, height):
# Extract data from scores
scoresData = scores[0][0][y]
x0_data = geometry[0][0][y]
x1_data = geometry[0][1][y]
x2_data = geometry[0][2][y]
x3_data = geometry[0][3][y]
anglesData = geometry[0][4][y]
for x in range(0, width):
score = scoresData[x]
# If score is lower than threshold score, move to next x
if(score < scoreThresh):
continue
# Calculate offset
offsetX = x * 4.0
offsetY = y * 4.0
angle = anglesData[x]
# Calculate cos and sin of angle
cosA = math.cos(angle)
sinA = math.sin(angle)
h = x0_data[x] + x2_data[x]
w = x1_data[x] + x3_data[x]
# Calculate offset
offset = ([offsetX + cosA * x1_data[x] + sinA * x2_data[x], offsetY - sinA * x1_data[x] + cosA * x2_data[x]])
# Find points for rectangle
p1 = (-sinA * h + offset[0], -cosA * h + offset[1])
p3 = (-cosA * w + offset[0], sinA * w + offset[1])
center = (0.5 * (p1[0] + p3[0]), 0.5 * (p1[1] + p3[1]))
detections.append((center, (w, h), -1 * angle * 180.0 / math.pi))
confidences.append(float(score))
# Return detections and confidences
return [detections, confidences]
def resize_image(im, max_side_len=2400):
'''
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.
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)
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 single_demo(input, output_dir):
im = cv2.imread(input)
im_resized, (rH, rW) = resize_image(im)
inpHeight, inpWidth, _ = im_resized.shape
im_name = input.split('/')[-1]
txt_name = 'res_' + im_name.split('.')[0] + '.txt'
if Inference_API == 'caffe':
import caffe
import time
gpu = args.gpu
caffe.set_device(gpu) # GPU_id pick
caffe.set_mode_gpu() # gpu mode
net = caffe.Net(model_def, # defines the structure of the model
model_weights, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)
# new_shape = [im.shape[2], im.shape[0], im.shape[1]]
# net.blobs['image'].reshape(1, *im.shape)
mu = np.array([103.94, 116.78, 123.68]) # the mean (BGR) pixel values
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR
image = caffe.io.load_image(input)
transformed_image = transformer.preprocess('data', image)
# copy the image data into the memory allocated for the net
net.blobs['data'].data[...] = transformed_image
### perform classification
start = time.time()
output = net.forward() # forward
elapsed = (time.time() - start) * 1000
print("CAFFE Inference time: %.2f ms" % elapsed)
F_score = output['f_score']
F_geometry = output['geo_concat']
if Inference_API == 'dnn':
net = cv2.dnn.readNet(model_weights, model_def, 'caffe')
blob = cv2.dnn.blobFromImage(im_resized, 1.0, (inpWidth, inpHeight), (123.68, 116.78, 103.94), True, False)
net.setInput(blob)
# outs = net.forward(['ScoreMap/score', 'GeoMap'])
outs = net.forward(['f_score', 'F_geometry'])
t, _ = net.getPerfProfile()
print('OPENCV-DNN Inference time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency()))
F_score = outs[0]
F_geometry = outs[1]
## Decode
boxes, confidences = decode(F_score, F_geometry, confThreshold)
## standard-NMS
print('bbox_number before NMS is %d' % len(boxes))
indices = cv2.dnn.NMSBoxesRotated(boxes, confidences, confThreshold, nmsThreshold)
print('bbox_number after NMS is %d' % len(indices))
txt_save_name = os.path.join(output_dir, txt_name)
with open(txt_save_name, 'w') as f:
## draw bbox
for i in indices:
coord = []
# get 4 corners of the rotated rect
vertices = cv2.boxPoints(boxes[i[0]])
# print(vertices)
# scale the bounding box coordinates based on the respective ratios
for j in range(4):
vertices[j][0] /= rW
vertices[j][1] /= rH
coord.append(int(vertices[1][0]))
coord.append(int(vertices[1][1]))
coord.append(int(vertices[2][0]))
coord.append(int(vertices[2][1]))
coord.append(int(vertices[3][0]))
coord.append(int(vertices[3][1]))
coord.append(int(vertices[0][0]))
coord.append(int(vertices[0][1]))
txt_line = ','.join(map(str, coord)) + ', RESULT\n'
f.write(txt_line)
for j in range(4):
p1 = (vertices[j][0], vertices[j][1])
p2 = (vertices[(j + 1) % 4][0], vertices[(j + 1) % 4][1])
cv2.line(im, p1, p2, (128, 240, 128), 2)
im_save_name = os.path.join(output_dir, im_name)
cv2.imwrite(im_save_name, im)
print('result saved at', im_save_name)
def batch_demo(input_dir, output_dir):
imgs = get_images(input_dir)
for img in tqdm(imgs):
single_demo(img, output_dir)
############ Parse Args ############
model_def = args.model_def
model_weights = args.model_weights
confThreshold = args.thr
nmsThreshold = args.nms
input = args.input ## single demo
input_dir = args.input_dir ## batch demo
output_dir = args.output_dir
Inference_API = args.infer
if __name__ == "__main__":
single_demo(input, output_dir)
# batch_demo(input_dir, output_dir)