Merge pull request #1485 from RUTUPARNk/de_closured_validLandmark

This commit is contained in:
Sefik Ilkin Serengil 2025-07-01 13:08:34 +01:00 committed by GitHub
commit e502170592
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 26 deletions

View File

@ -17,6 +17,23 @@ logger = Logger()
# pylint: disable=no-else-raise
def is_valid_landmark(coord: Optional[Union[tuple, list]], width: int, height: int) -> bool:
"""
Check if a landmark coordinate is within valid image bounds.
Args:
coord (tuple or list or None): (x, y) coordinate to check.
width (int): Image width.
height (int): Image height.
Returns:
bool: True if coordinate is valid and within bounds, False otherwise.
"""
if coord is None:
return False
if not (isinstance(coord, (tuple, list)) and len(coord) == 2):
return False
x, y = coord
return 0 <= x < width and 0 <= y < height
def extract_faces(
img_path: Union[str, np.ndarray, IO[bytes]],
@ -90,22 +107,6 @@ def extract_faces(
height, width, _ = img.shape
def is_valid_landmark(coord: Optional[Union[tuple, list]]) -> bool:
"""
Check if a landmark coordinate is within valid image bounds.
Args:
coord (tuple or list or None): (x, y) coordinate to check.
Returns:
bool: True if coordinate is valid and within bounds, False otherwise.
"""
if coord is None:
return False
if not (isinstance(coord, (tuple, list)) and len(coord) == 2):
return False
x, y = coord
return 0 <= x < width and 0 <= y < height
base_region = FacialAreaRegion(x=0, y=0, w=width, h=height, confidence=0)
if detector_backend == "skip":
@ -175,7 +176,7 @@ def extract_faces(
# Sanitize landmarks - set invalid ones to None
for key, value in landmarks.items():
if not is_valid_landmark(value):
if not is_valid_landmark(value, width, height):
landmarks[key] = None

View File

@ -1,18 +1,10 @@
import numpy as np
import pytest
from deepface.modules.detection import extract_faces, DetectedFace, FacialAreaRegion
from deepface.modules.detection import extract_faces, DetectedFace, FacialAreaRegion, is_valid_landmark
from deepface.commons.logger import Logger
logger = Logger()
def is_valid_landmark(coord, width, height):
if coord is None:
return False
if not (isinstance(coord, (tuple, list)) and len(coord) == 2):
return False
x, y = coord
return 0 <= x < width and 0 <= y < height
def sanitize_landmarks(region, width, height):
landmarks = {
"left_eye": region.left_eye,