-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathface.py
More file actions
163 lines (130 loc) · 4.53 KB
/
face.py
File metadata and controls
163 lines (130 loc) · 4.53 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
"""Face recognition of PC camera.
"""
import os
import cv2
import numpy as np
import utils.utils as u
from utils.window_manager import WindowManager
from utils.face_detector import FaceDetector
class Face:
def __init__(self, threshold):
"""Init.
# Arguments
threshold: Float, threshold for specific face.
"""
self._t = threshold
self._key = self._load_key()
self._key_cache = []
self._model = u.get_feature_model()
self._windowManager = WindowManager('Face', self.on_keypress)
self._faceDetector = FaceDetector('ssd', 0.5)
def run(self):
"""Run the main loop.
"""
capture = cv2.VideoCapture(0)
self._windowManager.create_window()
while self._windowManager.is_window_created:
success = capture.grab()
_, frame = capture.retrieve()
if frame is not None and success:
faces = self._faceDetector.detect(frame)
if self._key is not None and faces is not None:
label = self._compare_distance(frame, faces)
f = self._draw(frame, faces, label)
else:
f = self._draw(frame, faces)
self._windowManager.show(f)
self._windowManager.process_events(frame, faces)
def _load_key(self):
"""Load the key feature.
"""
kpath = 'data/key.npy'
if os.path.exists(kpath):
key = np.load('data/key.npy')
else:
key = None
return key
def _get_feat(self, frame, face):
"""Get face feature from frame.
# Arguments
frame: ndarray, video frame.
face: tuple, coordinates of face in the frame.
# Returns
feat: ndarray (128, ), face feature.
"""
f_h, f_w = frame.shape[:2]
x, y, w, h = face
x = max(0, x)
y = max(0, y)
w = min(f_w - x, w)
h = min(f_h - y, h)
img = frame[y: y + h, x: x + w, :]
image = u.process_image(img)
feat = self._model.predict(image)[0]
return feat
def _compare_distance(self, frame, faces):
"""Compare faces feature in the frame with key.
# Arguments
frame: ndarray, video frame.
faces: List, coordinates of faces in the frame.
# Returns
label: list, if match the key.
"""
label = []
for (x, y, w, h) in faces:
feat = self._get_feat(frame, (x, y, w, h))
dist = []
for k in self._key:
dist.append(np.linalg.norm(k - feat))
dist = min(dist)
print(dist)
if dist < self._t:
label.append(1)
else:
label.append(0)
print(label)
return label
def _draw(self, frame, faces, label=None):
"""Draw the rectangles in the frame.
# Arguments
frame: ndarray, video frame.
faces: List, coordinates of faces in the frame.
label: List, if match the key.
# Returns
f: ndarray, frame with rectangles.
"""
f = frame.copy()
color = [(0, 0, 255), (255, 0, 0)]
if label is None:
label = [0 for _ in range(len(faces))]
for rect, i in zip(faces, label):
(x, y, w, h) = rect
f = cv2.rectangle(f, (x, y),
(x + w, y + h),
color[i], 2)
return f
def on_keypress(self, keycode, frame, faces):
"""Handle a keypress event.
Press esc to quit window.
Press space 5 times to record different gestures of the face.
# Arguments
keycode: Integer, keypress event.
frame: ndarray, video frame.
faces: List, coordinates of faces in the frame.
"""
if keycode == 32: # space -> save face id.
nums = len(self._key_cache)
if nums < 5:
feat = self._get_feat(frame, faces[0])
self._key_cache.append(feat)
print('Face id {0} recorded!'.format(nums + 1))
else:
np.save('data/key.npy', np.array(self._key_cache))
print('All face ID recorded!')
self._key = self._key_cache
self._key_cache = []
elif keycode == 27: # escape -> quit
self._windowManager.destroy_window()
if __name__ == '__main__':
face = Face(0.3)
face.run()