-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcam_inference.py
207 lines (163 loc) · 5.84 KB
/
webcam_inference.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
import os
import cv2
import argparse
import numpy as np
import torch
from layers import PriorBox
from config import get_config
from models import RetinaFace
from utils.general import draw_detections
from utils.box_utils import decode, decode_landmarks, nms
def parse_arguments():
parser = argparse.ArgumentParser(description="Retinaface Webcam Inference")
# Model and device options
parser.add_argument(
'-w', '--weights',
type=str,
default='./weights/Resnet34_Final.pth',
help='Path to the trained model weights'
)
parser.add_argument(
'--network',
type=str,
default='resnet34',
choices=[
'mobilenetv1', 'mobilenetv1_0.25', 'mobilenetv1_0.50',
'mobilenetv2', 'resnet50', 'resnet34', 'resnet18'
],
help='Backbone network architecture to use'
)
# Detection settings
parser.add_argument(
'--conf-threshold',
type=float,
default=0.4,
help='Confidence threshold for filtering detections'
)
parser.add_argument(
'--pre-nms-topk',
type=int,
default=5000,
help='Maximum number of detections to consider before applying NMS'
)
parser.add_argument(
'--nms-threshold',
type=float,
default=0.4,
help='Non-Maximum Suppression (NMS) threshold'
)
parser.add_argument(
'--post-nms-topk',
type=int,
default=750,
help='Number of highest scoring detections to keep after NMS'
)
# Output options
parser.add_argument(
'-v', '--vis-threshold',
type=float,
default=0.6,
help='Visualization threshold for displaying detections'
)
return parser.parse_args()
@torch.no_grad()
def inference(model, image):
model.eval()
loc, conf, landmarks = model(image)
loc = loc.squeeze(0)
conf = conf.squeeze(0)
landmarks = landmarks.squeeze(0)
return loc, conf, landmarks
def resize_image(frame, target_shape=(640, 640)):
width, height = target_shape
# Aspect-ratio preserving resize
im_ratio = float(frame.shape[0]) / frame.shape[1]
model_ratio = height / width
if im_ratio > model_ratio:
new_height = height
new_width = int(new_height / im_ratio)
else:
new_width = width
new_height = int(new_width * im_ratio)
resize_factor = float(new_height) / frame.shape[0]
resized_frame = cv2.resize(frame, (new_width, new_height))
# Create blank image and place resized image on it
image = np.zeros((height, width, 3), dtype=np.uint8)
image[:new_height, :new_width, :] = resized_frame
return image, resize_factor
def main(params):
cfg = get_config(params.network)
if cfg is None:
raise KeyError(f"Config file for {params.network} not found!")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
rgb_mean = (104, 117, 123)
resize_factor = 1
# model initialization
model = RetinaFace(cfg=cfg)
model.to(device)
# loading state_dict
state_dict = torch.load(params.weights, map_location="cpu", weights_only=True)
model.load_state_dict(state_dict)
print("Model loaded successfully!")
# Open webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
return
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
image, resize_factor = resize_image(frame, target_shape=(640, 640))
# Prepare image for inference
image = np.float32(image)
img_height, img_width, _ = image.shape
image -= rgb_mean
image = image.transpose(2, 0, 1) # HWC -> CHW
image = torch.from_numpy(image).unsqueeze(0).to(device)
# forward pass
loc, conf, landmarks = inference(model, image)
# generate anchor boxes
priorbox = PriorBox(cfg, image_size=(img_height, img_width))
priors = priorbox.generate_anchors().to(device)
# decode boxes and landmarks
boxes = decode(loc, priors, cfg['variance'])
landmarks = decode_landmarks(landmarks, priors, cfg['variance'])
# scale adjustments
bbox_scale = torch.tensor([img_width, img_height] * 2, device=device)
boxes = (boxes * bbox_scale / resize_factor).cpu().numpy()
landmark_scale = torch.tensor([img_width, img_height] * 5, device=device)
landmarks = (landmarks * landmark_scale / resize_factor).cpu().numpy()
scores = conf.cpu().numpy()[:, 1]
# filter by confidence threshold
inds = scores > params.conf_threshold
boxes = boxes[inds]
landmarks = landmarks[inds]
scores = scores[inds]
# sort by scores
order = scores.argsort()[::-1][:params.pre_nms_topk]
boxes, landmarks, scores = boxes[order], landmarks[order], scores[order]
# apply NMS
detections = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = nms(detections, params.nms_threshold)
detections = detections[keep]
landmarks = landmarks[keep]
# keep top-k detections and landmarks
detections = detections[:params.post_nms_topk]
landmarks = landmarks[:params.post_nms_topk]
# concatenate detections and landmarks
detections = np.concatenate((detections, landmarks), axis=1)
# draw detections on the frame
draw_detections(frame, detections, params.vis_threshold)
# Display the resulting frame
cv2.imshow('Webcam Inference', frame)
# Press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close windows
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
args = parse_arguments()
main(args)