-
Notifications
You must be signed in to change notification settings - Fork 51
/
webcam_demo.py
129 lines (107 loc) · 3.5 KB
/
webcam_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
#!/usr/bin/env python
import argparse
import os
import cv2
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from face_detection.model.prior_box import PriorBox
from face_detection.model.retinaface import RetinaFace
from face_detection.utils.misc import draw_keypoint, inference
parser = argparse.ArgumentParser(description='PIMNet')
parser.add_argument(
'--checkpoint', type=str,
default='face_detection/weights/mobilenet0.25_final.pt',
help='Trained state_dict file path to open'
)
parser.add_argument(
'--cpu', action="store_true", default=False,
help='Use cpu inference'
)
parser.add_argument(
'--jit', action="store_true", default=False,
help='Use JIT'
)
parser.add_argument(
'--confidence-threshold', type=float, default=0.02,
help='confidence_threshold'
)
parser.add_argument(
'--nms-threshold', type=float, default=0.4,
help='nms_threshold'
)
parser.add_argument(
'--vis-thres', type=float, default=0.5,
help='visualization_threshold'
)
parser.add_argument(
'-s', '--save-image', action="store_true", default=False,
help='show detection results'
)
parser.add_argument(
'--save-dir', type=str, default='demo',
help='Dir to save results'
)
def main():
args = parser.parse_args()
assert os.path.isfile(args.checkpoint)
checkpoint = torch.load(args.checkpoint, map_location="cpu")
cfg = checkpoint["config"]
device = torch.device("cpu" if args.cpu else "cuda")
# net and model
detector = RetinaFace(**cfg)
detector.load_state_dict(checkpoint["net_state_dict"])
detector.eval().requires_grad_(False)
detector.to(device)
print('Finished loading model!')
cudnn.benchmark = True
# prepare testing
cap = cv2.VideoCapture(0)
assert cap.isOpened()
ret_val, img_tmp = cap.read()
im_height, im_width, _ = img_tmp.shape
scale = torch.Tensor([im_width, im_height, im_width, im_height])
scale = scale.to(device)
scale1 = torch.Tensor([im_width, im_height] * 5)
scale1 = scale1.to(device)
priorbox = PriorBox(cfg, image_size=(im_height, im_width))
priors = priorbox.forward()
priors = priors.to(device)
prior_data = priors.data
if args.jit:
img_tmp = img_tmp.transpose(2, 0, 1)
img_tmp = np.float32(img_tmp)
img_tmp = torch.from_numpy(img_tmp).unsqueeze(0)
dummy = img_tmp.to(device)
detector = torch.jit.trace(detector, example_inputs=dummy)
if args.save_image:
nframe = 0
fname = os.path.join(args.save_dir, "{:06d}.jpg")
os.makedirs(args.save_dir, exist_ok=True)
# testing begin
ret_val, img_raw = cap.read()
while ret_val:
start = cv2.getTickCount()
# NOTE preprocessing.
dets = inference(
detector, img_raw, scale, scale1, prior_data, cfg,
args.confidence_threshold, args.nms_threshold, device
)
fps = float(cv2.getTickFrequency() / (cv2.getTickCount() - start))
cv2.putText(
img_raw, f"FPS: {fps:.1f}", (5, 15),
cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255)
)
# show image
draw_keypoint(img_raw, dets, args.vis_thres)
if args.save_image:
cv2.imwrite(fname.format(nframe), img_raw)
nframe += 1
cv2.imshow("Webcam Demo", img_raw)
if cv2.waitKey(1) == 27: # Press ESC button to quit.
break
ret_val, img_raw = cap.read()
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()