-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_database.py
283 lines (239 loc) · 11.5 KB
/
face_database.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 cv2
import numpy as np
import tensorflow as tf
from scipy import misc
import json
import config
import facenet
from align import detect_face
args_margin = config.args_margin
args_image_size = config.args_image_size
minsize = config.minsize # minimum size of face
threshold = config.threshold # three steps's threshold
factor = config.factor # scale factor
face_threshold = config.face_threshold
db_name = config.db_name
known_img_path = config.known_img_path
update = config.update
# register
reg_person_names = ''
reg_locations = [0, 0, 0, 0]
reg_distances = 9999
reg_not_fount_count = 0
def face_detection(image_filename):
with tf.Graph().as_default():
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.25)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
with sess.as_default():
pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
# ---------------
args_seed = 666
# input a img of face closeup
np.random.seed(seed=args_seed)
src_path, _ = os.path.split(os.path.realpath(__file__))
args_model = os.path.expanduser(src_path + '/pre_train_models/20170512-110547.pb')
# Load the model
print('Loading feature extraction model')
facenet.load_model(args_model)
# 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_closeups = list()
face_source = list()
face_locations = list()
img = facenet.img_read(image_filename)
cv2.imshow("image", img)
cv2.waitKey(500)
bounding_boxes, points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
nrof_faces = bounding_boxes.shape[0]
if nrof_faces > 0:
det = bounding_boxes[:, 0:4]
img_size = np.asarray(img.shape)[0:2]
for det_no in range(nrof_faces):
each_det = np.squeeze(det[det_no])
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(each_det[0] - args_margin / 2, 0) # left Bound
bb[1] = np.maximum(each_det[1] - args_margin / 2, 0) # upper Bound
bb[2] = np.minimum(each_det[2] + args_margin / 2, img_size[1]) # right Bound
bb[3] = np.minimum(each_det[3] + args_margin / 2, img_size[0]) # lower Bound
cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
scaled = misc.imresize(cropped, (args_image_size, args_image_size), interp='bilinear')
# cv2.imshow("image", cropped)
# cv2.waitKey(1000)
face_closeups.append(scaled)
face_source.append(image_filename)
face_locations.append(bb)
face_vectors, face_source, _ = facenet.faceDB(db_name, img_path=known_img_path, update=update)
query_face_closeup = face_closeups
query_face_source = image_filename
query_face_locations = face_locations
query_processed_face_ = facenet.face_process(query_face_closeup, False, False, args_image_size)
# query_face_vector = inception_resnet_v2_facenet.get_face_vec(query_processed_face_)
print('Calculating features for images')
feed_dict = {images_placeholder: query_processed_face_, phase_train_placeholder: False}
emb_array = sess.run(embeddings, feed_dict=feed_dict)
query_face_vector = emb_array
# find the people who have those faces
# query_face_closeup, query_face_source, query_face_locations = get_face_img(np.atleast_1d(query_img_path))
source_list = list()
location_list = list()
name_list = list()
distance_list = list()
for face_no in range(len(query_face_vector)):
dist = facenet.cal_euclidean(query_face_vector[face_no], face_vectors)
# indices = dist.argsort()[:3] # find the indices of the 3 lower number
index = dist.argsort()[:1] # the most similar
# threshold checking
distance = dist[index]
if distance > face_threshold:
person_name = 'unknow'
else:
person_name = str(face_source[index]).split('/')[-2]
# faceInfo.append([query_face_source[face_no], query_face_locations[face_no], person_name, distance])
source_list.append(query_face_source[face_no])
location_list.append(query_face_locations[face_no])
name_list.append(person_name)
distance_list.append(distance)
# -------- register ---------
# global reg_not_fount_count
# global reg_person_names
# global reg_locations
# global reg_distances
#
# if nrof_faces == 0:
# if reg_not_fount_count < 10:
# name_list = reg_person_names
# face_locations = reg_locations
# distance_list = reg_distances
# reg_not_fount_count += 1
# else:
# reg_person_names = ''
# reg_locations = [0, 0, 0, 0]
# reg_distances = 9999
# else:
# reg_person_names = name_list.copy()
# reg_locations = face_locations.copy()
# reg_distances = distance_list.copy()
# reg_not_fount_count = 0
# ----------------------------
person_names = name_list
locations = face_locations
distances = distance_list
marked_frame = facenet.drawBoundaryBox([img] * len(emb_array), face_locations, name_list, distance_list)
for point in range(len(points)//2): # five points of each face
for face_no in range(len(points[point])):
cv2.circle(marked_frame[0], (points[point][face_no], points[point+5][face_no]), 2, (255, 255, 255), -1)
# cv2.circle(marked_frame[0], (447, 63), 5, (0, 0, 255), -1)
cv2.imshow("image", marked_frame[0])
cv2.waitKey(10000)
# ------------------------------------------------
if __name__ == "__main__":
# demo
image_filename = '/media/clliao/9c88dfb2-c12d-48cc-b30b-eaffb0cbf545/face_recognition_dataset/face_database/林高洲/007.png'
image_filename = '/media/clliao/9c88dfb2-c12d-48cc-b30b-eaffb0cbf545/face_recognition_dataset/26543368_10215276246329042_830858535_o.jpg'
img = facenet.img_read(image_filename)
# face_detection(image_filename)
# -------------------
# update database
db_face_vectors, db_face_source, _ = facenet.faceDB(db_name, img_path=known_img_path, update=True)
exit()
# -------------------
# show database
# output_json = list()
#
# for each_source in range(len(db_face_source)):
# name = db_face_source[each_source].split('/')[-2]
# vector = db_face_vectors[each_source]
# output_json.append({'face_name': name, "face_feature": vector})
#
# # with open('JSON_DB', 'w') as fw:
# # json.dump(output_json, fw)
# #
# # with open('JSON_DB', 'r') as fr:
# # output_json = json.load(fr)
#
# for each in output_json:
# print(each)
# --------------------
# get faces location
with tf.Graph().as_default():
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.25)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
with sess.as_default():
pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
args_seed = 666
# input a img of face closeup
np.random.seed(seed=args_seed)
src_path, _ = os.path.split(os.path.realpath(__file__))
args_model = os.path.expanduser(src_path + '/pre_train_models/20170512-110547.pb')
# Load the model
print('Loading feature extraction model')
facenet.load_model(args_model)
# 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")
bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
nrof_faces = bounding_boxes.shape[0]
# get face images
face_closeups = list()
face_source = list()
face_locations = list()
if nrof_faces > 0:
det = bounding_boxes[:, 0:4]
img_size = np.asarray(img.shape)[0:2]
for det_no in range(nrof_faces):
each_det = np.squeeze(det[det_no])
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(each_det[0] - args_margin / 2, 0) # left Bound
bb[1] = np.maximum(each_det[1] - args_margin / 2, 0) # upper Bound
bb[2] = np.minimum(each_det[2] + args_margin / 2, img_size[1]) # right Bound
bb[3] = np.minimum(each_det[3] + args_margin / 2, img_size[0]) # lower Bound
cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
scaled = misc.imresize(cropped, (args_image_size, args_image_size), interp='bilinear')
face_closeups.append(scaled)
face_source.append(image_filename)
face_locations.append(bb)
# --------------------
# pre-processing
query_face_closeup = face_closeups
query_face_source = image_filename
query_face_locations = face_locations
query_processed_face_ = facenet.face_process(query_face_closeup, False, False, args_image_size)
# --------------------
# face vectors
feed_dict = {images_placeholder: query_processed_face_, phase_train_placeholder: False}
emb_array = sess.run(embeddings, feed_dict=feed_dict)
query_face_vector = emb_array
# --------------------
# classifier
source_list = list()
location_list = list()
name_list = list()
distance_list = list()
for face_no in range(len(query_face_vector)):
dist = facenet.cal_euclidean(query_face_vector[face_no], db_face_vectors)
# indices = dist.argsort()[:3] # find the indices of the 3 lower number
index = dist.argsort()[:1] # the most similar
# threshold checking
distance = dist[index][0]
if distance > face_threshold:
person_name = config.unknown
else:
person_name = str(db_face_source[index]).split('/')[-2]
# faceInfo.append([query_face_source[face_no], query_face_locations[face_no], person_name, distance])
source_list.append(query_face_source[face_no])
location_list.append(query_face_locations[face_no])
name_list.append(person_name)
distance_list.append(distance)
# print(source_list)
print(person_name)
# show face closeup
for face in query_face_closeup:
cv2.imshow("image", face)
cv2.waitKey(1000)
print('--------------------------')