-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfacenote.py
More file actions
210 lines (172 loc) · 8.07 KB
/
facenote.py
File metadata and controls
210 lines (172 loc) · 8.07 KB
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import cv2
from scipy import misc
import tensorflow as tf
import numpy as np
import sys
import os
import copy
import argparse
from core import facenet
from core import detect_face
import random
from os.path import join as pjoin
import matplotlib.pyplot as plt
import sklearn
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.externals import joblib
def to_rgb(img):
w, h = img.shape
ret = np.empty((w, h, 3), dtype=np.uint8)
ret[:, :, 0] = ret[:, :, 1] = ret[:, :, 2] = img
return ret
def read_img(person_dir,f):
img=cv2.imread(pjoin(person_dir, f))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 判断数组维度
if gray.ndim == 2:
img = to_rgb(gray)
return img
def load_data(data_dir):
data = {}
for guy in os.listdir(data_dir):
person_dir = pjoin(data_dir, guy)
curr_pics = [read_img(person_dir, f) for f in os.listdir(person_dir)]
# 存储每一类人的文件夹内所有图片
data[guy] = curr_pics
return data
# from compare.py
def load_and_align_data(image, image_size, margin, gpu_memory_fraction):
# from align_dataset_mtcnn.py
minsize = 20 # minimum size of face
threshold = [ 0.6, 0.7, 0.7 ] # three steps's threshold
factor = 0.709 # scale factor
#mtcnn
print('Creating networks and loading parameters')
with tf.Graph().as_default():
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0)
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, './data')
# 读取图片
img = image
# 获取图片的shape
img_size = np.asarray(img.shape)[0:2]
# 返回边界框数组 (参数分别是输入图片 脸部最小尺寸 三个网络 阈值 factor不清楚)
bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
if len(bounding_boxes) < 1:
return 0,0,0
else:
crop=[]
det=bounding_boxes
det[:,0]=np.maximum(det[:,0], 0)
det[:,1]=np.maximum(det[:,1], 0)
det[:,2]=np.minimum(det[:,2], img_size[1])
det[:,3]=np.minimum(det[:,3], img_size[0])
# det[:,0]=np.maximum(det[:,0]-margin/2, 0)
# det[:,1]=np.maximum(det[:,1]-margin/2, 0)
# det[:,2]=np.minimum(det[:,2]+margin/2, img_size[1])
# det[:,3]=np.minimum(det[:,3]+margin/2, img_size[0])
det=det.astype(int)
for i in range(len(bounding_boxes)):
temp_crop=img[det[i,1]:det[i,3],det[i,0]:det[i,2],:]
aligned=misc.imresize(temp_crop, (image_size, image_size), interp='bilinear')
prewhitened = facenet.prewhiten(aligned)
crop.append(prewhitened)
crop_image=np.stack(crop)
return det,crop_image,1
def main(args):
model_dir = args.model_dir
with tf.Graph().as_default():
with tf.Session() as sess:
meta_file, ckpt_file = facenet.get_model_filenames(args.model_dir)
facenet.load_model(model_dir,meta_file,ckpt_file)
print('建立facenet embedding模型')
# 返回给定名称的tensor
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")
model = joblib.load('./models/knn_classifier.model')
#开启ip摄像头
# 参数为0表示打开内置摄像头,参数是视频文件路径则打开视频
capture =cv2.VideoCapture(video)
cv2.namedWindow("camera",1)
c=0
num = 0
frame_interval=3 # frame intervals
while True:
ret, frame = capture.read()
timeF = frame_interval
# print(shape(frame))
detect_face=[]
if(c%timeF == 0):
find_results=[]
# cv2.imshow("camera",frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if gray.ndim == 2:
img = to_rgb(gray)
det,crop_image,j= load_and_align_data(img, 160, 44, 1.0)
if j:
feed_dict = {images_placeholder: crop_image, phase_train_placeholder:False }
emb = sess.run(embeddings, feed_dict=feed_dict)
for xx in range(len(emb)):
print(type(emb[xx,:]),emb[xx,:].shape)
detect_face.append(emb[xx,:])
detect_face=np.array(detect_face)
detect_face=detect_face.reshape(-1,128)
print('facenet embedding模型建立完毕')
predict = model.predict(detect_face)
print(predict)
result=[]
for i in range(len(predict)):
if predict[i]==0:
result.append('Chen')
elif predict[i]==1:
result.append('Reenie')
# 绘制矩形框并标注文字
for rec_position in range(len(det)):
cv2.rectangle(frame,(det[rec_position,0],det[rec_position,1]),(det[rec_position,2],det[rec_position,3]),(0, 255, 0), 2, 8, 0)
cv2.putText(
frame,
result[rec_position],
(det[rec_position,0],det[rec_position,1]),
cv2.FONT_HERSHEY_COMPLEX_SMALL,
0.8,
(0, 0 ,255),
thickness = 2,
lineType = 2)
cv2.imshow('camera',frame)
c+=1
key = cv2.waitKey(3)
if key == 27:
#esc键退出
print("esc break...")
break
if key == ord(' '):
# 保存一张图像
num = num+1
filename = "frames_%s.jpg" % num
cv2.imwrite(filename,frame)
# When everything is done, release the capture
capture.release()
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', type=str,
help='Directory containing the meta_file and ckpt_file', default='./20170512-110547')
#parser.add_argument('--dlib_face_predictor', type=str,
#help='File containing the dlib face predictor.', default='../data/shape_predictor_68_face_landmarks.dat')
#parser.add_argument('--image_size', type=int,
# help='Image size (height, width) in pixels.', default=160)
#parser.add_argument('image_files', type=str, nargs='+', help='Images to compare')
#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)
return parser.parse_args(argv)
if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))