@@ -103,30 +103,33 @@ def detect_faces(
103
103
right_eye = facial_area .right_eye
104
104
confidence = facial_area .confidence
105
105
106
- # expand the facial area to be extracted and stay within img.shape limits
107
- x2 = max ( 0 , x - int (( w * expand_percentage ) / 100 )) # expand left
108
- y2 = max ( 0 , y - int (( h * expand_percentage ) / 100 )) # expand top
109
- w2 = min ( img . shape [ 1 ], w + int (( w * 2 * expand_percentage ) / 100 )) # expand right
110
- h2 = min ( img . shape [ 0 ], h + int (( h * 2 * expand_percentage ) / 100 )) # expand bottom
106
+ if expand_percentage > 0 :
107
+ # Expand the facial region height and width by the provided percentage
108
+ # ensuring that the expanded region stays within img.shape limits
109
+ expanded_w = w + int (w * expand_percentage / 100 )
110
+ expanded_h = h + int (h * expand_percentage / 100 )
111
111
112
- # extract detected face unaligned
113
- detected_face = img [int (y2 ) : int (y2 + h2 ), int (x2 ) : int (x2 + w2 )]
112
+ x = max (0 , x - int ((expanded_w - w ) / 2 ))
113
+ y = max (0 , y - int ((expanded_h - h ) / 2 ))
114
+ w = min (img .shape [1 ] - x , expanded_w )
115
+ h = min (img .shape [0 ] - y , expanded_h )
114
116
115
- # aligning detected face causes a lot of black pixels
116
- # if align is True:
117
- # detected_face, _ = detection.align_face(
118
- # img=detected_face, left_eye=left_eye, right_eye=right_eye
119
- # )
117
+ # extract detected face unaligned
118
+ detected_face = img [int (y ) : int (y + h ), int (x ) : int (x + w )]
120
119
121
120
# align original image, then find projection of detected face area after alignment
122
121
if align is True : # and left_eye is not None and right_eye is not None:
123
122
aligned_img , angle = detection .align_face (
124
123
img = img , left_eye = left_eye , right_eye = right_eye
125
124
)
126
- x1_new , y1_new , x2_new , y2_new = rotate_facial_area (
127
- facial_area = (x2 , y2 , x2 + w2 , y2 + h2 ), angle = angle , direction = 1 , size = img .shape
125
+ rotated_x1 , rotated_y1 , rotated_x2 , rotated_y2 = rotate_facial_area (
126
+ facial_area = (x , y , x + w , y + h ),
127
+ angle = angle ,
128
+ size = (img .shape [0 ], img .shape [1 ])
128
129
)
129
- detected_face = aligned_img [int (y1_new ) : int (y2_new ), int (x1_new ) : int (x2_new )]
130
+ detected_face = aligned_img [
131
+ int (rotated_y1 ) : int (rotated_y2 ),
132
+ int (rotated_x1 ) : int (rotated_x2 )]
130
133
131
134
result = DetectedFace (
132
135
img = detected_face ,
@@ -140,7 +143,9 @@ def detect_faces(
140
143
141
144
142
145
def rotate_facial_area (
143
- facial_area : Tuple [int , int , int , int ], angle : float , direction : int , size : Tuple [int , int ]
146
+ facial_area : Tuple [int , int , int , int ],
147
+ angle : float ,
148
+ size : Tuple [int , int ]
144
149
) -> Tuple [int , int , int , int ]:
145
150
"""
146
151
Rotate the facial area around its center.
@@ -149,14 +154,24 @@ def rotate_facial_area(
149
154
Args:
150
155
facial_area (tuple of int): Representing the (x1, y1, x2, y2) of the facial area.
151
156
x2 is equal to x1 + w1, and y2 is equal to y1 + h1
152
- angle (float): Angle of rotation in degrees.
153
- direction (int): Direction of rotation (-1 for clockwise, 1 for counterclockwise ).
157
+ angle (float): Angle of rotation in degrees. Its sign determines the direction of rotation.
158
+ Note that angles > 360 degrees are normalized to the range [0, 360 ).
154
159
size (tuple of int): Tuple representing the size of the image (width, height).
155
160
156
161
Returns:
157
162
rotated_coordinates (tuple of int): Representing the new coordinates
158
163
(x1, y1, x2, y2) or (x1, y1, x1+w1, y1+h1) of the rotated facial area.
159
164
"""
165
+
166
+ # Normalize the witdh of the angle so we don't have to
167
+ # worry about rotations greater than 360 degrees.
168
+ # We workaround the quirky behavior of the modulo operator
169
+ # for negative angle values.
170
+ direction = 1 if angle >= 0 else - 1
171
+ angle = abs (angle ) % 360
172
+ if angle == 0 :
173
+ return facial_area
174
+
160
175
# Angle in radians
161
176
angle = angle * np .pi / 180
162
177
0 commit comments