-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface-rec.py
154 lines (72 loc) · 2.22 KB
/
face-rec.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
# -*- coding:utf8 -*-
import sys,os,dlib,glob,numpy
from skimage import io
if len(sys.argv) != 5:
print r"请检查参数是否正确"
exit()
# 1.人脸关键点检测器
predictor_path = sys.argv[1]
# 2.人脸识别模型
face_rec_model_path = sys.argv[2]
# 3.候选人脸文件夹
faces_folder_path = sys.argv[3]
# 4.需识别的人脸
img_path = sys.argv[4]
# 1.加载正脸检测器
detector = dlib.get_frontal_face_detector()
# 2.加载人脸关键点检测器
sp = dlib.shape_predictor(predictor_path)
# 3. 加载人脸识别模型
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
# win = dlib.image_window()
# 候选人脸描述子list
descriptors = []
# 对文件夹下的每一个人脸进行:
# 1.人脸检测
# 2.关键点检测
# 3.描述子提取
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
#win.clear_overlay()
#win.set_image(img)
# 1.人脸检测
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
# 2.关键点检测
# shape = sp(img,d)
shape = sp(img,d)
# 画出人脸区域和和关键点
# win.clear_overlay()
# win.add_overlay(d)
# win.add_overlay(shape)
# 3.描述子提取,128D向量
face_descriptor = facerec.compute_face_descriptor(img, shape)
# 转换为numpy array
v = numpy.array(face_descriptor)
descriptors.append(v)
# 对需识别人脸进行同样处理
# 提取描述子,不再注释
img = io.imread(img_path)
dets = detector(img, 1)
dist = []
for k, d in enumerate(dets):
shape = sp(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
d_test = numpy.array(face_descriptor)
# 计算欧式距离
for i in descriptors:
dist_ = numpy.linalg.norm(i-d_test)
dist.append(dist_)
# 候选人名单
candidate = ['1','2','3','4','5','6','7','8','9','10','11']
# 候选人和距离组成一个dict
c_d = dict(zip(candidate,dist))
cd_sorted = sorted(c_d.iteritems(), key=lambda d:d[1])
if (cd_sorted):
print cd_sorted
print "\n The person is: ",cd_sorted[0][0]
else:
print "\n Nobady In here!"
dlib.hit_enter_to_continue()