function and variable names updated

This commit is contained in:
Sefik Ilkin Serengil 2024-12-26 12:47:02 +00:00
parent 701eca8812
commit 3d914f1337

View File

@ -301,15 +301,25 @@ def expand_and_align_face(
detected_face = img[int(y) : int(y + h), int(x) : int(x + w)]
# align original image, then find projection of detected face area after alignment
if align is True: # and left_eye is not None and right_eye is not None:
processed_face, new_x, new_y = alignment_preprocess(img=img, facial_area=(x,y,w,h))
# we were aligning the original image before, but this comes with an extra cost
# instead we now focus on the facial area with a margin
# and align it instead of original image to decrese the cost
expanded_face, expanded_faces_width, expanded_faces_height = expand_facial_area(
img=img, facial_area=(x, y, w, h)
)
aligned_img, angle = align_img_wrt_eyes(
img=processed_face, left_eye=left_eye, right_eye=right_eye
img=expanded_face, left_eye=left_eye, right_eye=right_eye
)
rotated_x1, rotated_y1, rotated_x2, rotated_y2 = project_facial_area(
facial_area=(new_x, new_y, new_x + w, new_y + h),
facial_area=(
expanded_faces_width,
expanded_faces_height,
expanded_faces_width + w,
expanded_faces_height + h,
),
angle=angle,
size=(processed_face.shape[0], processed_face.shape[1])
size=(expanded_face.shape[0], expanded_face.shape[1]),
)
detected_face = aligned_img[
int(rotated_y1) : int(rotated_y2), int(rotated_x1) : int(rotated_x2)
@ -348,9 +358,8 @@ def expand_and_align_face(
)
def alignment_preprocess(
img: np.ndarray,
facial_area: Tuple[int, int, int, int]
def expand_facial_area(
img: np.ndarray, facial_area: Tuple[int, int, int, int]
) -> Tuple[np.ndarray, int, int]:
"""
Expand the facial region to ensure alignment does not shift the face outside the image.
@ -359,20 +368,18 @@ def alignment_preprocess(
and adds black pixels if necessary.
Args:
----
- img (np.ndarray): pre-loaded image with detected face
- facial_area (tuple of int): Representing the (x, y, w, h) of the facial area.
- img (np.ndarray): pre-loaded image with detected face
- facial_area (tuple of int): Representing the (x, y, w, h) of the facial area.
Returns:
----
- expanded_face (np.ndarray): expanded facial image
- new_x (int): adjusted x-coordinates relative to the expanded region
- new_y (int): adjusted y-coordinates relative to the expanded region
- expanded_face (np.ndarray): expanded facial image
- expanded_faces_width (int): adjusted x-coordinates relative to the expanded region
- expanded_faces_height (int): adjusted y-coordinates relative to the expanded region
"""
x, y, w, h = facial_area
width_border = int(0.5 * w)
height_border = int(0.5 * h)
new_x, new_y = width_border, height_border
expanded_faces_width, expanded_faces_height = width_border, height_border
# calculate expanded coordinates
x1, y1 = x - width_border, y - height_border
@ -380,7 +387,7 @@ def alignment_preprocess(
# most of the time, the expanded region fits inside the image
if x1 >= 0 and y1 >= 0 and x2 <= img.shape[1] and y2 <= img.shape[0]:
return img[y1:y2, x1:x2], new_x, new_y
return img[y1:y2, x1:x2], expanded_faces_width, expanded_faces_height
# but sometimes, we need to add black pixels
# ensure the coordinates are within bounds
@ -390,19 +397,17 @@ def alignment_preprocess(
# create a black image
expanded_image = np.zeros(
(h + 2*height_border, w + 2*width_border, img.shape[2]),
dtype=img.dtype
(h + 2 * height_border, w + 2 * width_border, img.shape[2]), dtype=img.dtype
)
# map the cropped region
start_x = max(0, width_border - x)
start_y = max(0, height_border - y)
expanded_image[
start_y:start_y + cropped_region.shape[0],
start_x:start_x + cropped_region.shape[1]
start_y : start_y + cropped_region.shape[0], start_x : start_x + cropped_region.shape[1]
] = cropped_region
return expanded_image, new_x, new_y
return expanded_image, expanded_faces_width, expanded_faces_height
def align_img_wrt_eyes(