-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect.py
172 lines (138 loc) · 5.63 KB
/
detect.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
import os
import time
import logging
import argparse
import warnings
import cv2
import numpy as np
import torch
from torchvision import transforms
from models import get_model, SCRFD
from utils.general import compute_euler_angles_from_rotation_matrices, draw_cube, draw_axis
warnings.filterwarnings("ignore")
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
def parse_args():
"""Parse input arguments."""
parser = argparse.ArgumentParser(description='Head pose estimation inference.')
parser.add_argument("--network", type=str, default="resnet18", help="Model name, default `resnet18`")
parser.add_argument(
"--input",
type=str,
default='assets/in_video.mp4',
help="Path to input video file or camera id"
)
parser.add_argument("--view", action="store_true", help="Display the inference results")
parser.add_argument(
"--draw-type",
type=str,
default='cube',
choices=['cube', 'axis'],
help="Draw cube or axis for head pose"
)
parser.add_argument('--weights', type=str, required=True, help='Path to head pose estimation model weights')
parser.add_argument("--output", type=str, default="output.mp4", help="Path to save output file")
return parser.parse_args()
def pre_process(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image = transform(image)
image_batch = image.unsqueeze(0)
return image_batch
def expand_bbox(x_min, y_min, x_max, y_max, factor=0.2):
"""Expand the bounding box by a given factor."""
width = x_max - x_min
height = y_max - y_min
x_min_new = x_min - int(factor * height)
y_min_new = y_min - int(factor * width)
x_max_new = x_max + int(factor * height)
y_max_new = y_max + int(factor * width)
return max(0, x_min_new), max(0, y_min_new), x_max_new, y_max_new
def main(params):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
try:
face_detector = SCRFD(model_path="./weights/det_10g.onnx")
logging.info("Face Detection model weights loaded.")
except Exception as e:
logging.info(f"Exception occured while loading pre-trained weights of face detection model. Exception: {e}")
try:
head_pose = get_model(params.network, num_classes=6, pretrained=False)
state_dict = torch.load(params.weights, map_location=device)
head_pose.load_state_dict(state_dict)
logging.info("Head Pose Estimation model weights loaded.")
except Exception as e:
logging.info(
f"Exception occured while loading pre-trained weights of head pose estimation model. Exception: {e}")
head_pose.to(device)
head_pose.eval()
# Initialize video capture
video_source = params.input
if video_source.isdigit() or video_source == '0':
cap = cv2.VideoCapture(int(video_source))
else:
cap = cv2.VideoCapture(video_source)
if not cap.isOpened():
raise IOError("Cannot open webcam")
# Initialize VideoWriter if saving video
if params.output:
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(params.output, fourcc, cap.get(cv2.CAP_PROP_FPS), (width, height))
with torch.no_grad():
while True:
success, frame = cap.read()
if not success:
logging.info("Failed to obtain frame or EOF")
break
bboxes, keypoints = face_detector.detect(frame)
for bbox, keypoint in zip(bboxes, keypoints):
x_min, y_min, x_max, y_max = map(int, bbox[:4])
width = x_max - x_min
x_min, y_min, x_max, y_max = expand_bbox(x_min, y_min, x_max, y_max)
image = frame[y_min:y_max, x_min:x_max]
image = pre_process(image)
image = image.to(device)
start = time.time()
rotation_matrix = head_pose(image)
logging.info('Head pose estimation: %.2f ms' % ((time.time() - start) * 1000))
euler = np.degrees(compute_euler_angles_from_rotation_matrices(rotation_matrix))
p_pred_deg = euler[:, 0].cpu()
y_pred_deg = euler[:, 1].cpu()
r_pred_deg = euler[:, 2].cpu()
if args.draw_type == "cube":
draw_cube(
frame,
y_pred_deg,
p_pred_deg,
r_pred_deg,
bbox=[x_min, y_min, x_max, y_max],
size=width
)
else:
draw_axis(
frame,
y_pred_deg,
p_pred_deg,
r_pred_deg,
bbox=[x_min, y_min, x_max, y_max],
size_ratio=0.5
)
if params.view:
cv2.imshow('Demo', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Write the frame to the video file if saving
if params.output:
out.write(frame)
cap.release()
if params.output:
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
args = parse_args()
main(args)