-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
193 lines (157 loc) · 5.63 KB
/
main.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
import os
import cv2
import random
import warnings
import argparse
import logging
import numpy as np
import onnxruntime
from typing import Union, List, Tuple
from models import SCRFD, ArcFace
from utils.helpers import compute_similarity, draw_bbox_info, draw_bbox
warnings.filterwarnings("ignore")
def parse_args():
parser = argparse.ArgumentParser(description="Face Detection-and-Recognition")
parser.add_argument(
"--det-weight",
type=str,
default="./weights/det_10g.onnx",
help="Path to detection model"
)
parser.add_argument(
"--rec-weight",
type=str,
default="./weights/w600k_r50.onnx",
help="Path to recognition model"
)
parser.add_argument(
"--similarity-thresh",
type=float,
default=0.4,
help="Similarity threshold between faces"
)
parser.add_argument(
"--confidence-thresh",
type=float,
default=0.5,
help="Confidence threshold for face detection"
)
parser.add_argument(
"--faces-dir",
type=str,
default="./faces",
help="Path to faces stored dir"
)
parser.add_argument(
"--source",
type=str,
default="./assets/in_video.mp4",
help="Video file or video camera source. i.e 0 - webcam"
)
parser.add_argument(
"--max-num",
type=int,
default=0,
help="Maximum number of face detections from a frame"
)
parser.add_argument(
"--log-level",
type=str,
default="INFO",
help="Logging level"
)
return parser.parse_args()
def setup_logging(level: str) -> None:
logging.basicConfig(
level=getattr(logging, level.upper(), None),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
def build_targets(detector, recognizer, params: argparse.Namespace) -> List[Tuple[np.ndarray, str]]:
"""
Build targets using face detection and recognition.
Args:
detector (SCRFD): Face detector model.
recognizer (ArcFaceONNX): Face recognizer model.
params (argparse.Namespace): Command line arguments.
Returns:
List[Tuple[np.ndarray, str]]: A list of tuples containing feature vectors and corresponding image names.
"""
targets = []
for filename in os.listdir(params.faces_dir):
name = filename[:-4]
image_path = os.path.join(params.faces_dir, filename)
image = cv2.imread(image_path)
bboxes, kpss = detector.detect(image, max_num=1)
if len(kpss) == 0:
logging.warning(f"No face detected in {image_path}. Skipping...")
continue
embedding = recognizer(image, kpss[0])
targets.append((embedding, name))
return targets
def frame_processor(
frame: np.ndarray,
detector: SCRFD,
recognizer: ArcFace,
targets: List[Tuple[np.ndarray, str]],
colors: dict,
params: argparse.Namespace
) -> np.ndarray:
"""
Process a video frame for face detection and recognition.
Args:
frame (np.ndarray): The video frame.
detector (SCRFD): Face detector model.
recognizer (ArcFace): Face recognizer model.
targets (List[Tuple[np.ndarray, str]]): List of target feature vectors and names.
colors (dict): Dictionary of colors for drawing bounding boxes.
params (argparse.Namespace): Command line arguments.
Returns:
np.ndarray: The processed video frame.
"""
bboxes, kpss = detector.detect(frame, params.max_num)
for bbox, kps in zip(bboxes, kpss):
*bbox, conf_score = bbox.astype(np.int32)
embedding = recognizer(frame, kps)
max_similarity = 0
best_match_name = "Unknown"
for target, name in targets:
similarity = compute_similarity(target, embedding)
if similarity > max_similarity and similarity > params.similarity_thresh:
max_similarity = similarity
best_match_name = name
if best_match_name != "Unknown":
color = colors[best_match_name]
draw_bbox_info(frame, bbox, similarity=max_similarity, name=best_match_name, color=color)
else:
draw_bbox(frame, bbox, (255, 0, 0))
return frame
def main(params):
setup_logging(params.log_level)
detector = SCRFD(params.det_weight, input_size=(640, 640), conf_thres=params.confidence_thresh)
recognizer = ArcFace(params.rec_weight)
targets = build_targets(detector, recognizer, params)
colors = {name: (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) for _, name in targets}
cap = cv2.VideoCapture(params.source)
if not cap.isOpened():
raise Exception("Could not open video or webcam")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
out = cv2.VideoWriter("output_video.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
while True:
ret, frame = cap.read()
if not ret:
break
frame = frame_processor(frame, detector, recognizer, targets, colors, params)
out.write(frame)
cv2.imshow("Frame", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
args = parse_args()
if args.source.isdigit():
args.source = int(args.source)
main(args)