Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Face-recognition-using-insightface #1884

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Recognition-Algorithms/Face-recognition-using-insightface
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import cv2
import insightface
import numpy as np
from insightface.app import FaceAnalysis

# Initialize face analysis model
app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider']) # Use 'CUDAExecutionProvider' for GPU
app.prepare(ctx_id=-1) # ctx_id=-1 for CPU, 0 for GPU

def get_face_embedding(image_path):
"""Extract face embedding from an image"""
img = cv2.imread(image_path)
if img is None:
raise ValueError(f"Could not read image: {image_path}")

faces = app.get(img)

if len(faces) < 1:
raise ValueError("No faces detected in the image")
if len(faces) > 1:
print("Warning: Multiple faces detected. Using first detected face")

return faces[0].embedding

def compare_faces(emb1, emb2, threshold=0.6):
"""Compare two embeddings using cosine similarity"""
similarity = np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
return similarity, similarity > threshold

# Paths to your Indian face images
image1_path = "path/to/face1.jpg"
image2_path = "path/to/face2.jpg"

try:
# Get embeddings
emb1 = get_face_embedding(image1_path)
emb2 = get_face_embedding(image2_path)

# Compare faces
similarity_score, is_same_person = compare_faces(emb1, emb2)

print(f"Similarity Score: {similarity_score:.4f}")
print(f"Same person? {'YES' if is_same_person else 'NO'}")

except Exception as e:
print(f"Error: {str(e)}")