-
Notifications
You must be signed in to change notification settings - Fork 9
/
demo.py
258 lines (215 loc) · 9.01 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
import argparse
import cv2
import os
import time
import numpy as np
import torch
from dataset.coco import coco_class_index, coco_class_labels
from dataset.transforms import ValTransforms
from utils.misc import load_weight
from models import build_model
from config import build_config
def parse_args():
parser = argparse.ArgumentParser(description='YOLOF Demo Detection')
# basic
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/',
type=str, help='The path to save the detection results')
parser.add_argument('--path_to_saveVid', default='data/videos/result.avi',
type=str, help='The path to save the detection results video')
parser.add_argument('-vs', '--visual_threshold', default=0.3,
type=float, help='visual threshold')
# model
parser.add_argument('-v', '--version', default='yolof50',
help='build yolof')
parser.add_argument('--weight', default=None, type=str,
help='Trained state_dict file path to open')
parser.add_argument('--topk', default=100, type=int,
help='NMS threshold')
return parser.parse_args()
def plot_bbox_labels(img, bbox, label, cls_color, test_scale=0.4):
x1, y1, x2, y2 = bbox
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
# plot bbox
cv2.rectangle(img, (x1, y1), (x2, y2), cls_color, 2)
# plot title bbox
cv2.rectangle(img, (x1, y1-t_size[1]), (int(x1 + t_size[0] * test_scale), y1), cls_color, -1)
# put the test on the title bbox
cv2.putText(img, label, (int(x1), int(y1 - 5)), 0, test_scale, (0, 0, 0), 1, lineType=cv2.LINE_AA)
return img
def visualize(img, bboxes, scores, cls_inds, class_colors, vis_thresh=0.3):
ts = 0.4
for i, bbox in enumerate(bboxes):
if scores[i] > vis_thresh:
cls_color = class_colors[int(cls_inds[i])]
cls_id = coco_class_index[int(cls_inds[i])]
mess = '%s: %.2f' % (coco_class_labels[cls_id], scores[i])
img = plot_bbox_labels(img, bbox, mess, cls_color, test_scale=ts)
return img
def detect(net,
device,
transform,
vis_thresh,
mode='image',
path_to_img=None,
path_to_vid=None,
path_to_save=None):
# class color
np.random.seed(0)
class_colors = [(np.random.randint(255),
np.random.randint(255),
np.random.randint(255)) for _ in range(80)]
save_path = os.path.join(path_to_save, mode)
os.makedirs(save_path, exist_ok=True)
# ------------------------- Camera ----------------------------
if mode == 'camera':
print('use camera !!!')
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
ret, frame = cap.read()
if ret:
if cv2.waitKey(1) == ord('q'):
break
h, w, _ = frame.shape
orig_size = np.array([[w, h, w, h]])
# prepare
x = transform(frame)[0]
x = x.unsqueeze(0).to(device)
# inference
t0 = time.time()
bboxes, scores, cls_inds = net(x)
t1 = time.time()
print("detection time used ", t1-t0, "s")
# rescale
bboxes *= orig_size
bboxes[..., [0, 2]] = np.clip(bboxes[..., [0, 2]], a_min=0., a_max=w)
bboxes[..., [1, 3]] = np.clip(bboxes[..., [1, 3]], a_min=0., a_max=h)
frame_processed = visualize(img=frame,
bboxes=bboxes,
scores=scores,
cls_inds=cls_inds,
class_colors=class_colors,
vis_thresh=vis_thresh)
cv2.imshow('detection result', frame_processed)
cv2.waitKey(1)
else:
break
cap.release()
cv2.destroyAllWindows()
# ------------------------- Image ----------------------------
elif mode == 'image':
for i, img_id in enumerate(os.listdir(path_to_img)):
image = cv2.imread(path_to_img + '/' + img_id, cv2.IMREAD_COLOR)
h, w, _ = image.shape
orig_size = np.array([[w, h, w, h]])
# prepare
x = transform(image)[0]
x = x.unsqueeze(0).to(device)
# inference
t0 = time.time()
bboxes, scores, cls_inds = net(x)
t1 = time.time()
print("detection time used ", t1-t0, "s")
# rescale
bboxes *= orig_size
bboxes[..., [0, 2]] = np.clip(bboxes[..., [0, 2]], a_min=0., a_max=w)
bboxes[..., [1, 3]] = np.clip(bboxes[..., [1, 3]], a_min=0., a_max=h)
img_processed = visualize(img=image,
bboxes=bboxes,
scores=scores,
cls_inds=cls_inds,
class_colors=class_colors,
vis_thresh=vis_thresh)
cv2.imshow('detection', img_processed)
cv2.imwrite(os.path.join(save_path, str(i).zfill(6)+'.jpg'), img_processed)
cv2.waitKey(0)
# ------------------------- Video ---------------------------
elif mode == 'video':
video = cv2.VideoCapture(path_to_vid)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
save_size = (640, 480)
save_path = os.path.join(save_path, 'det.avi')
fps = 15.0
out = cv2.VideoWriter(save_path, fourcc, fps, save_size)
while(True):
ret, frame = video.read()
if ret:
# ------------------------- Detection ---------------------------
h, w, _ = frame.shape
orig_size = np.array([[w, h, w, h]])
# prepare
x = transform(frame)[0]
x = x.unsqueeze(0).to(device)
# inference
t0 = time.time()
bboxes, scores, cls_inds = net(x)
t1 = time.time()
print("detection time used ", t1-t0, "s")
# rescale
bboxes *= orig_size
bboxes[..., [0, 2]] = np.clip(bboxes[..., [0, 2]], a_min=0., a_max=w)
bboxes[..., [1, 3]] = np.clip(bboxes[..., [1, 3]], a_min=0., a_max=h)
frame_processed = visualize(img=frame,
bboxes=bboxes,
scores=scores,
cls_inds=cls_inds,
class_colors=class_colors,
vis_thresh=vis_thresh)
frame_processed_resize = cv2.resize(frame_processed, save_size)
out.write(frame_processed_resize)
cv2.imshow('detection', frame_processed)
cv2.waitKey(1)
else:
break
video.release()
out.release()
cv2.destroyAllWindows()
def run():
args = parse_args()
# cuda
if args.cuda:
print('use cuda')
device = torch.device("cuda")
else:
device = torch.device("cpu")
np.random.seed(0)
# YOLOF config
print('Model: ', args.version)
cfg = build_config(args)
# build model
model = build_model(args=args,
cfg=cfg,
device=device,
num_classes=80,
trainable=False)
# load trained weight
model = load_weight(model=model, path_to_ckpt=args.weight)
model.to(device).eval()
print('Finished loading model!')
# transform
transform = ValTransforms(
min_size=cfg['test_min_size'],
max_size=cfg['test_max_size'],
pixel_mean=cfg['pixel_mean'],
pixel_std=cfg['pixel_std'],
format=cfg['format']
)
# run
detect(net=model,
device=device,
transform=transform,
mode=args.mode,
path_to_img=args.path_to_img,
path_to_vid=args.path_to_vid,
path_to_save=args.path_to_save,
vis_thresh=args.visual_threshold)
if __name__ == '__main__':
run()