Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat task 2901 cleaning modules #990

Merged
merged 7 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ tests/dataset/*.pkl
tests/*.ipynb
tests/*.csv
*.pyc
**/.coverage
**/.coverage.*
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ test:
cd tests && python -m pytest . -s --disable-warnings

lint:
python -m pylint deepface/ --fail-under=10
python -m pylint deepface/ --fail-under=10

coverage:
pip install pytest-cov && cd tests && python -m pytest --cov=deepface
67 changes: 67 additions & 0 deletions deepface/DeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def verify(
distance_metric: str = "cosine",
enforce_detection: bool = True,
align: bool = True,
expand_percentage: int = 0,
normalization: str = "base",
) -> Dict[str, Any]:
"""
Expand All @@ -83,6 +84,8 @@ def verify(

align (bool): Flag to enable face alignment (default is True).

expand_percentage (int): expand detected facial area with a percentage (default is 0).

normalization (string): Normalize the input image before feeding it to the model.
Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace (default is base)

Expand Down Expand Up @@ -119,6 +122,7 @@ def verify(
distance_metric=distance_metric,
enforce_detection=enforce_detection,
align=align,
expand_percentage=expand_percentage,
normalization=normalization,
)

Expand All @@ -129,6 +133,7 @@ def analyze(
enforce_detection: bool = True,
detector_backend: str = "opencv",
align: bool = True,
expand_percentage: int = 0,
silent: bool = False,
) -> List[Dict[str, Any]]:
"""
Expand All @@ -152,6 +157,8 @@ def analyze(

align (boolean): Perform alignment based on the eye positions (default is True).

expand_percentage (int): expand detected facial area with a percentage (default is 0).

silent (boolean): Suppress or allow some log messages for a quieter analysis process
(default is False).

Expand Down Expand Up @@ -209,6 +216,7 @@ def analyze(
enforce_detection=enforce_detection,
detector_backend=detector_backend,
align=align,
expand_percentage=expand_percentage,
silent=silent,
)

Expand All @@ -221,6 +229,7 @@ def find(
enforce_detection: bool = True,
detector_backend: str = "opencv",
align: bool = True,
expand_percentage: int = 0,
threshold: Optional[float] = None,
normalization: str = "base",
silent: bool = False,
Expand Down Expand Up @@ -249,6 +258,8 @@ def find(

align (boolean): Perform alignment based on the eye positions (default is True).

expand_percentage (int): expand detected facial area with a percentage (default is 0).

threshold (float): Specify a threshold to determine whether a pair represents the same
person or different individuals. This threshold is used for comparing distances.
If left unset, default pre-tuned threshold values will be applied based on the specified
Expand Down Expand Up @@ -286,6 +297,7 @@ def find(
enforce_detection=enforce_detection,
detector_backend=detector_backend,
align=align,
expand_percentage=expand_percentage,
threshold=threshold,
normalization=normalization,
silent=silent,
Expand All @@ -298,6 +310,7 @@ def represent(
enforce_detection: bool = True,
detector_backend: str = "opencv",
align: bool = True,
expand_percentage: int = 0,
normalization: str = "base",
) -> List[Dict[str, Any]]:
"""
Expand All @@ -320,6 +333,8 @@ def represent(

align (boolean): Perform alignment based on the eye positions (default is True).

expand_percentage (int): expand detected facial area with a percentage (default is 0).

normalization (string): Normalize the input image before feeding it to the model.
Default is base. Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace
(default is base).
Expand All @@ -346,6 +361,7 @@ def represent(
enforce_detection=enforce_detection,
detector_backend=detector_backend,
align=align,
expand_percentage=expand_percentage,
normalization=normalization,
)

Expand Down Expand Up @@ -409,6 +425,7 @@ def extract_faces(
detector_backend: str = "opencv",
enforce_detection: bool = True,
align: bool = True,
expand_percentage: int = 0,
grayscale: bool = False,
) -> List[Dict[str, Any]]:
"""
Expand All @@ -429,6 +446,8 @@ def extract_faces(

align (bool): Flag to enable face alignment (default is True).

expand_percentage (int): expand detected facial area with a percentage (default is 0).

grayscale (boolean): Flag to convert the image to grayscale before
processing (default is False).

Expand All @@ -448,7 +467,9 @@ def extract_faces(
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
expand_percentage=expand_percentage,
grayscale=grayscale,
human_readable=True,
)


Expand All @@ -459,3 +480,49 @@ def cli() -> None:
import fire

fire.Fire()


# deprecated function(s)


def detectFace(
img_path: Union[str, np.ndarray],
target_size: tuple = (224, 224),
detector_backend: str = "opencv",
enforce_detection: bool = True,
align: bool = True,
) -> Union[np.ndarray, None]:
"""
Deprecated face detection function. Use extract_faces for same functionality.

Args:
img_path (str or np.ndarray): Path to the first image. Accepts exact image path
as a string, numpy array (BGR), or base64 encoded images.

target_size (tuple): final shape of facial image. black pixels will be
added to resize the image (default is (224, 224)).

detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).

enforce_detection (boolean): If no face is detected in an image, raise an exception.
Set to False to avoid the exception for low-resolution images (default is True).

align (bool): Flag to enable face alignment (default is True).

Returns:
img (np.ndarray): detected (and aligned) facial area image as numpy array
"""
logger.warn("Function detectFace is deprecated. Use extract_faces instead.")
face_objs = extract_faces(
img_path=img_path,
target_size=target_size,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
grayscale=False,
)
extracted_face = None
if len(face_objs) > 0:
extracted_face = face_objs[0]["face"]
return extracted_face
2 changes: 2 additions & 0 deletions deepface/basemodels/ArcFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class ArcFaceClient(FacialRecognition):
def __init__(self):
self.model = load_model()
self.model_name = "ArcFace"
self.input_shape = (112, 112)
self.output_shape = 512

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
2 changes: 2 additions & 0 deletions deepface/basemodels/DeepID.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class DeepIdClient(FacialRecognition):
def __init__(self):
self.model = load_model()
self.model_name = "DeepId"
self.input_shape = (47, 55)
self.output_shape = 160

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
2 changes: 2 additions & 0 deletions deepface/basemodels/Dlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class DlibClient(FacialRecognition):
def __init__(self):
self.model = DlibResNet()
self.model_name = "Dlib"
self.input_shape = (150, 150)
self.output_shape = 128

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
4 changes: 4 additions & 0 deletions deepface/basemodels/Facenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class FaceNet128dClient(FacialRecognition):
def __init__(self):
self.model = load_facenet128d_model()
self.model_name = "FaceNet-128d"
self.input_shape = (160, 160)
self.output_shape = 128

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand All @@ -75,6 +77,8 @@ class FaceNet512dClient(FacialRecognition):
def __init__(self):
self.model = load_facenet512d_model()
self.model_name = "FaceNet-512d"
self.input_shape = (160, 160)
self.output_shape = 512

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
2 changes: 2 additions & 0 deletions deepface/basemodels/FbDeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class DeepFaceClient(FacialRecognition):
def __init__(self):
self.model = load_model()
self.model_name = "DeepFace"
self.input_shape = (152, 152)
self.output_shape = 4096

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
2 changes: 2 additions & 0 deletions deepface/basemodels/OpenFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class OpenFaceClient(FacialRecognition):
def __init__(self):
self.model = load_model()
self.model_name = "OpenFace"
self.input_shape = (96, 96)
self.output_shape = 128

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
2 changes: 2 additions & 0 deletions deepface/basemodels/SFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class SFaceClient(FacialRecognition):
def __init__(self):
self.model = load_model()
self.model_name = "SFace"
self.input_shape = (112, 112)
self.output_shape = 128

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
2 changes: 2 additions & 0 deletions deepface/basemodels/VGGFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class VggFaceClient(FacialRecognition):
def __init__(self):
self.model = load_model()
self.model_name = "VGG-Face"
self.input_shape = (224, 224)
self.output_shape = 4096

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
Expand Down
6 changes: 3 additions & 3 deletions deepface/commons/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np


def findCosineDistance(
def find_cosine_distance(
source_representation: Union[np.ndarray, list], test_representation: Union[np.ndarray, list]
) -> np.float64:
if isinstance(source_representation, list):
Expand All @@ -17,7 +17,7 @@ def findCosineDistance(
return 1 - (a / (np.sqrt(b) * np.sqrt(c)))


def findEuclideanDistance(
def find_euclidean_distance(
source_representation: Union[np.ndarray, list], test_representation: Union[np.ndarray, list]
) -> np.float64:
if isinstance(source_representation, list):
Expand All @@ -38,7 +38,7 @@ def l2_normalize(x: Union[np.ndarray, list]) -> np.ndarray:
return x / np.sqrt(np.sum(np.multiply(x, x)))


def findThreshold(model_name: str, distance_metric: str) -> float:
def find_threshold(model_name: str, distance_metric: str) -> float:

base_threshold = {"cosine": 0.40, "euclidean": 0.55, "euclidean_l2": 0.75}

Expand Down