Skip to content

Commit f6a226e

Browse files
committed
validate projected coordinates are in the boundaries
1 parent 5603d5f commit f6a226e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

deepface/detectors/DetectorWrapper.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,30 @@ def rotate_facial_area(
175175
# Angle in radians
176176
angle = angle * np.pi / 180
177177

178+
height, weight = size
179+
178180
# Translate the facial area to the center of the image
179-
x = (facial_area[0] + facial_area[2]) / 2 - size[1] / 2
180-
y = (facial_area[1] + facial_area[3]) / 2 - size[0] / 2
181+
x = (facial_area[0] + facial_area[2]) / 2 - weight / 2
182+
y = (facial_area[1] + facial_area[3]) / 2 - height / 2
181183

182184
# Rotate the facial area
183185
x_new = x * np.cos(angle) + y * direction * np.sin(angle)
184186
y_new = -x * direction * np.sin(angle) + y * np.cos(angle)
185187

186188
# Translate the facial area back to the original position
187-
x_new = x_new + size[1] / 2
188-
y_new = y_new + size[0] / 2
189+
x_new = x_new + weight / 2
190+
y_new = y_new + height / 2
189191

190-
# Calculate the new facial area
192+
# Calculate projected coordinates after alignment
191193
x1 = x_new - (facial_area[2] - facial_area[0]) / 2
192194
y1 = y_new - (facial_area[3] - facial_area[1]) / 2
193195
x2 = x_new + (facial_area[2] - facial_area[0]) / 2
194196
y2 = y_new + (facial_area[3] - facial_area[1]) / 2
195197

196-
return (int(x1), int(y1), int(x2), int(y2))
198+
# validate projected coordinates are in image's boundaries
199+
x1 = max(int(x1), 0)
200+
y1 = max(int(y1), 0)
201+
x2 = min(int(x2), weight)
202+
y2 = min(int(y2), height)
203+
204+
return (x1, y1, x2, y2)

0 commit comments

Comments
 (0)