-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
147 lines (114 loc) · 3.28 KB
/
Copy pathmodel.py
File metadata and controls
147 lines (114 loc) · 3.28 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from collections import namedtuple
from dataclasses import dataclass
from enum import Enum
from functools import cached_property
import dlib
import numpy as np
class Gender(Enum):
FEMALE = 0
MALE = 1
UNKNOWN = 2
Point = namedtuple('Point', ['x', 'y'])
@dataclass
class Box:
top_left: Point
bottom_right: Point
@classmethod
def from_points(cls, top_left, bottom_right):
return cls.from_values(top_left.x, top_left.y, bottom_right.x, bottom_right.y)
@classmethod
def from_values(cls, top_left_x, top_left_y, bottom_right_x, bottom_right_y):
return cls(
top_left=Point(x=round(top_left_x), y=round(top_left_y)),
bottom_right=Point(x=round(bottom_right_x), y=round(bottom_right_y)),
)
@classmethod
def from_dlib_rect(cls, dlib_rect: dlib.rectangle):
return cls.from_values(
top_left_x=round(dlib_rect.left()),
top_left_y=round(dlib_rect.top()),
bottom_right_x=round(dlib_rect.right()),
bottom_right_y=round(dlib_rect.bottom()),
)
def to_dlib_rect(self) -> dlib.rectangle:
return dlib.rectangle(
left=self.top_left.x,
top=self.top_left.y,
right=self.bottom_right.x,
bottom=self.bottom_right.y,
)
@property
def top_left_x(self):
return self.top_left.x
@property
def top_left_y(self):
return self.top_left.y
@property
def bottom_right_x(self) -> int:
return self.bottom_right.x
@property
def bottom_right_y(self) -> int:
return self.bottom_right.y
@cached_property
def center(self) -> Point:
return Point(
x=(self.top_left.x + self.bottom_right.x) / 2,
y=(self.top_left.y + self.bottom_right.y) / 2,
)
def distance_to(self, other: 'Box') -> float:
return np.linalg.norm(
(self.center.x - other.center.x, self.center.y - other.center.y)
)
@dataclass(frozen=True)
class Color:
r: int = 0
b: int = 0
g: int = 0
def to_bgr(self):
return self.b, self.g, self.r
def to_rgb(self):
return self.r, self.g, self.b
@classmethod
def black(cls) -> 'Color':
return cls()
@classmethod
def white(cls) -> 'Color':
return cls(255, 255, 255)
@classmethod
def red(cls) -> 'Color':
return cls(r=255)
@classmethod
def green(cls) -> 'Color':
return cls(g=255)
@classmethod
def blue(cls) -> 'Color':
return cls(b=255)
@classmethod
def yellow(cls) -> 'Color':
return cls(r=255, g=255)
@classmethod
def orange(cls) -> 'Color':
return cls(r=255, g=69)
@dataclass
class Face:
box: Box
shape: dlib.full_object_detection # from SHAPE_MODEL
descriptor: np.ndarray # from RECOGNITION_MODEL
@dataclass
class GenderedFace(Face):
gender: Gender
gender_confidence: float
@classmethod
def from_face(
cls,
face: Face,
gender: Gender = Gender.UNKNOWN,
gender_confidence: float = 0.5,
):
return cls(
box=face.box,
shape=face.shape,
descriptor=face.descriptor,
gender=gender,
gender_confidence=gender_confidence,
)