From 38a2cf094243a5838dff44096874a2e1bc2a8e59 Mon Sep 17 00:00:00 2001 From: Narra_Venkata_Raghu_Charan Date: Tue, 18 Feb 2025 23:28:04 +0530 Subject: [PATCH] Create test_buffalo_l --- tests/test_buffalo_l | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/test_buffalo_l diff --git a/tests/test_buffalo_l b/tests/test_buffalo_l new file mode 100644 index 0000000..4e3f1ca --- /dev/null +++ b/tests/test_buffalo_l @@ -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.65): # Adjust this threshold according to your usecase. + """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 = "dataset/img1.jpg" +image2_path = "dataset/img2.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)}")