Merge pull request #922 from RazaProdigy/typehints

Adding Type Hints for Enhanced Code Clarity and Maintenance
This commit is contained in:
Sefik Ilkin Serengil 2023-12-18 09:30:32 +00:00 committed by GitHub
commit 8a5f2875fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,9 @@ import warnings
import time import time
import pickle import pickle
import logging import logging
from typing import Any, Dict, List, Tuple, Union
from keras.engine.functional import Functional
from deepface.basemodels import DlibResNet, SFace
# 3rd party dependencies # 3rd party dependencies
import numpy as np import numpy as np
@ -45,7 +48,7 @@ if tf_version == 2:
# ----------------------------------- # -----------------------------------
def build_model(model_name): def build_model(model_name: str) -> Union[Functional, DlibResNet.DlibResNet, SFace.SFaceModel]:
""" """
This function builds a deepface model This function builds a deepface model
Parameters: Parameters:
@ -91,15 +94,15 @@ def build_model(model_name):
def verify( def verify(
img1_path, img1_path: Union[str, np.ndarray],
img2_path, img2_path: Union[str, np.ndarray],
model_name="VGG-Face", model_name: str = "VGG-Face",
detector_backend="opencv", detector_backend: str = "opencv",
distance_metric="cosine", distance_metric: str = "cosine",
enforce_detection=True, enforce_detection: bool = True,
align=True, align: bool = True,
normalization="base", normalization: str = "base",
): ) -> Dict[str, Any]:
""" """
This function verifies an image pair is same person or different persons. In the background, This function verifies an image pair is same person or different persons. In the background,
verification function represents facial images as vectors and then calculates the similarity verification function represents facial images as vectors and then calculates the similarity
@ -231,13 +234,13 @@ def verify(
def analyze( def analyze(
img_path, img_path: Union[str, np.ndarray],
actions=("emotion", "age", "gender", "race"), actions: Tuple[str, ...] = ("emotion", "age", "gender", "race"),
enforce_detection=True, enforce_detection: bool = True,
detector_backend="opencv", detector_backend: str = "opencv",
align=True, align: bool = True,
silent=False, silent: bool = False,
): ) -> List[Dict[str, Any]]:
""" """
This function analyzes facial attributes including age, gender, emotion and race. This function analyzes facial attributes including age, gender, emotion and race.
In the background, analysis function builds convolutional neural network models to In the background, analysis function builds convolutional neural network models to
@ -409,16 +412,16 @@ def analyze(
def find( def find(
img_path, img_path: Union[str, np.ndarray],
db_path, db_path : str,
model_name="VGG-Face", model_name : str ="VGG-Face",
distance_metric="cosine", distance_metric : str ="cosine",
enforce_detection=True, enforce_detection : bool =True,
detector_backend="opencv", detector_backend : str ="opencv",
align=True, align : bool = True,
normalization="base", normalization : str ="base",
silent=False, silent : bool = False,
): ) -> List[pd.DataFrame]:
""" """
This function applies verification several times and find the identities in a database This function applies verification several times and find the identities in a database
@ -650,13 +653,13 @@ def find(
def represent( def represent(
img_path, img_path: Union[str, np.ndarray],
model_name="VGG-Face", model_name: str = "VGG-Face",
enforce_detection=True, enforce_detection: bool = True,
detector_backend="opencv", detector_backend: str = "opencv",
align=True, align: bool = True,
normalization="base", normalization: str = "base",
): ) -> List[Dict[str, Any]]:
""" """
This function represents facial images as vectors. The function uses convolutional neural This function represents facial images as vectors. The function uses convolutional neural
networks models to generate vector embeddings. networks models to generate vector embeddings.
@ -760,15 +763,15 @@ def represent(
def stream( def stream(
db_path="", db_path: str = "",
model_name="VGG-Face", model_name: str = "VGG-Face",
detector_backend="opencv", detector_backend: str = "opencv",
distance_metric="cosine", distance_metric: str = "cosine",
enable_face_analysis=True, enable_face_analysis: bool = True,
source=0, source: Any = 0,
time_threshold=5, time_threshold: int = 5,
frame_threshold=5, frame_threshold: int = 5,
): ) -> None:
""" """
This function applies real time face recognition and facial attribute analysis This function applies real time face recognition and facial attribute analysis
@ -816,13 +819,13 @@ def stream(
def extract_faces( def extract_faces(
img_path, img_path: Union[str, np.ndarray],
target_size=(224, 224), target_size: Tuple[int, int] = (224, 224),
detector_backend="opencv", detector_backend: str = "opencv",
enforce_detection=True, enforce_detection: bool = True,
align=True, align: bool = True,
grayscale=False, grayscale: bool = False,
): ) -> List[Dict[str, Any]]:
""" """
This function applies pre-processing stages of a face recognition pipeline This function applies pre-processing stages of a face recognition pipeline
including detection and alignment including detection and alignment