-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
97 lines (72 loc) · 2.77 KB
/
preprocessing.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
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
import dlib
import numpy as np
import skimage.transform as tr
from enum import Enum
class FaceDetectorException (Exception):
pass
class FaceDetector:
def __init__(self):
self.detector = dlib.get_frontal_face_detector()
def detect_faces(self,
image, *,
upscale_factor=1,
greater_than=None,
get_top=None):
try:
face_rects = list(self.detector(image, upscale_factor))
except Exception as e:
raise FaceDetectorException(e.args)
if greater_than is not None:
face_rects = list(filter(lambda r:
r.height() > greater_than and r.width() > greater_than,
face_rects))
face_rects.sort(key=lambda r: r.width() * r.height(), reverse=True)
if get_top is not None:
face_rects = face_rects[:get_top]
return face_rects
class FaceAlignMask(Enum):
INNER_EYES_AND_BOTTOM_LIP = [39, 42, 57]
OUTER_EYES_AND_NOSE = [36, 45, 33]
class FaceAligner:
def __init__(self,
dlib_predictor_path,
face_template_path):
self.predictor = dlib.shape_predictor(dlib_predictor_path)
self.face_template = np.load(face_template_path)
def get_landmarks(self,
image,
face_rect):
points = self.predictor(image, face_rect)
return np.array(list(map(lambda p: [p.x, p.y], points.parts())))
def align_face(self,
image,
face_rect, *,
dim=96,
border=0,
mask=FaceAlignMask.INNER_EYES_AND_BOTTOM_LIP):
mask = np.array(mask.value)
landmarks = self.get_landmarks(image, face_rect)
proper_landmarks = border + dim * self.face_template[mask]
A = np.hstack([landmarks[mask], np.ones((3, 1))]).astype(np.float64)
B = np.hstack([proper_landmarks, np.ones((3, 1))]).astype(np.float64)
T = np.linalg.solve(A, B).T
wrapped = tr.warp(image,
tr.AffineTransform(T).inverse,
output_shape=(dim + 2 * border, dim + 2 * border),
order=3,
mode='constant',
cval=0,
clip=True,
preserve_range=True)
return wrapped
def align_faces(self,
image,
face_rects,
*args,
**kwargs):
result = []
for rect in face_rects:
result.append(self.align_face(image, rect, *args, **kwargs))
return result
def clip_to_range(img):
return img / 255.0