-
Notifications
You must be signed in to change notification settings - Fork 1
/
Face_recognition_app.py
283 lines (229 loc) · 9.44 KB
/
Face_recognition_app.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
import os
import pickle
import time
import imutils
import tensorflow as tf
import numpy as np
from imutils import paths
from imutils.video import VideoStream
import facenet
from align import detect_face
import cv2
# some constants kept as default from facenet
minsize = 20
threshold = [0.6, 0.7, 0.7]
factor = 0.709
margin = 44
input_image_size = 160
sess = tf.Session()
# read pnet, rnet, onet models from align directory and files are det1.npy, det2.npy, det3.npy
pnet, rnet, onet = detect_face.create_mtcnn(sess, 'align')
# read 20170512-110547 model file downloaded from https://drive.google.com/file/d/0B5MzpY9kBtDVZ2RpVDYwWmxoSUk
facenet.load_model("20170512-110547/20170512-110547.pb")
# Get input and output tensors
images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
embedding_size = embeddings.get_shape()[1]
##########################
face_embedding_dict = {}
##########################
def getFace(img):
faces = []
img_size = np.asarray(img.shape)[0:2]
bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
if not len(bounding_boxes) == 0:
for face in bounding_boxes:
if face[4] > 0.50:
det = np.squeeze(face[0:4])
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(det[0] - margin / 2, 0)
bb[1] = np.maximum(det[1] - margin / 2, 0)
bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
resized = cv2.resize(cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC)
prewhitened = facenet.prewhiten(resized)
faces.append(
{'face': resized, 'rect': [bb[0], bb[1], bb[2], bb[3]], 'embedding': getEmbedding(prewhitened)})
return faces
def getEmbedding(resized):
reshaped = resized.reshape(-1, input_image_size, input_image_size, 3)
feed_dict = {images_placeholder: reshaped, phase_train_placeholder: False}
embedding = sess.run(embeddings, feed_dict=feed_dict)
return embedding
def compare2faces_decorator(func):
def inner(img1, img2):
# img1 = cv2.imread(img1)
imgg1 = imutils.resize(img1, width=1000)
# img2 = cv2.imread(img2)
imgg2 = imutils.resize(img2, width=1000)
return func(imgg1, imgg2)
return inner
@compare2faces_decorator
def compare2face(img1, img2):
face1 = getFace(img1)
face2 = getFace(img2)
##########################################ash
for face in face1:
cv2.rectangle(img1, (face['rect'][0], face['rect'][1]), (face['rect'][2], face['rect'][3]), (0, 255, 0), 2)
cv2.imshow("faces", img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
for face in face2:
cv2.rectangle(img2, (face['rect'][0], face['rect'][1]), (face['rect'][2], face['rect'][3]), (0, 255, 0), 2)
cv2.imshow("faces", img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
#############################################
if face1 and face2:
# calculate Euclidean distance
dist = np.sqrt(np.sum(np.square(np.subtract(face1[0]['embedding'], face2[0]['embedding']))))
return dist
return -1
def extract_embeddings_datset():
print("[INFO] quantifying faces...")
imagePaths = list(paths.list_images("dataset/"))
#print(imagePaths)
knownEmbeddings = []
knownNames = []
total=0
for (i, imagePath) in enumerate(imagePaths):
# extract the person name from the image path
print("[INFO] processing image {}/{}".format(i + 1, len(imagePaths)))
name = imagePath.split(os.path.sep)[-2]
# load the image, resize it to have a width of 600 pixels (while
# maintaining the aspect ratio), and then grab the image
# dimensions
image = cv2.imread(imagePath)
image = imutils.resize(image, width=600)
(h, w) = image.shape[:2]
frame = image
faces = getFace(frame)
for face in faces:
cv2.rectangle(frame, (face['rect'][0], face['rect'][1]), (face['rect'][2], face['rect'][3]), (0, 255, 0), 2)
person_embedding = face['embedding']
knownNames.append(name)
#print(name)
knownEmbeddings.append(person_embedding.flatten())
total += 1
print("[INFO] serializing {} encodings...".format(total))
data = {"embeddings": knownEmbeddings, "names": knownNames}
f = open("output/embeddings.pickle", "wb")
f.write(pickle.dumps(data))
f.close()
# cv2.imshow("faces", frame)
def addperson():
print("[INFO] starting video stream, please wait...")
vs = VideoStream(src=0).start()
time.sleep(3)
global frame
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 1000 pixels
frame = vs.read()
# frame = imutils.resize(frame, width=1000, height=10000)
faces = getFace(frame)
for face in faces:
cv2.rectangle(frame, (face['rect'][0], face['rect'][1]), (face['rect'][2], face['rect'][3]), (0, 255, 0), 2)
cv2.imshow("faces", frame)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, break from the loop and use last frame to extract embeddings
if key == ord("q"):
break
# cleanup and closing frame
cv2.destroyAllWindows()
vs.stop()
person_embedding = getEmbedding(frame)
name = input("Enter Name for the person")
# face_embedding_dict[name]=person_embedding
print(person_embedding)
print(type(person_embedding))
##########################
knownEmbeddings = []
knownNames = []
total = 0
knownNames.append(name)
knownEmbeddings.append(person_embedding.flatten())
# dump the facial embeddings + names to disk
print("[INFO] serializing {} encodings...".format(total))
data = {"embeddings": knownEmbeddings, "names": knownNames}
f = open("output/embeddings.pickle", "wb")
f.write(pickle.dumps(data))
f.close()
def findPersonImage(img):
recognizer = pickle.loads(open("output/recognizer.pickle", "rb").read())
le = pickle.loads(open("output/labelencoder.pickle", "rb").read())
frame=img
faces = getFace(frame)
for face in faces:
cv2.rectangle(frame, (face['rect'][0], face['rect'][1]), (face['rect'][2], face['rect'][3]), (0, 255, 0), 2)
person_embedding = face['embedding']
preds = recognizer.predict_proba(person_embedding)
j = np.argmax(preds)
proba = preds[0][j]
name = le.classes_[j]
text = "{}: {:.2f}%".format(name, proba * 100)
y = face['rect'][1] - 10 if face['rect'][1] - 10 > 10 else face['rect'][1] + 10
cv2.putText(img, text, (face['rect'][0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.imshow("faces", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cleanup and closing frame
def findPersonVideo():
recognizer = pickle.loads(open("output/recognizer.pickle", "rb").read())
le = pickle.loads(open("output/labelencoder.pickle", "rb").read())
print("[INFO] starting video stream, please wait...")
vs = VideoStream(src=0).start()
time.sleep(3)
global frame
# loop over the frames from the video stream
while True:
frame = vs.read()
#person_embedding = getEmbedding(frame)
#frame = imutils.resize(frame, width=1000, height=10000)
faces = getFace(frame)
print(len(frame[0]))
for face in faces:
cv2.rectangle(frame, (face['rect'][0], face['rect'][1]), (face['rect'][2], face['rect'][3]), (0, 255, 0), 2)
person_embedding = face['embedding']
preds = recognizer.predict_proba(person_embedding)
j = np.argmax(preds[0])
# if(j>=len(preds)):
# continue
proba = preds[0][j]
name = le.classes_[j]
#print("NAME:"+str(name))
text = "{}: {:.2f}%".format(name, proba * 100)
y= face['rect'][1] - 10 if face['rect'][1] -10 >10 else face['rect'][1] +10
cv2.putText(frame, text, (face['rect'][0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
cv2.imshow("faces", frame)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, break from the loop and use last frame to extract embeddings
if key == ord("q"):
break
# cleanup and closing frame
cv2.destroyAllWindows()
vs.stop()
####################################
if __name__ == "__main__":
#choice = int(input("\n1.Add Person\n2.Extract_emb_dataset"))
choice=1
if (choice == 1):
findPersonVideo()
elif (choice == 2):
extract_embeddings_datset()
elif(choice == 3):
img=cv2.imread("Brooke.jpeg")
findPersonImage(img)
# img1 = cv2.imread(args.img1)
# img2 = cv2.imread(args.img2)
# distance = compare2face(img1, img2)
# threshold = 1.10 # set yourself to meet your requirement
#
# if distance==-1:
# print("Face not found in one of the image")
# else:
# print("distance = "+str(distance))
# print("Result = " + ("same person" if distance <= threshold else "not same person"))