-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognition.py
294 lines (243 loc) · 13.6 KB
/
face_recognition.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
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""
Copyright (c) 2018-2022 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import logging as log
import sys
from argparse import ArgumentParser
from pathlib import Path
from time import perf_counter
from datetime import datetime
import cv2
import numpy as np
from openvino.runtime import Core, get_version
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python'))
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python/openvino/model_zoo'))
from utils import crop
from landmarks_detector import LandmarksDetector
from face_detector import FaceDetector
from faces_database import FacesDatabase
from face_identifier import FaceIdentifier
import monitors
from helpers import resolution
from images_capture import open_images_capture
from model_api.models import OutputTransform
from model_api.performance_metrics import PerformanceMetrics
log.basicConfig(format='[ %(levelname)s ] %(message)s', level=log.DEBUG, stream=sys.stdout)
DEVICE_KINDS = ['CPU', 'GPU', 'MYRIAD', 'HETERO', 'HDDL']
def build_argparser():
parser = ArgumentParser()
general = parser.add_argument_group('General')
general.add_argument('-i', '--input', required=True,
help='Required. An input to process. The input must be a single image, '
'a folder of images, video file or camera id.')
general.add_argument('--loop', default=False, action='store_true',
help='Optional. Enable reading the input in a loop.')
general.add_argument('-o', '--output',
help='Optional. Name of the output file(s) to save.')
general.add_argument('-limit', '--output_limit', default=1000, type=int,
help='Optional. Number of frames to store in output. '
'If 0 is set, all frames are stored.')
general.add_argument('--output_resolution', default=None, type=resolution,
help='Optional. Specify the maximum output window resolution '
'in (width x height) format. Example: 1280x720. '
'Input frame size used by default.')
general.add_argument('--no_show', action='store_true',
help="Optional. Don't show output.")
general.add_argument('--crop_size', default=(0, 0), type=int, nargs=2,
help='Optional. Crop the input stream to this resolution.')
general.add_argument('--match_algo', default='HUNGARIAN', choices=('HUNGARIAN', 'MIN_DIST'),
help='Optional. Algorithm for face matching. Default: HUNGARIAN.')
general.add_argument('-u', '--utilization_monitors', default='', type=str,
help='Optional. List of monitors to show initially.')
gallery = parser.add_argument_group('Faces database')
gallery.add_argument('-fg', default='', help='Optional. Path to the face images directory.')
gallery.add_argument('--run_detector', action='store_true',
help='Optional. Use Face Detection model to find faces '
'on the face images, otherwise use full images.')
gallery.add_argument('--allow_grow', action='store_true',
help='Optional. Allow to grow faces gallery and to dump on disk. '
'Available only if --no_show option is off.')
models = parser.add_argument_group('Models')
models.add_argument('-m_fd', type=Path, required=True,
help='Required. Path to an .xml file with Face Detection model.')
models.add_argument('-m_lm', type=Path, required=True,
help='Required. Path to an .xml file with Facial Landmarks Detection model.')
models.add_argument('-m_reid', type=Path, required=True,
help='Required. Path to an .xml file with Face Reidentification model.')
models.add_argument('--fd_input_size', default=(0, 0), type=int, nargs=2,
help='Optional. Specify the input size of detection model for '
'reshaping. Example: 500 700.')
infer = parser.add_argument_group('Inference options')
infer.add_argument('-d_fd', default='CPU', choices=DEVICE_KINDS,
help='Optional. Target device for Face Detection model. '
'Default value is CPU.')
infer.add_argument('-d_lm', default='CPU', choices=DEVICE_KINDS,
help='Optional. Target device for Facial Landmarks Detection '
'model. Default value is CPU.')
infer.add_argument('-d_reid', default='CPU', choices=DEVICE_KINDS,
help='Optional. Target device for Face Reidentification '
'model. Default value is CPU.')
infer.add_argument('-v', '--verbose', action='store_true',
help='Optional. Be more verbose.')
infer.add_argument('-t_fd', metavar='[0..1]', type=float, default=0.6,
help='Optional. Probability threshold for face detections.')
infer.add_argument('-t_id', metavar='[0..1]', type=float, default=0.3,
help='Optional. Cosine distance threshold between two vectors '
'for face identification.')
infer.add_argument('-exp_r_fd', metavar='NUMBER', type=float, default=1.15,
help='Optional. Scaling ratio for bboxes passed to face recognition.')
return parser
class FrameProcessor:
QUEUE_SIZE = 16
def __init__(self, args):
self.allow_grow = args.allow_grow and not args.no_show
log.info('OpenVINO Runtime')
log.info('\tbuild: {}'.format(get_version()))
core = Core()
self.face_detector = FaceDetector(core, args.m_fd,
args.fd_input_size,
confidence_threshold=args.t_fd,
roi_scale_factor=args.exp_r_fd)
self.landmarks_detector = LandmarksDetector(core, args.m_lm)
self.face_identifier = FaceIdentifier(core, args.m_reid,
match_threshold=args.t_id,
match_algo=args.match_algo)
self.face_detector.deploy(args.d_fd)
self.landmarks_detector.deploy(args.d_lm, self.QUEUE_SIZE)
self.face_identifier.deploy(args.d_reid, self.QUEUE_SIZE)
log.debug('Building faces database using images from {}'.format(args.fg))
self.faces_database = FacesDatabase(args.fg, self.face_identifier,
self.landmarks_detector,
self.face_detector if args.run_detector else None, args.no_show)
self.face_identifier.set_faces_database(self.faces_database)
log.info('Database is built, registered {} identities'.format(len(self.faces_database)))
def process(self, frame):
orig_image = frame.copy()
rois = self.face_detector.infer((frame,))
if self.QUEUE_SIZE < len(rois):
log.warning('Too many faces for processing. Will be processed only {} of {}'
.format(self.QUEUE_SIZE, len(rois)))
rois = rois[:self.QUEUE_SIZE]
landmarks = self.landmarks_detector.infer((frame, rois))
face_identities, unknowns = self.face_identifier.infer((frame, rois, landmarks))
if self.allow_grow and len(unknowns) > 0:
for i in unknowns:
# This check is preventing asking to save half-images in the boundary of images
if rois[i].position[0] == 0.0 or rois[i].position[1] == 0.0 or \
(rois[i].position[0] + rois[i].size[0] > orig_image.shape[1]) or \
(rois[i].position[1] + rois[i].size[1] > orig_image.shape[0]):
continue
crop_image = crop(orig_image, rois[i])
name = self.faces_database.ask_to_save(crop_image)
if name:
id = self.faces_database.dump_faces(crop_image, face_identities[i].descriptor, name)
face_identities[i].id = id
return [rois, landmarks, face_identities]
def markAttendance(text,confidence=1):
print(f"[ LOG ] writing attendance for {text} with confidence {confidence}")
with open('Attendance.csv', 'r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
now = datetime.now()
dtString = now.strftime('%H:%M:%S')
if text=="Unknown": # and confidence<75
# f.writelines(f'Unknown,{dtString},{confidence}\n')
placeholder=True
elif text not in nameList:
f.writelines(f'{text},{dtString},{confidence}\n')
def draw_detections(frame, frame_processor, detections, output_transform):
size = frame.shape[:2]
frame = output_transform.resize(frame)
for roi, landmarks, identity in zip(*detections):
confidence=0
text = frame_processor.face_identifier.get_identity_label(identity.id)
if identity.id != FaceIdentifier.UNKNOWN_ID:
# text += ' %.2f%%' % (100.0 * (1 - identity.distance))
confidence=(100.0 * (1 - identity.distance))
xmin = max(int(roi.position[0]), 0)
ymin = max(int(roi.position[1]), 0)
xmax = min(int(roi.position[0] + roi.size[0]), size[1])
ymax = min(int(roi.position[1] + roi.size[1]), size[0])
xmin, ymin, xmax, ymax = output_transform.scale([xmin, ymin, xmax, ymax])
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 220, 0), 2)
for point in landmarks:
x = xmin + output_transform.scale(roi.size[0] * point[0])
y = ymin + output_transform.scale(roi.size[1] * point[1])
cv2.circle(frame, (int(x), int(y)), 1, (0, 255, 255), 2)
textsize = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 1)[0]
cv2.rectangle(frame, (xmin, ymin), (xmin + textsize[0], ymin - textsize[1]), (255, 255, 255), cv2.FILLED)
cv2.putText(frame, text, (xmin, ymin), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 1)
markAttendance(text,confidence)
return frame
def center_crop(frame, crop_size):
fh, fw, _ = frame.shape
crop_size[0], crop_size[1] = min(fw, crop_size[0]), min(fh, crop_size[1])
return frame[(fh - crop_size[1]) // 2 : (fh + crop_size[1]) // 2,
(fw - crop_size[0]) // 2 : (fw + crop_size[0]) // 2,
:]
def main():
args = build_argparser().parse_args()
cap = open_images_capture(args.input, args.loop)
frame_processor = FrameProcessor(args)
frame_num = 0
metrics = PerformanceMetrics()
presenter = None
output_transform = None
input_crop = None
if args.crop_size[0] > 0 and args.crop_size[1] > 0:
input_crop = np.array(args.crop_size)
elif not (args.crop_size[0] == 0 and args.crop_size[1] == 0):
raise ValueError('Both crop height and width should be positive')
video_writer = cv2.VideoWriter()
while True:
start_time = perf_counter()
frame = cap.read()
if frame is None:
if frame_num == 0:
raise ValueError("Can't read an image from the input")
break
if input_crop:
frame = center_crop(frame, input_crop)
if frame_num == 0:
output_transform = OutputTransform(frame.shape[:2], args.output_resolution)
if args.output_resolution:
output_resolution = output_transform.new_resolution
else:
output_resolution = (frame.shape[1], frame.shape[0])
presenter = monitors.Presenter(args.utilization_monitors, 55,
(round(output_resolution[0] / 4), round(output_resolution[1] / 8)))
if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'),
cap.fps(), output_resolution):
raise RuntimeError("Can't open video writer")
detections = frame_processor.process(frame)
presenter.drawGraphs(frame)
frame = draw_detections(frame, frame_processor, detections, output_transform)
metrics.update(start_time, frame)
frame_num += 1
if video_writer.isOpened() and (args.output_limit <= 0 or frame_num <= args.output_limit):
video_writer.write(frame)
if not args.no_show:
cv2.imshow('Face recognition demo', frame)
key = cv2.waitKey(1)
# Quit
if key in {ord('q'), ord('Q'), 27}:
break
presenter.handleKey(key)
metrics.log_total()
for rep in presenter.reportMeans():
log.info(rep)
if __name__ == '__main__':
sys.exit(main() or 0)