-
Notifications
You must be signed in to change notification settings - Fork 1
/
FaceRecogniser.py
160 lines (128 loc) · 6.48 KB
/
FaceRecogniser.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
# MIT License
# Copyright (c) 2017 Luca Angioloni and Francesco Pegoraro
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import face_recognition
import cv2
import numpy as np
from PyQt5.QtCore import (Qt, QObject, pyqtSignal, QThread)
from FaceDatabase import FaceDatabase
class FaceRecogniser(QThread):
"""
Thread responsible for background capture and analysis of images from the camera.
It performs face detection, embedding extraction and queries the Face Database for recognition.
Attributes:
updated Qt Signal emitted every time a new image is elaborated.
person_identified Qt Signal emitted every time a person is identified.
database Instance of FaceDatabase.
currentFrame Numpy array containing the last elaborated frame.
active Boolean status of the recognition progress
currentUser Temporary attribute to store the last recognised user.
"""
updated = pyqtSignal() # in order to work it has to be defined out of the contructor
person_identified = pyqtSignal() # in order to work it has to be defined out of the contructor
def __init__(self):
super().__init__()
self.database = FaceDatabase()
self.database.retrieve()
self.currentFrame = None
self.active = False
self.currentUser = None
def get_current_frame(self):
"""Getter for the currentFrame attribute"""
return self.currentFrame
def deactivate(self):
"""Method called to stop and deactivate the face recognition Thread"""
self.active = False
if self.isRunning():
self.terminate()
def loop(self):
"""Method called to initialize and start the face recognition Thread"""
self.currentUser = None
self.start()
def get_single_face(self, face_locations):
"""
Method that accepts multiple face locations and returns the location of the biggest surface location.
Args:
face_locations List containing detected faces locations (each of them is a list containig the top, right, bottom, left values)
"""
selected = 0
max_area = 0
for i, face in enumerate(face_locations):
top, right, bottom, left = face
area = abs(right-left) * abs(bottom-top)
if area > max_area:
max_area = area
selected = i
return [face_locations[selected]]
def run(self):
"""Main loop of this Thread"""
self.active = True
video_capture = cv2.VideoCapture(0)
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
count = 0
last_id = ""
while self.active:
# Grab a single frame of video
ret, frame = video_capture.read()
if ret:
frame = cv2.flip(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB),1)
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(small_frame)
if face_locations:
face_locations = self.get_single_face(face_locations)
else:
count = 0
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
id = self.database.get_identity(face_encoding)
name = self.database.get_nickname(id)
face_names.append(name)
if id != "Unknown" and id == last_id:
count = count + 1
else:
last_id = id
count = 0
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (255, 0, 0), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (255, 0, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Store the current image
self.currentFrame = frame
if count > 10: # A user has been recognised, activation of acceptance graphical effect (green borders)
self.currentFrame = cv2.copyMakeBorder(frame, top=20, bottom=20, left=20, right=20, borderType= cv2.BORDER_CONSTANT, value=[0,220,0] )
if count > 12: # Final recognition of a user and send the person_identified signal
self.active = False
video_capture.release()
self.currentUser = last_id
self.person_identified.emit()
self.updated.emit()