-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognize.py
More file actions
48 lines (39 loc) · 1.7 KB
/
recognize.py
File metadata and controls
48 lines (39 loc) · 1.7 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
import numpy as np
from scipy.spatial.distance import cosine
import cv2
import face_recognition
from align_faces import align_face
# Load known face embeddings from a .npy file
def load_known_embeddings(embedding_file):
return np.load(embedding_file, allow_pickle=True).item()
# Helper function to convert distance to confidence percentage
def distance_to_confidence(distance):
return max(0, (1 - distance) * 100)
# Compare the current face embedding with known embeddings
def recognize_face(embedding, known_embeddings, threshold=0.10):
min_distance = float('inf')
recognized_person = None
for person, embeddings in known_embeddings.items():
for known_embedding in embeddings:
distance = cosine(embedding, known_embedding)
if distance < min_distance:
min_distance = distance
recognized_person = person
# Convert minimum distance to confidence percentage
confidence = distance_to_confidence(min_distance)
# If confidence is above the threshold, return the recognized person
if confidence >= 96: # 96% confidence
return recognized_person, confidence
else:
return "Unknown", confidence
# Function to get embedding from an image
def get_embedding(image_path):
aligned_face = align_face(image_path)
if aligned_face is not None:
# Convert image to RGB as face_recognition expects RGB format
rgb_face = cv2.cvtColor(aligned_face, cv2.COLOR_BGR2RGB)
# Get face encodings (embeddings) for the aligned face
face_encodings = face_recognition.face_encodings(rgb_face)
if face_encodings:
return face_encodings[0] # Assuming one face per image
return None