Skip to content

Commit cc28a20

Browse files
Add extract_box (#9)
2 parents e36a79c + 9afa11f commit cc28a20

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

facenet_pytorch/models/utils/detect_face.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,7 @@ def extract_face(
468468
Returns:
469469
Tensor representing the extracted face.
470470
"""
471-
margin = [margin * (box[2] - box[0]) / (image_size - margin), margin * (box[3] - box[1]) / (image_size - margin)]
472-
raw_image_size = get_size(img)
473-
box = (
474-
int(max(box[0] - margin[0] / 2, 0)),
475-
int(max(box[1] - margin[1] / 2, 0)),
476-
int(min(box[2] + margin[0] / 2, raw_image_size[0])),
477-
int(min(box[3] + margin[1] / 2, raw_image_size[1])),
478-
)
471+
box = extract_box(img, box, image_size, margin)
479472

480473
face = crop_resize(img, box, image_size)
481474

@@ -485,3 +478,32 @@ def extract_face(
485478
save_img(face, save_path)
486479

487480
return tv_functional.to_tensor(np.float32(face))
481+
482+
def extract_box(img: np.ndarray | torch.Tensor, box: np.ndarray, image_size:int = 160, margin: int = 0) -> np.ndarray:
483+
"""Extract box + margin from PIL Image given bounding box.
484+
485+
Arguments:
486+
img {PIL.Image} -- A PIL Image.
487+
box {numpy.ndarray} -- Four-element bounding box.
488+
image_size {int} -- Output image size in pixels. The image will be square.
489+
margin {int} -- Margin to add to bounding box, in terms of pixels in the final image.
490+
Note that the application of the margin differs slightly from the davidsandberg/facenet
491+
repo, which applies the margin to the original image before resizing, making the margin
492+
dependent on the original image size.
493+
494+
Returns:
495+
box-- np.ndarray[int] of ROI corners
496+
"""
497+
margin = [
498+
margin * (box[2] - box[0]) / (image_size - margin),
499+
margin * (box[3] - box[1]) / (image_size - margin),
500+
]
501+
raw_image_size = get_size(img)
502+
box = [
503+
int(max(box[0] - margin[0] / 2, 0)),
504+
int(max(box[1] - margin[1] / 2, 0)),
505+
int(min(box[2] + margin[0] / 2, raw_image_size[0])),
506+
int(min(box[3] + margin[1] / 2, raw_image_size[1])),
507+
]
508+
509+
return box

0 commit comments

Comments
 (0)