-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpklinit.py
executable file
·189 lines (151 loc) · 6.55 KB
/
pklinit.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
#!/usr/bin/python3.4
# -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import sys
#sys.path.append("/home/yanai-lab/araki-t/Git/facenet/src/")
import os
import argparse
<<<<<<< HEAD:pklinit.py
import facenet
import facenets.src.align.detect_face
=======
import arakinet
import facenet.src.align.detect_face
>>>>>>> 111bf877d13a051cc656ad54d6d0f2428220a14e:pklinit.py
import pickle
import scipy
from scipy import misc
img_paths_list = [] #{./Face/image1, ./Face/image2,...}
#imglist = [] # {image1,image2,image....}
#distance = {} # {[image:distance],[:],...}
#likelist = [] # alike image
def main(args):
args_filepaths = args.image_files
image_size = args.image_size
margin = args.margin
gpu_memory_fraction = args.gpu_memory_fraction
model = args.model
batch_size = args.batch_size
embs = []
extracted_filepaths = []
with tf.Graph().as_default():
with tf.Session() as sess:
# Load the model
arakinet.load_model(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")
for i in range(0, len(args_filepaths), batch_size):
target_filepaths = args_filepaths[i:i+batch_size]
print("target_filepaths len:{}".format(len(target_filepaths)))
images, target_filepaths = load_and_align_data(target_filepaths, image_size, margin, gpu_memory_fraction)
print("target_filepaths len:{}".format(len(target_filepaths)))
# Run forward pass to calculate embeddings
feed_dict = { images_placeholder: images, phase_train_placeholder:False }
emb = sess.run(embeddings, feed_dict=feed_dict)
print("emb len:{}".format(len(emb)))
for j in range(len(target_filepaths)):
extracted_filepaths.append(target_filepaths[j])
embs.append(emb[j, :])
save_embs(embs, extracted_filepaths)
def save_embs(embs, paths):
# 特徴量の取得
reps = {}
for i, (emb, path) in enumerate(zip(embs, paths)):
#print('%1d: %s' % (i, paths))
#print(emb)
try:
basename = os.path.basename(path)
reps[basename] = emb
except:
print('error %1d: %s' % (i, path) )
# 特徴量の保存
with open('img_facenet.pkl', 'wb') as f:
pickle.dump(reps, f)
def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction):
# 処理が正常に行えた画像パス
extracted_filepaths = []
minsize = 20 # minimum size of face
threshold = [ 0.6, 0.7, 0.7 ] # three steps's threshold
factor = 0.709 # scale factor
print('Creating networks and loading parameters')
with tf.Graph().as_default():
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
with sess.as_default():
pnet, rnet, onet = facenet.src.align.detect_face.create_mtcnn(sess, None)
nrof_samples = len(image_paths)
img_list = [] #[None] * nrof_samples
for i in range(nrof_samples):
print('%1d: %s' % (i, image_paths[i]))
img_paths_list.append(image_paths[i])
img = misc.imread(os.path.expanduser(image_paths[i]))
img_size = np.asarray(img.shape)[0:2]
try:
bounding_boxes, _ = facenet.src.align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
det = np.squeeze(bounding_boxes[0,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],:]
aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
prewhitened = arakinet.prewhiten(aligned)
#img_list[i] = prewhitened
img_list.append(prewhitened)
extracted_filepaths.append(image_paths[i])
except:
print("cannot extract_image_align")
image = np.stack(img_list)
return image, extracted_filepaths
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, help='Could be either a directory containing the meta_file and ckpt_file or a model protobuf (.pb) file', default="./Models/20180402-114759/20180402-114759.pb")
parser.add_argument('image_files', type=str, nargs='+', help='Images to compare')
parser.add_argument('--image_size', type=int,
help='Image size (height, width) in pixels.', default=160)
parser.add_argument('--margin', type=int,
help='Margin for the crop around the bounding box (height, width) in pixels.', default=44)
parser.add_argument('--gpu_memory_fraction', type=float,
help='Upper bound on the amount of GPU memory that will be used by the process.', default=1.0)
parser.add_argument('--batch_size', type=int,
help='Batch size for extraction image emb', default=1000)
return parser.parse_args(argv)
if __name__ == '__main__':
# try:
main(parse_arguments(sys.argv[1:]))
# except:
# print('\n\nError: too few arguments\n Please execute "' + sys.argv[0] + ' [MODEL] [IMGdir]/* "\n\n')
# import facechecker
# facechecker.checking("output.jpg")
exit()
#from scipy import spatial
pkl_path = "img_facenet.pkl"
with open(pkl_path, 'rb') as f:
data = pickle.load(f)
print(img_paths_list)
for i in img_paths_list:
imglist.append(i.split('/')[-1])
#print(imglist)
# A = data[img_paths_list[0].split('/')[-1]]
# B = data[img_paths_list[1].split('/')[-1]]
# print("A,B")
# print(scipy.spatial.distance.euclidean(A, B))
# print("A,C")
# print(scipy.spatial.distance.euclidean(A, C))
for i in imglist:
distance[i] = scipy.spatial.distance.euclidean(data[i], data[imglist[-1]])
# print(i)
# print(scipy.spatial.distance.euclidean(data[i], data[imglist[-1]]))
for j,k in sorted(distance.items(), key=lambda x:x[1]):
print(k,"\t",j)
if k > 0 and k < 1:
likelist.append(j)
print(likelist)
#print("Inputed image is like %s"%)