-
Notifications
You must be signed in to change notification settings - Fork 1
/
alignment.py
395 lines (324 loc) · 14.5 KB
/
alignment.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python2
#
# Copyright 2015-2016 Carnegie Mellon University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import cv2
import os
import random
import shutil
import util
import numpy as np
import dlib
fileDir = os.path.dirname(os.path.realpath(__file__))
print(fileDir)
dlibModelDir = os.path.join(fileDir, 'dlib')
print(dlibModelDir)
TEMPLATE = np.float32([
(0.0792396913815, 0.339223741112), (0.0829219487236, 0.456955367943),
(0.0967927109165, 0.575648016728), (0.122141515615, 0.691921601066),
(0.168687863544, 0.800341263616), (0.239789390707, 0.895732504778),
(0.325662452515, 0.977068762493), (0.422318282013, 1.04329000149),
(0.531777802068, 1.06080371126), (0.641296298053, 1.03981924107),
(0.738105872266, 0.972268833998), (0.824444363295, 0.889624082279),
(0.894792677532, 0.792494155836), (0.939395486253, 0.681546643421),
(0.96111933829, 0.562238253072), (0.970579841181, 0.441758925744),
(0.971193274221, 0.322118743967), (0.163846223133, 0.249151738053),
(0.21780354657, 0.204255863861), (0.291299351124, 0.192367318323),
(0.367460241458, 0.203582210627), (0.4392945113, 0.233135599851),
(0.586445962425, 0.228141644834), (0.660152671635, 0.195923841854),
(0.737466449096, 0.182360984545), (0.813236546239, 0.192828009114),
(0.8707571886, 0.235293377042), (0.51534533827, 0.31863546193),
(0.516221448289, 0.396200446263), (0.517118861835, 0.473797687758),
(0.51816430343, 0.553157797772), (0.433701156035, 0.604054457668),
(0.475501237769, 0.62076344024), (0.520712933176, 0.634268222208),
(0.565874114041, 0.618796581487), (0.607054002672, 0.60157671656),
(0.252418718401, 0.331052263829), (0.298663015648, 0.302646354002),
(0.355749724218, 0.303020650651), (0.403718978315, 0.33867711083),
(0.352507175597, 0.349987615384), (0.296791759886, 0.350478978225),
(0.631326076346, 0.334136672344), (0.679073381078, 0.29645404267),
(0.73597236153, 0.294721285802), (0.782865376271, 0.321305281656),
(0.740312274764, 0.341849376713), (0.68499850091, 0.343734332172),
(0.353167761422, 0.746189164237), (0.414587777921, 0.719053835073),
(0.477677654595, 0.706835892494), (0.522732900812, 0.717092275768),
(0.569832064287, 0.705414478982), (0.635195811927, 0.71565572516),
(0.69951672331, 0.739419187253), (0.639447159575, 0.805236879972),
(0.576410514055, 0.835436670169), (0.525398405766, 0.841706377792),
(0.47641545769, 0.837505914975), (0.41379548902, 0.810045601727),
(0.380084785646, 0.749979603086), (0.477955996282, 0.74513234612),
(0.523389793327, 0.748924302636), (0.571057789237, 0.74332894691),
(0.672409137852, 0.744177032192), (0.572539621444, 0.776609286626),
(0.5240106503, 0.783370783245), (0.477561227414, 0.778476346951)])
TPL_MIN, TPL_MAX = np.min(TEMPLATE, axis=0), np.max(TEMPLATE, axis=0)
MINMAX_TEMPLATE = (TEMPLATE - TPL_MIN) / (TPL_MAX - TPL_MIN)
class AlignDlib:
"""
Use `dlib's landmark estimation <http://blog.dlib.net/2014/08/real-time-face-pose-estimation.html>`_ to align faces.
The alignment preprocess faces for input into a neural network.
Faces are resized to the same size (such as 96x96) and transformed
to make landmarks (such as the eyes and nose) appear at the same
location on every image.
Normalized landmarks:
.. image:: ../images/dlib-landmark-mean.png
"""
#: Landmark indices.
INNER_EYES_AND_BOTTOM_LIP = [39, 42, 57]
OUTER_EYES_AND_NOSE = [36, 45, 33]
OUTER_EYES_AND_JAW = [8, 36, 45]
INNER_EYES_AND_NOSE = [39, 42, 33]
def __init__(self, facePredictor):
"""
Instantiate an 'AlignDlib' object.
:param facePredictor: The path to dlib's
:type facePredictor: str
"""
assert facePredictor is not None
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor(facePredictor)
def getAllFaceBoundingBoxes(self, rgbImg):
"""
Find all face bounding boxes in an image.
:param rgbImg: RGB image to process. Shape: (height, width, 3)
:type rgbImg: numpy.ndarray
:return: All face bounding boxes in an image.
:rtype: dlib.rectangles
"""
assert rgbImg is not None
try:
return self.detector(rgbImg, 1)
except Exception as e:
print("Warning: {}".format(e))
# In rare cases, exceptions are thrown.
return []
def getLargestFaceBoundingBox(self, rgbImg, skipMulti=False):
"""
Find the largest face bounding box in an image.
:param rgbImg: RGB image to process. Shape: (height, width, 3)
:type rgbImg: numpy.ndarray
:param skipMulti: Skip image if more than one face detected.
:type skipMulti: bool
:return: The largest face bounding box in an image, or None.
:rtype: dlib.rectangle
"""
assert rgbImg is not None
faces = self.getAllFaceBoundingBoxes(rgbImg)
if (not skipMulti and len(faces) > 0) or len(faces) == 1:
return max(faces, key=lambda rect: rect.width() * rect.height())
else:
return None
def findLandmarks(self, rgbImg, bb):
"""
Find the landmarks of a face.
:param rgbImg: RGB image to process. Shape: (height, width, 3)
:type rgbImg: numpy.ndarray
:param bb: Bounding box around the face to find landmarks for.
:type bb: dlib.rectangle
:return: Detected landmark locations.
:rtype: list of (x,y) tuples
"""
assert rgbImg is not None
assert bb is not None
points = self.predictor(rgbImg, bb)
return list(map(lambda p: (p.x, p.y), points.parts()))
def align(self, imgDim, rgbImg, bb=None,
landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP,
skipMulti=False):
r"""align(imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP)
Transform and align a face in an image.
:param imgDim: The edge length in pixels of the square the image is resized to.
:type imgDim: int
:param rgbImg: RGB image to process. Shape: (height, width, 3)
:type rgbImg: numpy.ndarray
:param bb: Bounding box around the face to align. \
Defaults to the largest face.
:type bb: dlib.rectangle
:param landmarks: Detected landmark locations. \
Landmarks found on `bb` if not provided.
:type landmarks: list of (x,y) tuples
:param landmarkIndices: The indices to transform to.
:type landmarkIndices: list of ints
:param skipMulti: Skip image if more than one face detected.
:type skipMulti: bool
:return: The aligned RGB image. Shape: (imgDim, imgDim, 3)
:rtype: numpy.ndarray
"""
assert imgDim is not None
assert rgbImg is not None
assert landmarkIndices is not None
if bb is None:
bb = self.getLargestFaceBoundingBox(rgbImg, skipMulti)
if bb is None:
return
if landmarks is None:
landmarks = self.findLandmarks(rgbImg, bb)
npLandmarks = np.float32(landmarks)
npLandmarkIndices = np.array(landmarkIndices)
H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],
imgDim * MINMAX_TEMPLATE[npLandmarkIndices])
thumbnail = cv2.warpAffine(rgbImg, H, (imgDim, imgDim))
return thumbnail
class Image:
"""Object containing image metadata."""
def __init__(self, cls, name, path):
"""
Instantiate an 'Image' object.
:param cls: The image's class; the name of the person.
:type cls: str
:param name: The image's name.
:type name: str
:param path: Path to the image on disk.
:type path: str
"""
assert cls is not None
assert name is not None
assert path is not None
self.cls = cls
self.name = name
self.path = path
def getBGR(self):
"""
Load the image from disk in BGR format.
:return: BGR image. Shape: (height, width, 3)
:rtype: numpy.ndarray
"""
try:
bgr = cv2.imread(self.path)
except:
bgr = None
return bgr
def getRGB(self):
"""
Load the image from disk in RGB format.
:return: RGB image. Shape: (height, width, 3)
:rtype: numpy.ndarray
"""
bgr = self.getBGR()
if bgr is not None:
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
else:
rgb = None
return rgb
def __repr__(self):
"""String representation for printing."""
return "({}, {})".format(self.cls, self.name)
def iterImgs(directory):
u"""
Iterate through the images in a directory.
The images should be organized in subdirectories
named by the image's class (who the person is)::
$ tree directory
person-1
...
person-m
:param directory: The directory to iterate through.
:type directory: str
:return: An iterator over Image objects.
"""
assert directory is not None
exts = [".jpg", ".jpeg", ".png"]
for subdir, dirs, files in os.walk(directory):
for path in files:
(imageClass, fName) = (os.path.basename(subdir), path)
(imageName, ext) = os.path.splitext(fName)
if ext.lower() in exts:
yield Image(imageClass, imageName, os.path.join(subdir, fName))
def write(vals, fName):
if os.path.isfile(fName):
print("{} exists. Backing up.".format(fName))
os.rename(fName, "{}.bak".format(fName))
with open(fName, 'w') as f:
for p in vals:
f.write(",".join(str(x) for x in p))
f.write("\n")
def alignMain(args):
util.mkdirP(args.outputDir)
imgs = list(iterImgs(args.inputDir))
# Shuffle so multiple versions can be run at once.
random.shuffle(imgs)
landmarkMap = {
'outerEyesAndNose': AlignDlib.OUTER_EYES_AND_NOSE,
'innerEyesAndBottomLip': AlignDlib.INNER_EYES_AND_BOTTOM_LIP,
'outerEyesAndJaw': AlignDlib.OUTER_EYES_AND_JAW,
'innerEyesAndNose': AlignDlib.INNER_EYES_AND_NOSE
}
if args.landmarks not in landmarkMap:
raise Exception("Landmarks unrecognized: {}".format(args.landmarks))
landmarkIndices = landmarkMap[args.landmarks]
align = AlignDlib(args.dlibFacePredictor)
nFallbacks = 0
for imgObject in imgs:
print("=== {} ===".format(imgObject.path))
outDir = os.path.join(args.outputDir, imgObject.cls)
util.mkdirP(outDir)
outputPrefix = os.path.join(outDir, imgObject.name)
imgName = outputPrefix + ".png"
if os.path.isfile(imgName):
if args.verbose:
print(" + Already found, skipping.")
else:
rgb = imgObject.getRGB()
if rgb is None:
if args.verbose:
print(" + Unable to load.")
outRgb = None
else:
outRgb = align.align(args.size, rgb,
landmarkIndices=landmarkIndices,
skipMulti=args.skipMulti)
if outRgb is None and args.verbose:
print(" + Unable to align.")
if args.fallbackLfw and outRgb is None:
nFallbacks += 1
deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw,
imgObject.cls),
imgObject.name)
shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(args.outputDir,
imgObject.cls),
imgObject.name))
if outRgb is not None:
if args.verbose:
print(" + Writing aligned file to disk.")
outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
cv2.imwrite(imgName, outBgr)
if args.fallbackLfw:
print('nFallbacks:', nFallbacks)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('inputDir', type=str, help="Input image directory.")
parser.add_argument('--dlibFacePredictor', type=str, help="Path to dlib's face predictor.",
default=os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))
subparsers = parser.add_subparsers(dest='mode', help="Mode")
computeMeanParser = subparsers.add_parser(
'computeMean', help='Compute the image mean of a directory of images.')
computeMeanParser.add_argument('--numImages', type=int, help="The number of images. '0' for all images.",
default=0) # <= 0 ===> all imgs
alignmentParser = subparsers.add_parser(
'align', help='Align a directory of images.')
alignmentParser.add_argument('landmarks', type=str,
choices=['outerEyesAndNose',
'innerEyesAndBottomLip',
'innerEyesAndNose',
'outerEyesAndJaw'],
help='The landmarks to align to.')
alignmentParser.add_argument(
'outputDir', type=str, help="Output directory of aligned images.")
alignmentParser.add_argument('--size', type=int, help="Default image size.",
default=96)
alignmentParser.add_argument('--fallbackLfw', type=str,
help="If alignment doesn't work, fallback to copying the deep funneled version from this directory..")
alignmentParser.add_argument(
'--skipMulti', action='store_true', help="Skip images with more than one face.")
alignmentParser.add_argument('--verbose', action='store_true')
args = parser.parse_args()
alignMain(args)