From fd1039e5f1124b777b4b1a7040d40c093edf1a19 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Tue, 16 Apr 2024 20:11:17 +0100 Subject: [PATCH] confirm projected coordinates are in boundaries --- retinaface/RetinaFace.py | 2 +- retinaface/commons/postprocess.py | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/retinaface/RetinaFace.py b/retinaface/RetinaFace.py index cc82831..77ff2df 100644 --- a/retinaface/RetinaFace.py +++ b/retinaface/RetinaFace.py @@ -283,7 +283,7 @@ def extract_faces( # find new facial area coordinates after alignment rotated_x1, rotated_y1, rotated_x2, rotated_y2 = postprocess.rotate_facial_area( - (x, y, x + w, y + h), rotate_angle, rotate_direction, img.shape + (x, y, x + w, y + h), rotate_angle, rotate_direction, (img.shape[0], img.shape[1]) ) facial_img = aligned_img[ int(rotated_y1) : int(rotated_y2), int(rotated_x1) : int(rotated_x2) diff --git a/retinaface/commons/postprocess.py b/retinaface/commons/postprocess.py index 8cbde32..d73f931 100644 --- a/retinaface/commons/postprocess.py +++ b/retinaface/commons/postprocess.py @@ -114,25 +114,33 @@ def rotate_facial_area( # Angle in radians angle = angle * np.pi / 180 + height, weight = size + # Translate the facial area to the center of the image - x = (facial_area[0] + facial_area[2]) / 2 - size[1] / 2 - y = (facial_area[1] + facial_area[3]) / 2 - size[0] / 2 + x = (facial_area[0] + facial_area[2]) / 2 - weight / 2 + y = (facial_area[1] + facial_area[3]) / 2 - height / 2 # Rotate the facial area x_new = x * np.cos(angle) + y * direction * np.sin(angle) y_new = -x * direction * np.sin(angle) + y * np.cos(angle) # Translate the facial area back to the original position - x_new = x_new + size[1] / 2 - y_new = y_new + size[0] / 2 + x_new = x_new + weight / 2 + y_new = y_new + height / 2 - # Calculate the new facial area + # Calculate projected coordinates after alignment x1 = x_new - (facial_area[2] - facial_area[0]) / 2 y1 = y_new - (facial_area[3] - facial_area[1]) / 2 x2 = x_new + (facial_area[2] - facial_area[0]) / 2 y2 = y_new + (facial_area[3] - facial_area[1]) / 2 - return (int(x1), int(y1), int(x2), int(y2)) + # validate projected coordinates are in image's boundaries + x1 = max(int(x1), 0) + y1 = max(int(y1), 0) + x2 = min(int(x2), weight) + y2 = min(int(y2), height) + + return (x1, y1, x2, y2) def bbox_pred(boxes, box_deltas):