Skip to content

Commit 87f4893

Browse files
authored
Merge pull request #1047 from AndreaLanfranchi/al20220227-rotate-facial-area
Simplify and strengthen rotate_facial_area
2 parents 7544b46 + 9005329 commit 87f4893

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

deepface/detectors/DetectorWrapper.py

+33-18
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,33 @@ def detect_faces(
103103
right_eye = facial_area.right_eye
104104
confidence = facial_area.confidence
105105

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)
111111

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)
114116

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)]
120119

121120
# align original image, then find projection of detected face area after alignment
122121
if align is True: # and left_eye is not None and right_eye is not None:
123122
aligned_img, angle = detection.align_face(
124123
img=img, left_eye=left_eye, right_eye=right_eye
125124
)
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])
128129
)
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)]
130133

131134
result = DetectedFace(
132135
img=detected_face,
@@ -140,7 +143,9 @@ def detect_faces(
140143

141144

142145
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]
144149
) -> Tuple[int, int, int, int]:
145150
"""
146151
Rotate the facial area around its center.
@@ -149,14 +154,24 @@ def rotate_facial_area(
149154
Args:
150155
facial_area (tuple of int): Representing the (x1, y1, x2, y2) of the facial area.
151156
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).
154159
size (tuple of int): Tuple representing the size of the image (width, height).
155160
156161
Returns:
157162
rotated_coordinates (tuple of int): Representing the new coordinates
158163
(x1, y1, x2, y2) or (x1, y1, x1+w1, y1+h1) of the rotated facial area.
159164
"""
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+
160175
# Angle in radians
161176
angle = angle * np.pi / 180
162177

0 commit comments

Comments
 (0)