-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdemo.py
226 lines (188 loc) · 9.34 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
import argparse
import os
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from data import BaseTransform, VOC_CLASSES, coco_class_index, coco_class_labels, VOC_CLASSES_mask
from data import config
import numpy as np
import cv2
import tools
import time
def parse_args():
parser = argparse.ArgumentParser(description='YOLO Demo Detection')
parser.add_argument('-v', '--version', default='yolo_v2',
help='yolo_v2 and tiny_yolo_v2.')
parser.add_argument('--trained_model', default='weights/',
type=str, help='Trained state_dict file path to open')
parser.add_argument('--mode', default='image',
type=str, help='Use the data from image, video or camera')
parser.add_argument('-size', '--input_size', default=416, type=int,
help='input_size')
parser.add_argument('--cuda', action='store_true', default=False,
help='Use cuda')
parser.add_argument('--conf_thresh', default=0.1, type=float,
help='Confidence threshold')
parser.add_argument('--nms_thresh', default=0.50, type=float,
help='NMS threshold')
parser.add_argument('--path_to_img', default='data/demo/Images/',
type=str, help='The path to image files')
parser.add_argument('--path_to_vid', default='data/demo/video/',
type=str, help='The path to video files')
parser.add_argument('--path_to_saveVid', default='data/video/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')
return parser.parse_args()
def vis(img, bbox_pred, scores, cls_inds, class_color, thresh=0.3):
cls_name = ("background", "face", "mask")
for i, box in enumerate(bbox_pred):
if scores[i] > thresh:
cls_indx = cls_inds[i]
cls_id = coco_class_index[int(cls_indx)]
print(cls_id)
#cls_name = coco_class_labels[cls_id]
mess = '%s: %.3f' % (cls_name[cls_id], scores[i])
# bounding box
xmin, ymin, xmax, ymax = box
box_w = int(xmax - xmin)
cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), class_color[int(cls_indx)], 2)
cv2.rectangle(img, (int(xmin), int(abs(ymin)-15)), (int(xmin+box_w*0.55), int(ymin)), class_color[int(cls_indx)], -1)
cv2.putText(img, mess, (int(xmin), int(ymin)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 2)
return img
def detect(net, device, transform, thresh, mode='image', path_to_img=None, path_to_vid=None, path_to_save=None):
class_color = [(0,0,255), (0,255,0)]
# ------------------------- Camera ----------------------------
# I'm not sure whether this 'camera' mode works ...
if mode == 'camera':
print('use camera !!!')
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
ret, frame = cap.read()
cv2.imshow('current frame', frame)
if cv2.waitKey(1) == ord('q'):
break
x = torch.from_numpy(transform(frame)[0][:, :, (2, 1, 0)]).permute(2, 0, 1)
x = x.unsqueeze(0).to(device)
if "cuda" in device:
torch.cuda.synchronize()
t0 = time.time()
detections = net(x) # forward pass
if "cuda" in device:
torch.cuda.synchronize()
t1 = time.time()
print("detection time used ", t1-t0, "s")
# scale each detection back up to the image
scale = np.array([[frame.shape[1], frame.shape[0],
frame.shape[1], frame.shape[0]]])
bbox_pred, scores, cls_inds = detections
# map the boxes to origin image scale
bbox_pred *= scale
frame_processed = vis(frame, bbox_pred, scores, cls_inds, class_color, thresh=thresh)
cv2.imshow('detection result', frame_processed)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
# ------------------------- Image ----------------------------
elif mode == 'image':
for file in os.listdir(path_to_img):
img = cv2.imread(path_to_img + '/' + file, cv2.IMREAD_COLOR)
x = torch.from_numpy(transform(img)[0][:, :, (2, 1, 0)]).permute(2, 0, 1)
x = x.unsqueeze(0).to(device)
torch.cuda.synchronize()
t0 = time.time()
detections = net(x) # forward pass
torch.cuda.synchronize()
t1 = time.time()
print("detection time used ", t1-t0, "s")
# scale each detection back up to the image
scale = np.array([[img.shape[1], img.shape[0],
img.shape[1], img.shape[0]]])
bbox_pred, scores, cls_inds = detections
# map the boxes to origin image scale
bbox_pred *= scale
img_processed = vis(img, bbox_pred, scores, cls_inds, class_color=class_color, thresh=thresh)
cv2.imshow('detection result', img_processed)
cv2.waitKey(0)
# ------------------------- Video ---------------------------
elif mode == 'video':
video = cv2.VideoCapture(path_to_vid)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output000.avi',fourcc, 40.0, (1280,720))
while(True):
ret, frame = video.read()
if ret:
# ------------------------- Detection ---------------------------
t0 = time.time()
x = torch.from_numpy(transform(frame)[0][:, :, (2, 1, 0)]).permute(2, 0, 1)
x = x.unsqueeze(0).to(device)
torch.cuda.synchronize()
t0 = time.time()
detections = net(x) # forward pass
torch.cuda.synchronize()
t1 = time.time()
print("detection time used ", t1-t0, "s")
# scale each detection back up to the image
scale = np.array([[frame.shape[1], frame.shape[0],
frame.shape[1], frame.shape[0]]])
bbox_pred, scores, cls_inds = detections
# map the boxes to origin image scale
bbox_pred *= scale
frame_processed = vis(frame, bbox_pred, scores, cls_inds, class_color=class_color, thresh=thresh)
out.write(frame_processed)
cv2.imshow('detection result', frame_processed)
cv2.waitKey(1)
else:
break
video.release()
out.release()
cv2.destroyAllWindows()
def run():
args = parse_args()
# use cuda
if args.cuda:
device = torch.device("cuda")
else:
device = torch.device("cpu")
#input_size = [args.input_size, args.input_size]
input_size = [240, 320]
# load net
if args.version == 'yolo_v2':
from models.yolo_v2 import myYOLOv2
anchor_size = config.ANCHOR_SIZE_COCO
net = myYOLOv2(device, input_size=input_size, num_classes=80, conf_thresh=args.conf_thresh, nms_thresh=args.nms_thresh, anchor_size=anchor_size)
elif args.version == 'yolo_v3':
from models.yolo_v3 import myYOLOv3
anchor_size = config.MULTI_ANCHOR_SIZE_COCO
net = myYOLOv3(device, input_size=input_size, num_classes=80, conf_thresh=args.conf_thresh, nms_thresh=args.nms_thresh, anchor_size=anchor_size)
elif args.version == 'slim_yolo_v2':
from models.slim_yolo_v2 import SlimYOLOv2
anchor_size = config.ANCHOR_SIZE_COCO
net = SlimYOLOv2(device, input_size=input_size, num_classes=80, conf_thresh=args.conf_thresh, nms_thresh=args.nms_thresh, anchor_size=anchor_size)
elif args.version == 'slim_yolo_v2_q_bf':
from models.slim_yolo_v2 import SlimYOLOv2_quantize_bnfuse
anchor_size = config.ANCHOR_SIZE_MASK
#mask config
net = SlimYOLOv2_quantize_bnfuse(device, input_size=input_size, num_classes=2, conf_thresh=args.conf_thresh, nms_thresh=args.nms_thresh, anchor_size=anchor_size)
elif args.version == 'tiny_yolo_v3':
from models.tiny_yolo_v3 import YOLOv3tiny
anchor_size = config.TINY_MULTI_ANCHOR_SIZE_COCO
net = YOLOv3tiny(device, input_size=input_size, num_classes=80, conf_thresh=args.conf_thresh, nms_thresh=args.nms_thresh, anchor_size=anchor_size)
else:
print('Unknown version !!!')
exit()
net.load_state_dict(torch.load(args.trained_model, map_location=device))
net.to(device).eval()
print('Finished loading model!')
# run
if args.mode == 'camera':
detect(net, device, BaseTransform(net.input_size),
thresh=args.visual_threshold, mode=args.mode)
elif args.mode == 'image':
detect(net, device, BaseTransform(net.input_size),
thresh=args.visual_threshold, mode=args.mode, path_to_img=args.path_to_img)
elif args.mode == 'video':
detect(net, device, BaseTransform(net.input_size),
thresh=args.visual_threshold, mode=args.mode, path_to_vid=args.path_to_vid, path_to_save=args.path_to_saveVid)
if __name__ == '__main__':
run()