-
Notifications
You must be signed in to change notification settings - Fork 3
/
face.py
72 lines (60 loc) · 3.03 KB
/
face.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Face:
"""
Base class for detected face objects
Attributes:
bbox (np.array): np array with shape (4,) and in [top left x, top left y, bottom right x, bottom right y] format (Essential)
score (float): confidence score of detected face
kps (np.array): np array with shape (5, 2) for 5 keypoints (using for face alignment)
embedding (np.array): np array with shape (512,) for reperesentation vector
identity (string): name of identified person
cropped_face (np.aray): np array with shape (h, w, 3), cropped face bgr image
"""
def __init__(self, bbox, score, kps, cropped_face=None, embedding=None, identity="unknown"):
"""
The constructor for Face class.
Parameters:
bbox (np.array): np array with shape (4,) and in [top left x, top left y, bottom right x, bottom right y] format (Essential)
score (float): confidence score of detected face
kps (np.array): np array with shape (5, 2) for 5 keypoints (using for face alignment)
cropped_face (np.aray): np array with shape (h, w, 3), cropped face bgr image
embedding (np.array): np array with shape (512,) for reperesentation vector
identity (string): name of identified person
similarity (float): similaity to the source face, between -1 and +1
"""
self.bbox = bbox
self.score = score
self.kps = kps
self.cropped_face = cropped_face
self.embedding = embedding
self.identity = identity
self.similarity = -1.
def to_tlwh(self):
"""
Helper function for converting [top left x, top left y, bottom right x, bottom right y] (default) format
to [top left x, top left y, width, height] format
Returns:
tlwh_bbox (np.array): bounding box in [top left x, top left y, width, height] format
"""
tlwh_bbox = self.bbox.copy()
tlwh_bbox[2:] = tlwh_bbox[2:] - tlwh_bbox[:2]
return tlwh_bbox
def to_xywh(self):
"""
Helper function for converting [top left x, top left y, bottom right x, bottom right y] (default) format
to [center x, center y, width, height] format
Returns:
xywh_bbox (np.array): bounding box in [center x, center y, width, height] format
"""
xywh_bbox = self.to_tlwh()
xywh_bbox[:2] = xywh_bbox[:2] + xywh_bbox[2:]/2
return xywh_bbox
def to_xyah(self):
"""
Helper function for converting [top left x, top left y, bottom right x, bottom right y] (default) format
to [center x, center y, aspec ratio, height] format
Returns:
xyah_bbox (np.array): bounding box in [center x, center y, aspect_ratio, height] format, where the aspect_ratio is width / height
"""
xyah_bbox = self.to_xywh()
xyah_bbox[2] = xyah_bbox[2] / xyah_bbox[3]
return xyah_bbox