Skip to content

Commit

Permalink
facial area coordinates must be in image borders
Browse files Browse the repository at this point in the history
  • Loading branch information
serengil committed Aug 15, 2024
1 parent f3d1809 commit 3059170
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
13 changes: 8 additions & 5 deletions deepface/modules/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ def extract_faces(
if img is None:
raise ValueError(f"Exception while loading {img_name}")

base_region = FacialAreaRegion(x=0, y=0, w=img.shape[1], h=img.shape[0], confidence=0)
height, width, _ = img.shape

base_region = FacialAreaRegion(x=0, y=0, w=width, h=height, confidence=0)

if detector_backend == "skip":
face_objs = [DetectedFace(img=img, facial_area=base_region, confidence=0)]
Expand Down Expand Up @@ -137,10 +139,11 @@ def extract_faces(
if normalize_face:
current_img = current_img / 255 # normalize input in [0, 1]

x = int(current_region.x)
y = int(current_region.y)
w = int(current_region.w)
h = int(current_region.h)
# cast to int for flask, and do final checks for borders
x = max(0, int(current_region.x))
y = max(0, int(current_region.y))
w = min(width - x - 1, int(current_region.w))
h = min(height - y - 1, int(current_region.h))

resp_obj = {
"face": current_img,
Expand Down
Binary file added tests/dataset/selfie-many-people.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/test_extract_faces.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,28 @@ def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
return "data:image/jpeg," + encoded_string


def test_facial_coordinates_are_in_borders():
img_path = "dataset/selfie-many-people.jpg"
img = cv2.imread(img_path)
height, width, _ = img.shape

results = DeepFace.extract_faces(img_path=img_path)

assert len(results) > 0

for result in results:
facial_area = result["facial_area"]

x = facial_area["x"]
y = facial_area["y"]
w = facial_area["w"]
h = facial_area["h"]

assert x >= 0
assert y >= 0
assert x + w < width
assert y + h < height

logger.info("✅ facial area coordinates are all in image borders")

0 comments on commit 3059170

Please sign in to comment.