-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrecognize.py
491 lines (376 loc) · 15.7 KB
/
recognize.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
"""
First run this 'extractEmbeddings.py' for extract embeddings
Second run this 'trainModel.py' for traine classification mode
Third run this file along with image path to recognize facess in image
Note:
1) We are using state of the art Face Detection Model called Retina-Face to detect facess acuuretly
2) This Face Recognition model detects only trained facess
3) We also give some thresholds to identify facess that are not in Traing Set as 'None'
4) We give 'None' to those facess
"""
import numpy as np
import pickle
import cv2
import os
import model as embedding
import imutils
import argparse
import torch
# we save 'RetinaFace' model at 'models/retinaface'
# we load retinaface model to detect facess
import torch.backends.cudnn as cudnn
from models.retinaface.config import cfg
from models.retinaface.prior_box import PriorBox
from models.retinaface.py_cpu_nms import py_cpu_nms
from retina_face import RetinaFace
from models.retinaface.box_utils import decode , decode_landm
import torchvision.transforms.functional as F
import matplotlib.pyplot as plt
from PIL import Image
import time
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
currentDir = os.getcwd()
# paths to embedding pickle file
embeddingPickle = os.path.join(currentDir, "output/FinalEmbeddings.pickle")
# path to save recognizer pickle file
recognizerPickle = os.path.join(currentDir, "output/FinalRecognizer.pickle")
# path to save labels pickle file
labelPickle = os.path.join(currentDir, "output/FinalLabel.pickle")
# path to save prdictedImages
predictedImg = os.path.join(currentDir, "predictedImg")
if not os.path.exists(predictedImg):
os.mkdir(predictedImg)
# Use argparse to get image path on commend line
# loading 'RetinaFace' weights to detect facess
trained_model_path = "models/retinaface/weights/Final_Retinaface.pth"
cpu = True
confidence_threshold = 0.05
top_k = 5000
nms_threshold = 0.3
keep_top_k = 750
save_image_path = "predictedImg"
vis_threshold = 0.6
### check_keys
def check_keys(model, pretrained_state_dict):
ckpt_keys = set(pretrained_state_dict.keys())
model_keys = set(model.state_dict().keys())
used_pretrained_keys = model_keys & ckpt_keys
unused_pretrained_keys = ckpt_keys - model_keys
missing_keys = model_keys - ckpt_keys
#print('Missing keys:{}'.format(len(missing_keys)))
#print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
#print('Used keys:{}'.format(len(used_pretrained_keys)))
assert len(used_pretrained_keys) > 0, 'load NONE from pretrained checkpoint'
return True
### remove_prefix
def remove_prefix(state_dict, prefix):
''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
#print('remove prefix \'{}\''.format(prefix))
f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
return {f(key): value for key, value in state_dict.items()}
### load_model
def load_model(model, pretrained_path, load_to_cpu):
#print('Loading pretrained model from {}'.format(pretrained_path))
if load_to_cpu:
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
else:
device = torch.cuda.current_device()
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
if "state_dict" in pretrained_dict.keys():
pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
else:
pretrained_dict = remove_prefix(pretrained_dict, 'module.')
check_keys(model, pretrained_dict)
model.load_state_dict(pretrained_dict, strict=False)
return model
torch.set_grad_enabled(False)
#net and model
net = RetinaFace(phase="test")
net = load_model(net , trained_model_path, cpu)
net.eval()
print("Finished loading model!")
cudnn.benchmark = True
device = torch.device("cpu" if cpu else "cuda")
net = net.to(device)
resize = 1
# load embedding model
embedder = embedding.InceptionResnetV1(pretrained="vggface2").eval()
# load the actual face recognition model along with the label encoder
recognizer = pickle.loads(open(recognizerPickle, "rb").read())
label = pickle.loads(open(labelPickle, "rb").read())
# loading embeddings pickle
data = pickle.loads(open(embeddingPickle, "rb").read())
COLORS = np.random.randint(0, 255, size=(len(label.classes_), 3), dtype="uint8")
Embeddings = np.array(data["embeddings"])
names = np.array(data["names"])
print("Embeddings ", Embeddings.shape)
print("Names ", names.shape)
#print("Labels ", labels.shape)
def distance(emb1, emb2):
return np.sum(np.square(emb1 - emb2))
video = cv2.VideoCapture(0)
while True:
ret,frame = video.read()
img = np.float32(frame)
im_height,im_width,_ = img.shape
scale = torch.Tensor([img.shape[1],img.shape[0],img.shape[1],img.shape[0]])
img -= (104,117,123)
img = img.transpose(2,0,1)
img = torch.from_numpy(img).unsqueeze(0)
img = img.to(device)
scale = scale.to(device)
tic = time.time()
loc,conf,landms = net(img) #forward pass
print('net forward time: {:.4f}'.format(time.time() - tic))
priorbox = PriorBox(cfg,image_size=(im_height,im_width))
priors = priorbox.forward()
priors = priors.to(device)
prior_data = priors.data
boxes = decode(loc.data.squeeze(0),prior_data,cfg['variance'])
boxes = boxes * scale / resize
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0).data.cpu().numpy()[:,1]
landms = decode_landm(landms.data.squeeze(0),prior_data,cfg['variance'])
scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2]])
scale1 = scale1.to(device)
landms = landms * scale1 / resize
landms = landms.cpu().numpy()
# ignore low scores
inds = np.where(scores > confidence_threshold)[0]
boxes = boxes[inds]
landms = landms[inds]
scores = scores[inds]
# keep top-K befor NMS
order = scores.argsort()[::-1][:top_k]
boxes = boxes[order]
landms = landms[order]
scores = scores[order]
# do NMS
dets = np.hstack((boxes,scores[:,np.newaxis])).astype(np.float32,copy=False)
keep = py_cpu_nms(dets,nms_threshold)
# keep = nms(dets,args.nms_threshold,force_cpu = args.cpu)
dets = dets[keep,:]
landms = landms[keep]
# keep top-K faster NMS
dets = dets[:keep_top_k,:]
landms = landms[:keep_top_k,:]
dets = np.concatenate((dets, landms), axis=1)
for b in dets:
if b[4] < vis_threshold:
continue
boxes = np.array(b[0:4])
boxes = boxes.astype('int')
(startX,startY,endX,endY) = boxes
face = frame[startY:endY,startX:endX]
try:
# print("yes-1")
faceRead = Image.fromarray(face)
faceRead = faceRead.resize((160,160),Image.ANTIALIAS)
faceRead = F.to_tensor(faceRead)
# print("yes-2")
except:
print("[Error] - resizing face " )
continue
# print(faceRead.shape)
# getting embeddings for cropped faces
faceEmbed = embedder(faceRead.unsqueeze(0))
flattenEmbed = faceEmbed.squeeze(0).detach().numpy()
# print(flattenEmbed.shape)
# predecting class
array = np.array(flattenEmbed).reshape(1,-1)
# perform classification to recognize the face
preds = recognizer.predict_proba(array)[0]
j = np.argmax(preds)
proba = preds[j]
name = label.classes_[j]
# print(name)
result = np.where(names == name)
resultEmbeddings = Embeddings[result]
dists = []
for emb in resultEmbeddings:
d = distance(emb,flattenEmbed)
dists.append(d)
# print(dists)
distarray = np.array(dists)
# print(distarray)
min_dist = np.min(distarray)
max_dist = np.max(distarray)
#print("Name : ",name)
#print("min dist : ",min_dist)
#print("max dist : ", max_dist)
if proba >= 0.5:
if (min_dist < 0.75 and max_dist < 1.4) or (min_dist < 0.5) or (proba ==1 and min_dist <= 0.5):
print("dist name ", name)
print("min dist :",min_dist)
print("max dist :",max_dist)
color = [int(c) for c in COLORS[j]]
cv2.rectangle(frame,(startX,startY),(endX,endY),color,2)
text = "{}: {:.2f}".format(name,proba)
cv2.putText(frame,text,(startX,startY - 5),cv2.FONT_HERSHEY_SIMPLEX,0.5,color,2)
else:
print("___________missing__________")
print("dist name",name)
print("min dist : ",min_dist)
print("max dist : ", max_dist)
print("probability : ",proba)
name = "NONE"
color = (255,255,0)
cv2.rectangle(frame,(startX,startY),(endX,endY),color,2)
text = "{}".format(name)
cv2.putText(frame,text,(startX,startY - 5), cv2.FONT_HERSHEY_SIMPLEX,0.5,color,2)
else:
name = "NONE"
color = (255,255,0)
cv2.rectangle(frame,(startX,startY),(endX,endY),color,2)
text = "{}".format(name)
cv2.putText(frame,text,(startX,startY - 5),cv2.FONT_HERSHEY_SIMPLEX,0.5,color,2)
cv2.imshow("Capture",frame)
key = cv2.waitKey(2)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()
# def detectFacess(path):
# image_path = path
# img_raw = cv2.imread(image_path, cv2.IMREAD_COLOR)
# #print(img_raw)
# img_raw_rgb = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB)
# imageName = image_path.split('/')[-1].split('.')[-2]
# img = np.float32(img_raw)
# im_height, im_width, _ = img.shape
# scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
# img -= (104, 117, 123)
# img = img.transpose(2, 0, 1)
# img = torch.from_numpy(img).unsqueeze(0)
# img = img.to(device)
# scale = scale.to(device)
# tic = time.time()
# loc, conf, landms = net(img) # forward pass
# print('net forward time: {:.4f}'.format(time.time() - tic))
# priorbox = PriorBox(cfg, image_size=(im_height, im_width))
# priors = priorbox.forward()
# priors = priors.to(device)
# prior_data = priors.data
# boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
# boxes = boxes * scale / resize
# boxes = boxes.cpu().numpy()
# scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
# landms = decode_landm(landms.data.squeeze(0), prior_data, cfg['variance'])
# scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
# img.shape[3], img.shape[2], img.shape[3], img.shape[2],
# img.shape[3], img.shape[2]])
# scale1 = scale1.to(device)
# landms = landms * scale1 / resize
# landms = landms.cpu().numpy()
# # ignore low scores
# inds = np.where(scores > confidence_threshold)[0]
# boxes = boxes[inds]
# landms = landms[inds]
# scores = scores[inds]
# # keep top-K before NMS
# order = scores.argsort()[::-1][:top_k]
# boxes = boxes[order]
# landms = landms[order]
# scores = scores[order]
# # do NMS
# dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
# keep = py_cpu_nms(dets, nms_threshold)
# # keep = nms(dets, args.nms_threshold,force_cpu=args.cpu)
# dets = dets[keep, :]
# landms = landms[keep]
# # keep top-K faster NMS
# dets = dets[:keep_top_k, :]
# landms = landms[:keep_top_k, :]
# dets = np.concatenate((dets, landms), axis=1)
# for b in dets:
# if b[4] < vis_threshold:
# continue
# boxes = np.array(b[0:4])
# boxes = boxes.astype('int')
# (startX , startY, endX, endY) = boxes
# face = img_raw_rgb[startY:endY , startX:endX]
# try:
# #print("yes-1")
# faceRead = Image.fromarray(face)
# faceRead = faceRead.resize((160, 160), Image.ANTIALIAS)
# faceRead = F.to_tensor(faceRead)
# #print("yes-2")
# except:
# print("[Error] - resizing face ")
# continue
# #print(faceRead.shape)
# # getting embeddings for croped faces
# faceEmbed = embedder(faceRead.unsqueeze(0))
# flattenEmbed = faceEmbed.squeeze(0).detach().numpy()
# #print(flattenEmbed.shape)
# # predectiong class
# array = np.array(flattenEmbed).reshape(1,-1)
# # perform classification to recognize the face
# preds = recognizer.predict_proba(array)[0]
# j = np.argmax(preds)
# proba = preds[j]
# name = label.classes_[j]
# #print(name)
# result = np.where(names == name)
# resultEmbeddings = Embeddings[result]
# dists = []
# for emb in resultEmbeddings:
# d = distance(emb, flattenEmbed)
# dists.append(d)
# #print(dists)
# distarray = np.array(dists)
# #print(distarray)
# min_dist = np.min(distarray)
# max_dist = np.max(distarray)
# #print("Name : ",name)
# #print("min dist : ",min_dist)
# #print("max dist : ", max_dist)
# if proba >= 0.5:
# if (min_dist < 0.75 and max_dist < 1.4) or (min_dist < 0.5) or (proba == 1 and min_dist <= 0.5):
# print("dist name ", name)
# print("min dist : ",min_dist)
# print("max dist : ", max_dist)
# color = [int(c) for c in COLORS[j]]
# cv2.rectangle(img_raw, (startX, startY), (endX, endY), color, 2)
# text = "{}: {:.2f}".format(name, proba)
# cv2.putText(img_raw,text, (startX, startY - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# else:
# print("________________missing______________")
# print("dist name ", name)
# print("min dist : ",min_dist)
# print("max dist : ", max_dist)
# print("probability :",proba)
# name = "NONE"
# color = (255, 255, 255)
# cv2.rectangle(img_raw, (startX, startY), (endX, endY), color, 2)
# text = "{}".format(name)
# cv2.putText(img_raw,text, (startX, startY - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# else:
# name = "NONE"
# color = (255, 255, 255)
# cv2.rectangle(img_raw, (startX, startY), (endX, endY), color, 2)
# text = "{}".format(name)
# cv2.putText(img_raw,text, (startX, startY - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# # save image predicte foler
# cv2.imwrite("{}/{}.png".format(predictedImg, imageName), img_raw)
# #im = Image.open("{}/{}.png".format(predictedImg,imageName))
# #return im
# cv2.imshow(imageName, img_raw)
# cv2.waitKey(0)
# if __name__ == '__main__':
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--imagePath", required=True, help="Image path to recognize facess")
# args = vars(ap.parse_args())
# imagePath = args["imagePath"]
# currentDirImage = os.getcwd()
# print(currentDirImage)
# ImageDir = os.path.join(currentDirImage,imagePath)
# print(ImageDir)
# if not os.path.exists(ImageDir):
# print("Image not exists")
# #print("image path: ",ImageDir)
# readImg = plt.imread(ImageDir)
# #print("shape :", readImg.shape)
# detectFacess(imagePath)