@@ -175,22 +175,30 @@ def rotate_facial_area(
175
175
# Angle in radians
176
176
angle = angle * np .pi / 180
177
177
178
+ height , weight = size
179
+
178
180
# 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
181
183
182
184
# Rotate the facial area
183
185
x_new = x * np .cos (angle ) + y * direction * np .sin (angle )
184
186
y_new = - x * direction * np .sin (angle ) + y * np .cos (angle )
185
187
186
188
# 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
189
191
190
- # Calculate the new facial area
192
+ # Calculate projected coordinates after alignment
191
193
x1 = x_new - (facial_area [2 ] - facial_area [0 ]) / 2
192
194
y1 = y_new - (facial_area [3 ] - facial_area [1 ]) / 2
193
195
x2 = x_new + (facial_area [2 ] - facial_area [0 ]) / 2
194
196
y2 = y_new + (facial_area [3 ] - facial_area [1 ]) / 2
195
197
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