Skip to content

Commit fd1039e

Browse files
committed
confirm projected coordinates are in boundaries
1 parent a4fd2b8 commit fd1039e

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

retinaface/RetinaFace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def extract_faces(
283283

284284
# find new facial area coordinates after alignment
285285
rotated_x1, rotated_y1, rotated_x2, rotated_y2 = postprocess.rotate_facial_area(
286-
(x, y, x + w, y + h), rotate_angle, rotate_direction, img.shape
286+
(x, y, x + w, y + h), rotate_angle, rotate_direction, (img.shape[0], img.shape[1])
287287
)
288288
facial_img = aligned_img[
289289
int(rotated_y1) : int(rotated_y2), int(rotated_x1) : int(rotated_x2)

retinaface/commons/postprocess.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,33 @@ def rotate_facial_area(
114114
# Angle in radians
115115
angle = angle * np.pi / 180
116116

117+
height, weight = size
118+
117119
# Translate the facial area to the center of the image
118-
x = (facial_area[0] + facial_area[2]) / 2 - size[1] / 2
119-
y = (facial_area[1] + facial_area[3]) / 2 - size[0] / 2
120+
x = (facial_area[0] + facial_area[2]) / 2 - weight / 2
121+
y = (facial_area[1] + facial_area[3]) / 2 - height / 2
120122

121123
# Rotate the facial area
122124
x_new = x * np.cos(angle) + y * direction * np.sin(angle)
123125
y_new = -x * direction * np.sin(angle) + y * np.cos(angle)
124126

125127
# Translate the facial area back to the original position
126-
x_new = x_new + size[1] / 2
127-
y_new = y_new + size[0] / 2
128+
x_new = x_new + weight / 2
129+
y_new = y_new + height / 2
128130

129-
# Calculate the new facial area
131+
# Calculate projected coordinates after alignment
130132
x1 = x_new - (facial_area[2] - facial_area[0]) / 2
131133
y1 = y_new - (facial_area[3] - facial_area[1]) / 2
132134
x2 = x_new + (facial_area[2] - facial_area[0]) / 2
133135
y2 = y_new + (facial_area[3] - facial_area[1]) / 2
134136

135-
return (int(x1), int(y1), int(x2), int(y2))
137+
# validate projected coordinates are in image's boundaries
138+
x1 = max(int(x1), 0)
139+
y1 = max(int(y1), 0)
140+
x2 = min(int(x2), weight)
141+
y2 = min(int(y2), height)
142+
143+
return (x1, y1, x2, y2)
136144

137145

138146
def bbox_pred(boxes, box_deltas):

0 commit comments

Comments
 (0)