-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathencode_faces.py
262 lines (209 loc) · 9.38 KB
/
encode_faces.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
'''
Find faces in given images and encode into 128-D embeddings.
Usage:
$ python3 encode_faces.py --dataset dataset --encodings encodings.pickle
Originally part of the smart-zoneminder project:
See https://github.com/goruck/smart-zoneminder.
Copyright (c) 2019 Lindo St. Angel
'''
import numpy as np
import argparse
import pickle
import cv2
import face_recognition
from glob import glob
from os.path import sep
from edgetpu.detection.engine import DetectionEngine
print('quantifying faces...')
# Construct the argument parser and parse the arguments.
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--dataset', required=True,
help='path to input directory of faces + images')
ap.add_argument('-e', '--encodings', required=True,
help='name of serialized output file of facial encodings')
args = vars(ap.parse_args())
# Init OpenCV's deep learning face embedding model.
EMB_MODEL_PATH = './nn4.v2.t7'
embedder = cv2.dnn.readNetFromTorch(EMB_MODEL_PATH)
# Init OpenCV's dnn face detection and localization model.
FACE_DET_PROTOTXT_PATH = './deploy.prototxt'
FACE_DET_MODEL_PATH = './res10_300x300_ssd_iter_140000_fp16.caffemodel'
face_det = cv2.dnn.readNetFromCaffe(FACE_DET_PROTOTXT_PATH, FACE_DET_MODEL_PATH)
# Init tpu engine.
DET_MODEL_PATH = './mobilenet_ssd_v2_face_quant_postprocess_edgetpu.tflite'
face_engine = DetectionEngine(DET_MODEL_PATH)
# Grab the paths to the input images in our dataset.
imagePaths = glob(args['dataset'] + '/**/*.*', recursive=True)
# Initialize the list of known encodings and known names.
knownEncodings = []
knownNames = []
def resize_to_square(img, size, keep_aspect_ratio=False, interpolation=cv2.INTER_AREA):
# Resize image to square shape.
# If keep_aspect_ratio=True, then:
# If the original image is lanscape, add black pixels on the bottom-side only.
# If the original image is portrait, add black pixels on the right-side only.
(h, w) = img.shape[:2]
if h == w or keep_aspect_ratio == False:
return cv2.resize(img, (size, size), interpolation)
# Check if image is color.
chan = None if len(img.shape) < 3 else img.shape[2]
# Determine size of black mask.
mask_size = h if h > w else w
if chan is None:
mask = np.zeros((mask_size, mask_size), dtype=img.dtype)
mask[:h, :w] = img[:h, :w]
else:
mask = np.zeros((mask_size, mask_size, chan), dtype=img.dtype)
mask[:h, :w, :] = img[:h, :w, :]
return cv2.resize(mask, (size, size), interpolation)
def dlib_face_det(image):
# Detect and localize faces using dlib (via face_recognition).
# Assumes only one face is in image passed.
# Convert image from BGR (OpenCV ordering) to dlib ordering (RGB).
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Detect the (x, y)-coordinates of the bounding boxes
# corresponding to each face in the input image.
# NB: model='cnn' causes OOM.
boxes = face_recognition.face_locations(rgb,
number_of_times_to_upsample=2, model='hog')
if len(boxes) == 0:
print('*** no face found! ***')
return None
# Return bounding box coords in dlib format.
return boxes
def cv2_face_det(image):
# Detect and localize faces using OpenCV dnn.
# Assumes only one face is in image passed.
# Threshold for valid face detect.
CONFIDENCE_THRES = 0.9
# Construct an input blob for the image and resize and normalize it.
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300,300)), 1.0,
(300,300), (104.0, 177.0, 123.0))
# Pass the blob through the network and obtain the detections and
# predictions.
face_det.setInput(blob)
detections = face_det.forward()
if len(detections) > 0:
# We're making the assumption that each image has only ONE
# face, so find the bounding box with the largest probability.
pred_num = np.argmax(detections[0, 0, :, 2])
confidence = detections[0, 0, pred_num, 2]
print('detection confidence: {}'.format(confidence))
else:
print('*** no face found! ***')
return None
# Filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence.
if confidence > CONFIDENCE_THRES:
# Compute the (x, y)-coordinates of the bounding box for image.
(h, w) = image.shape[:2]
print('img h: {} img w: {}'.format(h, w))
box = detections[0, 0, pred_num, 3:7] * np.array([w, h, w, h])
(face_left, face_top, face_right, face_bottom) = box.astype('int')
#print('face_left: {} face_top: {} face_right: {} face_bottom: {}'
#.format(face_left, face_top, face_right, face_bottom))
# Return bounding box coords in dlib format.
# Sometimes the dnn returns bboxes larger than image, so check.
# If bbox too large just return bbox of whole image.
# TODO: figure out why this happens.
(h, w) = image.shape[:2]
if (face_right - face_left) > w or (face_bottom - face_top) > h:
print('*** bbox out of bounds! ***')
return [(0, w, h, 0)]
else:
return [(face_top, face_right, face_bottom, face_left)]
else:
print('*** no face found! ***')
return None
def tpu_face_det(image):
# Detect faces using TPU engine.
# Assumes only one face is in image passed.
# Threshold for valid face detect.
CONFIDENCE_THRES = 0.05
# Resize image for face detection.
# The tpu face det model used requires (320, 320).
res = resize_to_square(img=image, size=320, keep_aspect_ratio=True,
interpolation=cv2.INTER_AREA)
# Detect the (x, y)-coordinates of the bounding boxes corresponding
# to each face in the input image using the TPU engine.
# NB: reshape(-1) converts the res ndarray into 1-d.
detection = face_engine.DetectWithInputTensor(input_tensor=res.reshape(-1),
threshold=CONFIDENCE_THRES, top_k=1)
if not detection:
print('*** no face found! ***')
return None
# Convert coords and carve out face roi.
# Its assumed that only one face is in each image so take detection[0]
box = (detection[0].bounding_box.flatten().tolist()) * np.array([w, h, w, h])
(face_left, face_top, face_right, face_bottom) = box.astype('int')
#print('face_left: {} face_top: {} face_right: {} face_bottom: {}'
#.format(face_left, face_top, face_right, face_bottom))
# Return bounding box coords in dlib format.
return [(face_top, face_right, face_bottom, face_left)]
def cv2_encoder(image, boxes):
# Encode face into a 128-D representation (embeddings) using OpenCV.
# NB: Accuracy will be poor because face alignment is not performed first.
# TODO: See https://www.pyimagesearch.com/2017/05/22/face-alignment-with-opencv-and-python/
# Don't use this for now.
# Carve out face from bbox.
(face_top, face_right, face_bottom, face_left) = boxes[0]
face_roi = image[face_top:face_bottom, face_left:face_right, :]
# Construct a blob for the image, then pass the blob
# through the face embedding model to obtain the 128-d
# quantification of the face.
faceBlob = cv2.dnn.blobFromImage(
face_roi, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False)
embedder.setInput(faceBlob)
# Only one face is assumed so take the 1st element.
encoding = embedder.forward()[0]
return encoding
def dlib_encoder(image, boxes):
# Encode face into a 128-D representation (embeddings) using dlib.
# Convert image from BGR (OpenCV ordering) to dlib ordering (RGB).
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Generate encodings. Only one face is assumed so take the 1st element.
encoding = face_recognition.face_encodings(face_image=rgb,
known_face_locations=boxes, num_jitters=10)[0]
return encoding
# Loop over the image paths.
# NB: Its assumed that only one face is in each image.
for (i, imagePath) in enumerate(imagePaths):
print('processing image {}/{}'.format(i + 1,
len(imagePaths)))
# extract the person name from the image path
name = imagePath.split(sep)[-2]
# Load the input image.
image = cv2.imread(imagePath)
(h, w) = image.shape[:2]
if h == 0 or w == 0:
print('*** image size zero! ***')
continue
# Find face in image.
# The dlib method is most accurate but slow.
# The tpu method is pretty accurate and very fast.
# The cv2 method is somewhere in between.
print('...finding face in image')
boxes = tpu_face_det(image)
#boxes = cv2_face_det(image)
#boxes = dlib_face_det(image)
if boxes is None:
continue
#(face_top, face_right, face_bottom, face_left) = boxes[0]
#face_roi = image[face_top:face_bottom, face_left:face_right, :]
#cv2.imwrite('./face_roi{}.jpg'.format(i), face_roi)
# Compute the facial embedding (encoding).
# Don't use the cv2 method for now since accuracy
# is poor w/o face alignment.
# The dlib method is very accurate but relatively slow.
print('...encoding face')
#encoding = cv2_encoder(image, boxes)
encoding = dlib_encoder(image, boxes)
#print(encoding)
# Add encoding and name to set of known names and encodings.
knownEncodings.append(encoding)
knownNames.append(name)
# Dump the facial encodings + names to disk.
print('serializing encodings')
data = {'encodings': knownEncodings, 'names': knownNames}
with open(args['encodings'], 'wb') as outfile:
outfile.write(pickle.dumps(data))