-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface.py
More file actions
181 lines (153 loc) · 6.01 KB
/
Copy pathface.py
File metadata and controls
181 lines (153 loc) · 6.01 KB
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
import cv2
import numpy as np
import onnxruntime as ort
import torch
import os
from collections import OrderedDict
from tqdm import tqdm
from torchvision.models import resnet50, ResNet50_Weights
from models.retinaface import RetinaFace
from data.config import cfg_re50, cfg_mnet
from utils.box_utils import decode, decode_landm
from layers.functions.prior_box import PriorBox
# -------- Config -------- #
ONNX_MODEL_PATH = "facenet512.onnx"
THRESHOLD = 0.55 # Cosine similarity threshold
DETECTION_PROB_THRESHOLD = 0.90 # RetinaFace confidence threshold
RETINAFACE_NETWORK = "resnet50" # or "mobilenet0.25"
RETINAFACE_MODEL_PATH = "weights/Resnet50_Final.pth"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# -------- RetinaFace Init -------- #
if RETINAFACE_NETWORK == "resnet50":
cfg = cfg_re50
elif RETINAFACE_NETWORK == "mobilenet0.25":
cfg = cfg_mnet
else:
raise ValueError("Unsupported network.")
net = RetinaFace(cfg=cfg, phase='test')
state_dict = torch.load(RETINAFACE_MODEL_PATH, map_location=lambda storage, loc: storage, weights_only=True)
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k.replace('module.', '') if k.startswith('module.') else k
new_state_dict[name] = v
net.load_state_dict(new_state_dict)
net.eval()
if DEVICE == "cuda":
net = net.cuda()
# -------- FaceNet Init -------- #
session = ort.InferenceSession("facenet512.onnx", providers=["CPUExecutionProvider"])
input_name = session.get_inputs()[0].name
def preprocess(img):
img = cv2.resize(img, (160, 160))
img = img.astype(np.float32)
mean, std = img.mean(), img.std()
img = (img - mean) / (std + 1e-6)
img = np.expand_dims(img, axis=0)
return img
def get_embedding(img):
img = preprocess(img)
emb = session.run(None, {input_name: img})[0][0]
return emb / np.linalg.norm(emb)
def cosine_similarity(a, b):
return np.dot(a, b)
def extract_faces(frame):
img = np.float32(frame)
im_height, im_width, _ = img.shape
scale = torch.Tensor([im_width, im_height, im_width, im_height])
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0)
if DEVICE == "cuda":
img = img.cuda()
scale = scale.cuda()
with torch.no_grad():
loc, conf, landms = net(img)
priorbox = PriorBox(cfg, image_size=(im_height, im_width))
priors = priorbox.forward()
if DEVICE == "cuda":
priors = priors.cuda()
boxes = decode(loc.data.squeeze(0), priors, cfg['variance'])
boxes = boxes * scale
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
inds = np.where(scores > DETECTION_PROB_THRESHOLD)[0]
boxes = boxes[inds]
scores = scores[inds]
faces = []
for box, prob in zip(boxes, scores):
x1, y1, x2, y2 = map(int, box)
face = frame[y1:y2, x1:x2]
if face.size > 0:
faces.append((face, prob))
return faces
def seconds_to_timestamp(sec):
h = int(sec // 3600)
m = int((sec % 3600) // 60)
s = int(sec % 60)
return f"{h:02}:{m:02}:{s:02}"
def main(reference_img_path, video_path):
if not os.path.exists(reference_img_path) or not os.path.exists(video_path):
print("Reference image or video not found.")
return
print("[+] Loading reference image...")
ref_img = cv2.imread(reference_img_path)
ref_faces = extract_faces(ref_img)
if not ref_faces:
print("No face detected in reference image.")
return
ref_face, ref_prob = ref_faces[0]
ref_embedding = get_embedding(ref_face)
print(f"[+] Reference face detected with probability: {ref_prob:.2f}")
print("[+] Processing video...")
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_idx = 0
matched_frames = []
detection_probs = []
with tqdm(total=total_frames, desc="Processing frames") as pbar:
while True:
ret, frame = cap.read()
if not ret:
break
faces = extract_faces(frame)
for face, prob in faces:
try:
emb = get_embedding(face)
sim = cosine_similarity(ref_embedding, emb)
if sim > THRESHOLD:
timestamp_sec = frame_idx / fps
matched_frames.append(timestamp_sec)
detection_probs.append(prob)
break
except Exception:
continue
frame_idx += 1
pbar.update(1)
cap.release()
if not matched_frames:
print("No matching face found in the video.")
return
ranges = []
start = matched_frames[0]
for i in range(1, len(matched_frames)):
if matched_frames[i] - matched_frames[i - 1] > 1.5:
end = matched_frames[i - 1]
ranges.append((start, end))
start = matched_frames[i]
ranges.append((start, matched_frames[-1]))
avg_prob = np.mean(detection_probs)
print(f"\n Face matched in {len(matched_frames)} frames.")
print(f" Average detection probability of matched faces: {avg_prob:.2f}")
print(f" First appearance: {seconds_to_timestamp(matched_frames[0])}")
print(f" Last appearance: {seconds_to_timestamp(matched_frames[-1])}")
print("\n Appearance intervals:")
for start, end in ranges:
print(f" {seconds_to_timestamp(start)} → {seconds_to_timestamp(end)}")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--image", type=str, required=True, help="Path to reference face image")
parser.add_argument("--video", type=str, required=True, help="Path to input video")
args = parser.parse_args()
main(args.image, args.video)