-
Notifications
You must be signed in to change notification settings - Fork 4
/
Camera.py
253 lines (210 loc) · 10.4 KB
/
Camera.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
import os
import sys
import cv2
import face_recognition
from TransferLearning import loadDictionary, loadLists, toList, getLivenessValue, runInParallel, dynamicAdd, \
getFolderSize, checkIfHere
from init import *
import numpy as np
from Excel import *
from LivenessDetection import getModel, getModelPred
import socket
# from Sheets import * # Uncomment to use online/offline mode
from timeit import default_timer as timer
# Global Variables
global dynamicState
global pauseState
global onlineMode
dynamicState = False
pauseState = True
onlineMode = False
# Dynamic Addition Variable
def addPerson():
global dynamicState
dynamicState = True
# Check Internet Function
def internetCheck():
try:
# connect to the host -- tells us if the host is actually
# reachable
socket.create_connection(("www.google.com", 80))
return True
except OSError:
pass
return False
# To make a video camera Object
class VideoCamera(object):
# initializations of a Videocamera
def __init__(self, source):
try:
# Video capture
self.video = cv2.VideoCapture(source)
# Some global variables
global processThisFrame, faceLocations, faceEncodings, faceNames, encodingList, encodingNames
global faceNamesKnown, fullStudentNames, inputFrames, model, start, internetCheck
# Initialize variables
faceLocations = []
faceEncodings = []
faceNames = []
inputFrames = []
processThisFrame = True
# Load List information
fullStudentNames = loadLists("List Information/Full Student Names") # List with full Student Names
faceNamesKnown = loadLists("List Information/Face Names Known") # List With Face Names
encodingNames = loadLists("List Information/Encoding Names") # List With encoding names
loadDictionary("List Information/Face Names Known", faceEncodingsKnown) # Dictionary with Encodings
encodingList = toList(faceEncodingsKnown)
# Load encodings
for x in range(0, int(len(encodingList))):
encodingList[x] = np.load("Encodings/" + str(encodingNames[x]))
# Load Liveness Model
model = getModelPred()
# Start Late timer
start = timer()
# Internet Check
internetCheck = internetCheck()
except Exception as e:
print(e)
def __del__(self):
# Delete Video Capture
self.video.release()
# Dynamic Addition After-Function
def additionProcess(self):
# Some global variables
global encodingList, encodingNames, faceEncodingsKnown
global faceNamesKnown, fullStudentNames
# Load Lists Again
fullStudentNames = loadLists(
"List Information/Full Student Names") # List with full Student Names
faceNamesKnown = loadLists("List Information/Face Names Known") # List With Face Names
encodingNames = loadLists("List Information/Encoding Names") # List With encoding names
loadDictionary("List Information/Face Names Known",
faceEncodingsKnown) # Dictionary with Encodings
# Run Encoding Model if necessary
if getFolderSize("Encodings/") != len(encodingNames):
import EncodingModel
# Reload Encodings
encodingList = toList(faceEncodingsKnown)
for x in range(0, int(len(encodingList))):
encodingList[x] = np.load("Encodings/" + str(encodingNames[x]))
# Dyanic Addition Core Function
def addFace(self):
# Some global variables
global dynamicState, encodingNames, fullStudentNames, faceNamesKnown, encodingList, frame
# Only run Dynamic Addition if a face is found and is unknown
if 'Unknown' in faceNames and len(faceLocations) > 1:
# Run dynamic core addition
dynamicAdd(frame)
# Relaod Lists
fullStudentNames = loadLists("List Information/Full Student Names") # List with full Student Names
faceNamesKnown = loadLists("List Information/Face Names Known") # List With Face Names
encodingNames = loadLists("List Information/Encoding Names") # List With encoding names
loadDictionary("List Information/Face Names Known", faceEncodingsKnown) # Dictionary with Encodings
# Run Encoding Model as necessary
if getFolderSize("Encodings/") != len(encodingNames):
import EncodingModel
# Reload Enecodings
encodingList = toList(faceEncodingsKnown)
for x in range(0, int(len(encodingList))):
encodingList[x] = np.load("Encodings/" + str(encodingNames[x]))
# Turn off dynamic addition once done
dynamicState = False
def getRawFrame(self):
# Returns the raw frame
_, frameToReturn = self.video.read()
return frameToReturn
def goOnline(self):
global onlineMode
onlineMode = not onlineMode
def getFrame(self):
try:
# Some global variables
global processThisFrame, faceLocations, faceNames, encodingList, faceNamesKnown, fullStudentNames
global model, inputFrames, frame, dynamicState, start, internetCheck, onlineMode
# Read OpenCV video
success, frame = self.video.read()
# Resize as necessary
smallFrame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Change Colors as necessary
rgbSmallFrame = smallFrame[:, :, ::-1]
# End time for Late feature
end = timer()
# Calculate time spent
elapsedTime = end - start
# Only process every other frame of video to save time
if processThisFrame:
# Find all the faces and face encodings in the current frame of video
faceLocations = face_recognition.face_locations(rgbSmallFrame)
faceEncodings = face_recognition.face_encodings(rgbSmallFrame, faceLocations)
# Empty Face names for every iteration
faceNames = []
# Calculate Blur; if its too blurry it won't do facial recognition
blurAmount = cv2.Laplacian(frame, cv2.CV_64F).var()
if blurAmount > 40:
for faceEncoding in faceEncodings:
# See if the face is a match for the known face(s)
matchesFound = face_recognition.compare_faces(encodingList, faceEncoding)
name = "Unknown"
# Or instead, use the known face with the smallest distance to the new face
faceDistances = face_recognition.face_distance(encodingList, faceEncoding)
matchIndex = np.argmin(faceDistances)
if matchesFound[matchIndex]:
name = faceNamesKnown[matchIndex]
# Add name to the faceNames array
faceNames.append(name)
# Process every other frame
processThisFrame = not processThisFrame
# Display the results
for (top, right, bottom, left), name in zip(faceLocations, faceNames):
# 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
# Recalculate blur
blurAmount = cv2.Laplacian(frame, cv2.CV_64F).var()
# Calculate liveness amount
livenessVal = getLivenessValue(frame, inputFrames, model)
# if liveness is over 95% then continue recognition
if livenessVal > 0.95:
# Blur must be over 40 in order to accurately recognize a face
if blurAmount > 40:
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Online/Offline Mode
if internetCheck and onlineMode:
for x in range(0, len(fullStudentNames)):
if name in fullStudentNames[x]:
# Check if they are late
if elapsedTime > 300:
updateLatePersonExcel(fullStudentNames[x])
# updateLatePerson() # Uncomment to use online/offline mode (GSheets)
else:
updatePresentPersonExcel(fullStudentNames[x])
# updatePresentPerson() # Uncomment to use online/offline mode (GSheets)
else:
for x in range(0, len(fullStudentNames)):
if name in fullStudentNames[x]:
# Check if they are late
if elapsedTime > 300:
updateLatePersonExcel(fullStudentNames[x])
else:
updatePresentPersonExcel(fullStudentNames[x])
for x in range(0, len(faceNamesKnown)):
checkIfHere(name, faceNamesKnown[x])
else:
# Do not mark anyone if its a spoof
cv2.putText(frame, "WARNING: SPOOF DETECTED", (100, 75), font, 1.0, (0, 0, 255), 2)
# Encode frame so it can be displayed on a webpage
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
except Exception as e:
# Enceptions to get file + line numbers errors are on
exceptionType, exceptionObject, exceptionThrowback = sys.exc_info()
fileName = os.path.split(exceptionThrowback.tb_frame.f_code.co_filename)[1]
print(exceptionType, fileName, exceptionThrowback.tb_lineno)
print(e)