Skip to content

Commit 27ce418

Browse files
committed
correction in facial areas and expanding logic
1 parent 3a38aab commit 27ce418

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

retinaface/RetinaFace.py

+18-29
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,20 @@ def extract_faces(
245245

246246
x = facial_area[0]
247247
y = facial_area[1]
248-
w = facial_area[2]
249-
h = facial_area[3]
248+
w = facial_area[2] - x
249+
h = facial_area[3] - y
250250

251-
# expand the facial area to be extracted and stay within img.shape limits
252-
x1 = max(0, x - int((w * expand_face_area) / 100)) # expand left
253-
y1 = max(0, y - int((h * expand_face_area) / 100)) # expand top
254-
x2 = min(img.shape[1], w + int((w * expand_face_area) / 100)) # expand right
255-
y2 = min(img.shape[0], h + int((h * expand_face_area) / 100)) # expand bottom
251+
if expand_face_area > 0:
252+
expanded_w = w + int(w * expand_face_area / 100)
253+
expanded_h = h + int(h * expand_face_area / 100)
256254

257-
facial_img = img[y1:y2, x1:x2]
255+
# overwrite facial area
256+
x = max(0, x - int((expanded_w - w) / 2))
257+
y = max(0, y - int((expanded_h - h) / 2))
258+
w = min(img.shape[1] - x, expanded_w)
259+
h = min(img.shape[0] - y, expanded_h)
260+
261+
facial_img = img[y : y + h, x : x + w]
258262

259263
if align is True:
260264
landmarks = identity["landmarks"]
@@ -265,32 +269,17 @@ def extract_faces(
265269
# mouth_left = landmarks["mouth_left"]
266270

267271
# notice that left eye of one is seen on the right from your perspective
268-
facial_img, rotate_angle, rotate_direction = postprocess.alignment_procedure(
272+
aligned_img, rotate_angle, rotate_direction = postprocess.alignment_procedure(
269273
img=img, left_eye=right_eye, right_eye=left_eye, nose=nose
270274
)
271275

272276
# find new facial area coordinates after alignment
273-
projected_facial_area = postprocess.rotate_facial_area(
274-
facial_area, rotate_angle, rotate_direction, img.shape
275-
)
276-
# Expand the facial area to be extracted and stay within img.shape limits
277-
x1 = max(
278-
0,
279-
projected_facial_area[0] - int((projected_facial_area[2] * expand_face_area) / 100),
280-
)
281-
y1 = max(
282-
0,
283-
projected_facial_area[1] - int((projected_facial_area[3] * expand_face_area) / 100),
284-
)
285-
x2 = min(
286-
img.shape[1],
287-
projected_facial_area[2] + int((projected_facial_area[2] * expand_face_area) / 100),
288-
)
289-
y2 = min(
290-
img.shape[0],
291-
projected_facial_area[3] + int((projected_facial_area[3] * expand_face_area) / 100),
277+
rotated_x1, rotated_y1, rotated_x2, rotated_y2 = postprocess.rotate_facial_area(
278+
(x, y, x + w, y + h), rotate_angle, rotate_direction, img.shape
292279
)
293-
facial_img = facial_img[y1:y2, x1:x2]
280+
facial_img = aligned_img[
281+
int(rotated_y1) : int(rotated_y2), int(rotated_x1) : int(rotated_x2)
282+
]
294283

295284
resp.append(facial_img[:, :, ::-1])
296285

tests/test_actions.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_analyze_crowded_photo():
5252
plt.imshow(img[:, :, ::-1])
5353
plt.axis("off")
5454
plt.show()
55-
cv2.imwrite("outputs/" + img_path.split("/")[1], img)
55+
# cv2.imwrite("outputs/" + img_path.split("/")[1], img)
5656

5757
logger.info("✅ Crowded photo analysis test done")
5858

@@ -75,7 +75,7 @@ def test_alignment_for_clock_way():
7575

7676

7777
def do_alignment_checks(img: np.ndarray, expected_faces: int) -> None:
78-
faces = RetinaFace.extract_faces(img_path=img, align=True, expand_face_area=10)
78+
faces = RetinaFace.extract_faces(img_path=img, align=True, expand_face_area=25)
7979

8080
# it has one clear face
8181
assert len(faces) == expected_faces
@@ -93,3 +93,17 @@ def do_alignment_checks(img: np.ndarray, expected_faces: int) -> None:
9393

9494
# check eyes are on same horizantal
9595
assert abs(right_eye[1] - left_eye[1]) < 10
96+
97+
98+
def test_different_expanding_ratios():
99+
expand_ratios = [0, 25, 50]
100+
101+
for expand_ratio in expand_ratios:
102+
faces = RetinaFace.extract_faces(
103+
img_path="tests/dataset/img11.jpg", align=True, expand_face_area=expand_ratio
104+
)
105+
for face in faces:
106+
if do_plotting is True:
107+
plt.imshow(face)
108+
plt.axis("off")
109+
plt.show()

0 commit comments

Comments
 (0)