Skip to content

Commit

Permalink
Merge pull request serengil#1247 from serengil/feat-task-0206-alignme…
Browse files Browse the repository at this point in the history
…t-bug-when-faces-are-close-to-border

Add a black border around an image to avoid moving faces outside afte…
  • Loading branch information
serengil authored Jun 2, 2024
2 parents 188e15c + 578f3e3 commit dcd15cf
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions deepface/detectors/DetectorWrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, List, Tuple
import numpy as np
import cv2
from deepface.modules import detection
from deepface.models.Detector import Detector, DetectedFace, FacialAreaRegion
from deepface.detectors import (
Expand Down Expand Up @@ -84,6 +85,8 @@ def detect_faces(
- confidence (float): The confidence score associated with the detected face.
"""
height, width, _ = img.shape

face_detector: Detector = build_model(detector_backend)

# validate expand percentage score
Expand All @@ -94,6 +97,19 @@ def detect_faces(
)
expand_percentage = 0

# If faces are close to the upper boundary, alignment move them outside
# Add a black border around an image to avoid this.
if align is True:
img = cv2.copyMakeBorder(
img,
int(0.5 * height),
int(0.5 * height),
int(0.5 * width),
int(0.5 * width),
cv2.BORDER_CONSTANT,
value=[0, 0, 0], # Color of the border (black)
)

# find facial areas of given image
facial_areas = face_detector.detect_faces(img)

Expand Down Expand Up @@ -126,6 +142,7 @@ def detect_faces(
aligned_img, angle = detection.align_face(
img=img, left_eye=left_eye, right_eye=right_eye
)

rotated_x1, rotated_y1, rotated_x2, rotated_y2 = rotate_facial_area(
facial_area=(x, y, x + w, y + h), angle=angle, size=(img.shape[0], img.shape[1])
)
Expand Down

0 comments on commit dcd15cf

Please sign in to comment.