-
Notifications
You must be signed in to change notification settings - Fork 33
/
demo.py
310 lines (264 loc) · 11.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import argparse
import cv2
import os
import time
import numpy as np
import imageio
import torch
# load transform
from dataset.build import build_transform
# load some utils
from utils.misc import load_weight
from utils.box_ops import rescale_bboxes
from utils.vis_tools import visualize
from models.detectors import build_model
from config import build_model_config, build_trans_config, build_dataset_config
def parse_args():
parser = argparse.ArgumentParser(description='Real-time Object Detection LAB')
# Basic setting
parser.add_argument('-size', '--img_size', default=640, type=int,
help='the max size of input image')
parser.add_argument('--mosaic', default=None, type=float,
help='mosaic augmentation.')
parser.add_argument('--mixup', default=None, type=float,
help='mixup augmentation.')
parser.add_argument('--mode', default='image',
type=str, help='Use the data from image, video or camera')
parser.add_argument('--cuda', action='store_true', default=False,
help='Use cuda')
parser.add_argument('--path_to_img', default='dataset/demo/images/',
type=str, help='The path to image files')
parser.add_argument('--path_to_vid', default='dataset/demo/videos/',
type=str, help='The path to video files')
parser.add_argument('--path_to_save', default='det_results/demos/',
type=str, help='The path to save the detection results')
parser.add_argument('--show', action='store_true', default=False,
help='show visualization')
parser.add_argument('--gif', action='store_true', default=False,
help='generate gif.')
# Model setting
parser.add_argument('-m', '--model', default='yolov1', type=str,
help='build yolo')
parser.add_argument('-nc', '--num_classes', default=80, type=int,
help='number of classes.')
parser.add_argument('--weight', default=None,
type=str, help='Trained state_dict file path to open')
parser.add_argument('-ct', '--conf_thresh', default=0.35, type=float,
help='confidence threshold')
parser.add_argument('-nt', '--nms_thresh', default=0.5, type=float,
help='NMS threshold')
parser.add_argument('--topk', default=100, type=int,
help='topk candidates dets of each level before NMS')
parser.add_argument("--deploy", action="store_true", default=False,
help="deploy mode or not")
parser.add_argument('--fuse_conv_bn', action='store_true', default=False,
help='fuse Conv & BN')
parser.add_argument('--no_multi_labels', action='store_true', default=False,
help='Perform post-process with multi-labels trick.')
parser.add_argument('--nms_class_agnostic', action='store_true', default=False,
help='Perform NMS operations regardless of category.')
# Data setting
parser.add_argument('-d', '--dataset', default='coco',
help='coco, voc, crowdhuman, widerface.')
return parser.parse_args()
def detect(args,
model,
device,
transform,
num_classes,
class_names,
class_indexs,
mode='image'):
# class color
np.random.seed(0)
class_colors = [(np.random.randint(255),
np.random.randint(255),
np.random.randint(255)) for _ in range(num_classes)]
save_path = os.path.join(args.path_to_save, mode)
os.makedirs(save_path, exist_ok=True)
# ------------------------- Camera ----------------------------
if mode == 'camera':
print('use camera !!!')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
save_size = (640, 480)
cur_time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
save_video_name = os.path.join(save_path, cur_time+'.avi')
fps = 15.0
out = cv2.VideoWriter(save_video_name, fourcc, fps, save_size)
print(save_video_name)
image_list = []
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
ret, frame = cap.read()
if ret:
if cv2.waitKey(1) == ord('q'):
break
orig_h, orig_w, _ = frame.shape
# prepare
x, _, ratio = transform(frame)
x = x.unsqueeze(0).to(device)
# inference
t0 = time.time()
outputs = model(x)
scores = outputs['scores']
labels = outputs['labels']
bboxes = outputs['bboxes']
t1 = time.time()
print("Infer time: {:.1f} ms. ".format((t1 - t0) * 1000))
# rescale bboxes
bboxes = rescale_bboxes(bboxes, [orig_w, orig_h], ratio)
# vis detection
frame_vis = visualize(image=frame,
bboxes=bboxes,
scores=scores,
labels=labels,
class_colors=class_colors,
class_names=class_names,
class_indexs=class_indexs)
frame_resized = cv2.resize(frame_vis, save_size)
out.write(frame_resized)
if args.gif:
gif_resized = cv2.resize(frame, (640, 480))
gif_resized_rgb = gif_resized[..., (2, 1, 0)]
image_list.append(gif_resized_rgb)
if args.show:
cv2.imshow('detection', frame_resized)
cv2.waitKey(1)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
# generate GIF
if args.gif:
save_gif_path = os.path.join(save_path, 'gif_files')
os.makedirs(save_gif_path, exist_ok=True)
save_gif_name = os.path.join(save_gif_path, '{}.gif'.format(cur_time))
print('generating GIF ...')
imageio.mimsave(save_gif_name, image_list, fps=fps)
print('GIF done: {}'.format(save_gif_name))
# ------------------------- Video ---------------------------
elif mode == 'video':
video = cv2.VideoCapture(args.path_to_vid)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
save_size = (640, 480)
cur_time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
save_video_name = os.path.join(save_path, cur_time+'.avi')
fps = 15.0
out = cv2.VideoWriter(save_video_name, fourcc, fps, save_size)
print(save_video_name)
image_list = []
while(True):
ret, frame = video.read()
if ret:
# ------------------------- Detection ---------------------------
orig_h, orig_w, _ = frame.shape
# prepare
x, _, ratio = transform(frame)
x = x.unsqueeze(0).to(device)
# inference
t0 = time.time()
outputs = model(x)
scores = outputs['scores']
labels = outputs['labels']
bboxes = outputs['bboxes']
t1 = time.time()
print("Infer time: {:.1f} ms. ".format((t1 - t0) * 1000))
# rescale bboxes
bboxes = rescale_bboxes(bboxes, [orig_w, orig_h], ratio)
# vis detection
frame_vis = visualize(image=frame,
bboxes=bboxes,
scores=scores,
labels=labels,
class_colors=class_colors,
class_names=class_names,
class_indexs=class_indexs)
frame_resized = cv2.resize(frame_vis, save_size)
out.write(frame_resized)
if args.gif:
gif_resized = cv2.resize(frame, (640, 480))
gif_resized_rgb = gif_resized[..., (2, 1, 0)]
image_list.append(gif_resized_rgb)
if args.show:
cv2.imshow('detection', frame_resized)
cv2.waitKey(1)
else:
break
video.release()
out.release()
cv2.destroyAllWindows()
# generate GIF
if args.gif:
save_gif_path = os.path.join(save_path, 'gif_files')
os.makedirs(save_gif_path, exist_ok=True)
save_gif_name = os.path.join(save_gif_path, '{}.gif'.format(cur_time))
print('generating GIF ...')
imageio.mimsave(save_gif_name, image_list, fps=fps)
print('GIF done: {}'.format(save_gif_name))
# ------------------------- Image ----------------------------
elif mode == 'image':
for i, img_id in enumerate(os.listdir(args.path_to_img)):
image = cv2.imread((args.path_to_img + '/' + img_id), cv2.IMREAD_COLOR)
orig_h, orig_w, _ = image.shape
# prepare
x, _, ratio = transform(image)
x = x.unsqueeze(0).to(device)
# inference
t0 = time.time()
outputs = model(x)
scores = outputs['scores']
labels = outputs['labels']
bboxes = outputs['bboxes']
t1 = time.time()
print("Infer time: {:.1f} ms. ".format((t1 - t0) * 1000))
# rescale bboxes
bboxes = rescale_bboxes(bboxes, [orig_w, orig_h], ratio)
# vis detection
img_processed = visualize(image=image,
bboxes=bboxes,
scores=scores,
labels=labels,
class_colors=class_colors,
class_names=class_names,
class_indexs=class_indexs)
cv2.imwrite(os.path.join(save_path, str(i).zfill(6)+'.jpg'), img_processed)
if args.show:
cv2.imshow('detection', img_processed)
cv2.waitKey(0)
def run():
args = parse_args()
# cuda
if args.cuda:
print('use cuda')
device = torch.device("cuda")
else:
device = torch.device("cpu")
# config
model_cfg = build_model_config(args)
trans_cfg = build_trans_config(model_cfg['trans_type'])
data_cfg = build_dataset_config(args)
## Data info
num_classes = data_cfg['num_classes']
class_names = data_cfg['class_names']
class_indexs = data_cfg['class_indexs']
# build model
model = build_model(args, model_cfg, device, num_classes, False)
# load trained weight
model = load_weight(model, args.weight, args.fuse_conv_bn)
model.to(device).eval()
# transform
val_transform, trans_cfg = build_transform(args, trans_cfg, model_cfg['max_stride'], is_train=False)
print("================= DETECT =================")
# run
detect(args = args,
mode = args.mode,
model = model,
device = device,
transform = val_transform,
num_classes = num_classes,
class_names = class_names,
class_indexs = class_indexs,
)
if __name__ == '__main__':
run()