-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
utils.py
41 lines (33 loc) · 1.35 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""A module provides a bunch of helper functions."""
import numpy as np
def refine(boxes, max_width, max_height, shift=0.1):
"""Refine the face boxes to suit the face landmark detection's needs.
Args:
boxes: [[x1, y1, x2, y2], ...]
max_width: Value larger than this will be clipped.
max_height: Value larger than this will be clipped.
shift (float, optional): How much to shift the face box down. Defaults to 0.1.
Returns:
Refined results.
"""
refined = boxes.copy()
width = refined[:, 2] - refined[:, 0]
height = refined[:, 3] - refined[:, 1]
# Move the boxes in Y direction
shift = height * shift
refined[:, 1] += shift
refined[:, 3] += shift
center_x = (refined[:, 0] + refined[:, 2]) / 2
center_y = (refined[:, 1] + refined[:, 3]) / 2
# Make the boxes squares
square_sizes = np.maximum(width, height)
refined[:, 0] = center_x - square_sizes / 2
refined[:, 1] = center_y - square_sizes / 2
refined[:, 2] = center_x + square_sizes / 2
refined[:, 3] = center_y + square_sizes / 2
# Clip the boxes for safety
refined[:, 0] = np.clip(refined[:, 0], 0, max_width)
refined[:, 1] = np.clip(refined[:, 1], 0, max_height)
refined[:, 2] = np.clip(refined[:, 2], 0, max_width)
refined[:, 3] = np.clip(refined[:, 3], 0, max_height)
return refined