From cfff58792ab20dd654a602629a5c7c0a31ed2b2d Mon Sep 17 00:00:00 2001 From: RazaProdigy Date: Sun, 17 Dec 2023 13:59:32 +0400 Subject: [PATCH 1/3] Added type hints --- deepface/DeepFace.py | 101 ++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 8058c6f..ecc62fe 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -5,6 +5,7 @@ import warnings import time import pickle import logging +from typing import Any, Dict, List, Tuple # 3rd party dependencies import numpy as np @@ -45,7 +46,7 @@ if tf_version == 2: # ----------------------------------- -def build_model(model_name): +def build_model(model_name: str) -> Any: """ This function builds a deepface model Parameters: @@ -91,15 +92,15 @@ def build_model(model_name): def verify( - img1_path, - img2_path, - model_name="VGG-Face", - detector_backend="opencv", - distance_metric="cosine", - enforce_detection=True, - align=True, - normalization="base", -): + img1_path: str, + img2_path: str, + model_name: str = "VGG-Face", + detector_backend: str = "opencv", + distance_metric: str = "cosine", + enforce_detection: bool = True, + align: bool = True, + normalization: str = "base", +) -> Dict[str, Any]: """ 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 @@ -231,13 +232,13 @@ def verify( def analyze( - img_path, - actions=("emotion", "age", "gender", "race"), - enforce_detection=True, - detector_backend="opencv", - align=True, - silent=False, -): + img_path: str, + actions: Tuple[str, ...] = ("emotion", "age", "gender", "race"), + enforce_detection: bool = True, + detector_backend: str = "opencv", + align: bool = True, + silent: bool = False, +) -> List[Dict[str, Any]]: """ This function analyzes facial attributes including age, gender, emotion and race. In the background, analysis function builds convolutional neural network models to @@ -409,16 +410,16 @@ def analyze( def find( - img_path, - db_path, - model_name="VGG-Face", - distance_metric="cosine", - enforce_detection=True, - detector_backend="opencv", - align=True, - normalization="base", - silent=False, -): + img_path: str, + db_path : str, + model_name : str ="VGG-Face", + distance_metric : str ="cosine", + enforce_detection : bool =True, + detector_backend : str ="opencv", + align : bool = True, + normalization : str ="base", + silent : bool = False, +) -> List[pd.DataFrame]: """ This function applies verification several times and find the identities in a database @@ -650,13 +651,13 @@ def find( def represent( - img_path, - model_name="VGG-Face", - enforce_detection=True, - detector_backend="opencv", - align=True, - normalization="base", -): + img_path: str, + model_name: str = "VGG-Face", + enforce_detection: bool = True, + detector_backend: str = "opencv", + align: bool = True, + normalization: str = "base", +) -> List[Dict[str, Any]]: """ This function represents facial images as vectors. The function uses convolutional neural networks models to generate vector embeddings. @@ -760,15 +761,15 @@ def represent( def stream( - db_path="", - model_name="VGG-Face", - detector_backend="opencv", - distance_metric="cosine", - enable_face_analysis=True, - source=0, - time_threshold=5, - frame_threshold=5, -): + db_path: str = "", + model_name: str = "VGG-Face", + detector_backend: str = "opencv", + distance_metric: str = "cosine", + enable_face_analysis: bool = True, + source: Any = 0, + time_threshold: int = 5, + frame_threshold: int = 5, +) -> None: """ This function applies real time face recognition and facial attribute analysis @@ -816,13 +817,13 @@ def stream( def extract_faces( - img_path, - target_size=(224, 224), - detector_backend="opencv", - enforce_detection=True, - align=True, - grayscale=False, -): + img_path: str, + target_size: Tuple[int, int] = (224, 224), + detector_backend: str = "opencv", + enforce_detection: bool = True, + align: bool = True, + grayscale: bool = False, +) -> List[Dict[str, Any]]: """ This function applies pre-processing stages of a face recognition pipeline including detection and alignment From 90a0282ed94fec2a99979e96864642534402272b Mon Sep 17 00:00:00 2001 From: RazaProdigy Date: Mon, 18 Dec 2023 13:18:57 +0400 Subject: [PATCH 2/3] Refine type hints as per PR review --- deepface/DeepFace.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index ecc62fe..9a4725a 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -5,7 +5,9 @@ import warnings import time import pickle import logging -from typing import Any, Dict, List, Tuple +from typing import Any, Dict, List, Tuple, Union +from keras.engine.functional import Functional +from deepface.basemodels import DlibResNet, SFace # 3rd party dependencies import numpy as np @@ -46,7 +48,7 @@ if tf_version == 2: # ----------------------------------- -def build_model(model_name: str) -> Any: +def build_model(model_name: str) -> Union[Functional, DlibResNet.DlibResNet, SFace.SFaceModel]: """ This function builds a deepface model Parameters: @@ -92,8 +94,8 @@ def build_model(model_name: str) -> Any: def verify( - img1_path: str, - img2_path: str, + img1_path: Union[str, np.ndarray], + img2_path: Union[str, np.ndarray], model_name: str = "VGG-Face", detector_backend: str = "opencv", distance_metric: str = "cosine", @@ -232,7 +234,7 @@ def verify( def analyze( - img_path: str, + img_path: Union[str, np.ndarray],, actions: Tuple[str, ...] = ("emotion", "age", "gender", "race"), enforce_detection: bool = True, detector_backend: str = "opencv", @@ -410,7 +412,7 @@ def analyze( def find( - img_path: str, + img_path: Union[str, np.ndarray], db_path : str, model_name : str ="VGG-Face", distance_metric : str ="cosine", @@ -651,7 +653,7 @@ def find( def represent( - img_path: str, + img_path: Union[str, np.ndarray], model_name: str = "VGG-Face", enforce_detection: bool = True, detector_backend: str = "opencv", @@ -817,7 +819,7 @@ def stream( def extract_faces( - img_path: str, + img_path: Union[str, np.ndarray], target_size: Tuple[int, int] = (224, 224), detector_backend: str = "opencv", enforce_detection: bool = True, From aa96a76cb266bd88e0562264b7cbd7f4fd523030 Mon Sep 17 00:00:00 2001 From: RazaProdigy Date: Mon, 18 Dec 2023 13:25:36 +0400 Subject: [PATCH 3/3] Removed typo from code --- deepface/DeepFace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 9a4725a..5989a2e 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -234,7 +234,7 @@ def verify( def analyze( - img_path: Union[str, np.ndarray],, + img_path: Union[str, np.ndarray], actions: Tuple[str, ...] = ("emotion", "age", "gender", "race"), enforce_detection: bool = True, detector_backend: str = "opencv",