review chanages

This commit is contained in:
raghucharan16 2025-03-01 12:18:09 +05:30
parent 2a6b222d52
commit 527a89c931
2 changed files with 31 additions and 24 deletions

View File

@ -1,5 +1,5 @@
import os import os
from typing import List from typing import List, Union
import numpy as np import numpy as np
from deepface.commons import weight_utils, folder_utils from deepface.commons import weight_utils, folder_utils
@ -25,12 +25,12 @@ class Buffalo_L(FacialRecognition):
raise ModuleNotFoundError( raise ModuleNotFoundError(
"InsightFace and its dependencies are optional for the Buffalo_L model. " "InsightFace and its dependencies are optional for the Buffalo_L model. "
"Please install them with: " "Please install them with: "
"pip install insightface>=0.7.3 onnxruntime>=1.9.0 typing-extensions pydantic" "pip install insightface>=0.7.3 onnxruntime>=1.9.0 typing-extensions pydantic albumentations"
) from err ) from err
# Define the model filename and subdirectory # Define the model filename and subdirectory
sub_dir = "buffalo_l" sub_dir = "buffalo_l"
model_file = "w600k_r50.onnx" model_file = "webface_r50.onnx"
model_rel_path = os.path.join(sub_dir, model_file) model_rel_path = os.path.join(sub_dir, model_file)
# Get the DeepFace home directory and construct weights path # Get the DeepFace home directory and construct weights path
@ -46,7 +46,7 @@ class Buffalo_L(FacialRecognition):
# Download the model weights if not already present # Download the model weights if not already present
weights_path = weight_utils.download_weights_if_necessary( weights_path = weight_utils.download_weights_if_necessary(
file_name=model_rel_path, file_name=model_rel_path,
source_url="https://drive.google.com/uc?export=download&confirm=pbef&id=1N0GL-8ehw_bz2eZQWz2b0A5XBdXdxZhg" # pylint: disable=line-too-long source_url="https://drive.google.com/uc?export=download&confirm=pbef&id=1N0GL-8ehw_bz2eZQWz2b0A5XBdXdxZhg" # pylint: disable=line-too-long
) )
# Verify the model file exists # Verify the model file exists
@ -55,20 +55,27 @@ class Buffalo_L(FacialRecognition):
else: else:
raise FileNotFoundError(f"Model file not found at: {weights_path}") raise FileNotFoundError(f"Model file not found at: {weights_path}")
# Load the model # Load the model using the full path
self.model = get_model(model_file, root=buffalo_l_dir) self.model = get_model(weights_path)
self.model.prepare(ctx_id=-1, input_size=self.input_shape) self.model.prepare(ctx_id=-1, input_size=self.input_shape)
def preprocess(self, img: np.ndarray) -> np.ndarray: def preprocess(self, img: np.ndarray) -> np.ndarray:
""" """
Preprocess the image to match InsightFace recognition model expectations. Preprocess the image or batch of images to match InsightFace recognition model expectations.
Args: Args:
img: Image in shape (1, 112, 112, 3) or (112, 112, 3) img: Image in shape (1, 112, 112, 3) or (112, 112, 3) or batch (batch_size, 112, 112, 3)
Returns: Returns:
Preprocessed image as numpy array Preprocessed image or batch as numpy array
""" """
if len(img.shape) == 4: if len(img.shape) == 4: # Batch of images
img = img[0] preprocessed_imgs = []
for i in range(img.shape[0]):
single_img = img[i]
if single_img.max() <= 1.0:
single_img = (single_img * 255).astype(np.uint8)
single_img = single_img[:, :, ::-1] # Convert RGB to BGR
preprocessed_imgs.append(single_img)
return np.array(preprocessed_imgs)
if len(img.shape) != 3: if len(img.shape) != 3:
raise ValueError( raise ValueError(
f"Expected image to be 3D after preprocessing, but got shape: {img.shape}") f"Expected image to be 3D after preprocessing, but got shape: {img.shape}")
@ -77,21 +84,20 @@ class Buffalo_L(FacialRecognition):
img = img[:, :, ::-1] # Convert RGB to BGR img = img[:, :, ::-1] # Convert RGB to BGR
return img return img
def forward(self, img: np.ndarray) -> List[float]: def forward(self, img: np.ndarray) -> Union[List[float], List[List[float]]]:
""" """
Extract face embedding from a pre-cropped face image. Extract face embedding from a pre-cropped face image or batch of images.
Args: Args:
img: Preprocessed face image with shape (1, 112, 112, 3) img: Preprocessed face image with shape (1, 112, 112, 3) or batch (batch_size, 112, 112, 3)
Returns: Returns:
Face embedding as a list of floats Face embedding as a list of floats (single image) or list of lists of floats (batch)
""" """
img = self.preprocess(img) img = self.preprocess(img)
embedding = self.model.get_feat(img) if len(img.shape) == 4: # Batch
if isinstance(embedding, np.ndarray) and len(embedding.shape) > 1: embeddings = self.model.get_feat(img)
embedding = embedding.flatten() return [embedding.tolist() for embedding in embeddings]
elif isinstance(embedding, list): elif len(img.shape) == 3: # Single image
embedding = np.array(embedding).flatten() embedding = self.model.get_feat(np.expand_dims(img, axis=0))[0]
return embedding.tolist()
else: else:
raise ValueError(f"Unexpected embedding type: {type(embedding)}") raise ValueError(f"Unexpected embedding type after preprocessing: {img.shape}")
return embedding.tolist()

View File

@ -8,4 +8,5 @@ insightface>=0.7.3
onnxruntime>=1.9.0 onnxruntime>=1.9.0
tf-keras tf-keras
typing-extensions typing-extensions
pydantic pydantic
albumentations