From 5a73d917444b4a386b6575cd0660efccc2b3ac54 Mon Sep 17 00:00:00 2001 From: Raghucharan16 Date: Thu, 20 Feb 2025 23:08:09 +0530 Subject: [PATCH] code review changes --- .../models/facial_recognition/Buffalo_L.py | 47 +++++++------------ deepface/modules/verification.py | 1 + requirements.txt | 5 +- requirements_additional.txt | 5 +- 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/deepface/models/facial_recognition/Buffalo_L.py b/deepface/models/facial_recognition/Buffalo_L.py index 0ec2365..fc01a4f 100644 --- a/deepface/models/facial_recognition/Buffalo_L.py +++ b/deepface/models/facial_recognition/Buffalo_L.py @@ -1,11 +1,19 @@ import cv2 import numpy as np -from insightface.model_zoo import get_model from deepface.models.FacialRecognition import FacialRecognition from deepface.commons.logger import Logger logger = Logger() +# Check for insightface dependency +try: + from insightface.model_zoo import get_model +except ModuleNotFoundError: + raise ModuleNotFoundError( + "InsightFace is an optional dependency for the Buffalo_L model." + "You can install it with: pip install insightface>=0.7.3" + ) + class Buffalo_L(FacialRecognition): def __init__(self): self.model = None @@ -17,8 +25,6 @@ class Buffalo_L(FacialRecognition): """ Load the InsightFace Buffalo_L recognition model. """ - # Load the recognition model directly (e.g., w600k_r50 from buffalo_l) - # The buffalo_l package includes recognition model weights self.model = get_model('buffalo_l/w600k_r50.onnx', download=True) self.model.prepare(ctx_id=-1, input_size=self.input_shape) # ctx_id=-1 for CPU @@ -32,12 +38,10 @@ class Buffalo_L(FacialRecognition): """ if len(img.shape) == 4: # (1, 112, 112, 3) img = img[0] # Remove batch dimension - # Ensure image is in uint8 format (0-255 range) if img.max() <= 1.0: # If normalized to [0, 1] img = (img * 255).astype(np.uint8) - # Convert to BGR if in RGB (InsightFace expects BGR) - if img.shape[2] == 3: - img = img[:, :, ::-1] # RGB to BGR + # Always convert RGB to BGR (InsightFace expects BGR, DeepFace provides RGB) + img = img[:, :, ::-1] return img def forward(self, img): @@ -48,29 +52,14 @@ class Buffalo_L(FacialRecognition): Returns: numpy array: Face embedding """ - # Preprocess the input image img = self.preprocess(img) - - # Extract embedding directly (no detection needed) embedding = self.model.get_feat(img) - # InsightFace recognition models return a list or array; ensure 1D output - if isinstance(embedding, (list, np.ndarray)) and len(embedding.shape) > 1: - embedding = embedding.flatten() + # Handle different embedding formats + if isinstance(embedding, np.ndarray): + if len(embedding.shape) > 1: + embedding = embedding.flatten() + elif isinstance(embedding, list): + embedding = np.array(embedding).flatten() - return embedding - - def verify(self, img1, img2, threshold=0.65): - """ - Verify if two images contain the same person using cosine similarity. - Args: - img1, img2 (numpy arrays): Preprocessed images - threshold (float): Cosine similarity threshold - Returns: - tuple: (similarity_score, is_same_person) - """ - emb1 = self.forward(img1) - emb2 = self.forward(img2) - - similarity = np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2)) - return similarity, similarity > threshold \ No newline at end of file + return embedding \ No newline at end of file diff --git a/deepface/modules/verification.py b/deepface/modules/verification.py index 43c3ba9..2e827b8 100644 --- a/deepface/modules/verification.py +++ b/deepface/modules/verification.py @@ -422,6 +422,7 @@ def find_threshold(model_name: str, distance_metric: str) -> float: "DeepFace": {"cosine": 0.23, "euclidean": 64, "euclidean_l2": 0.64}, "DeepID": {"cosine": 0.015, "euclidean": 45, "euclidean_l2": 0.17}, "GhostFaceNet": {"cosine": 0.65, "euclidean": 35.71, "euclidean_l2": 1.10}, + "Buffalo_L": {"cosine": 0.65, "euclidean": 11.13, "euclidean_l2": 1.1}, } threshold = thresholds.get(model_name, base_threshold).get(distance_metric, 0.4) diff --git a/requirements.txt b/requirements.txt index fc28fd7..afb1518 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,4 @@ flask_cors>=4.0.1 mtcnn>=0.1.0 retina-face>=0.0.14 fire>=0.4.0 -gunicorn>=20.1.0 -tf-keras -insightface==0.7.3 -onnxruntime>=1.9.0 \ No newline at end of file +gunicorn>=20.1.0 \ No newline at end of file diff --git a/requirements_additional.txt b/requirements_additional.txt index ea76fde..94d82dd 100644 --- a/requirements_additional.txt +++ b/requirements_additional.txt @@ -3,4 +3,7 @@ mediapipe>=0.8.7.3 dlib>=19.20.0 ultralytics>=8.0.122 facenet-pytorch>=2.5.3 -torch>=2.1.2 \ No newline at end of file +torch>=2.1.2 +insightface>=0.7.3 +onnxruntime>=1.9.0 +tf-keras \ No newline at end of file