-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecognitionClass.py
More file actions
363 lines (283 loc) · 12.7 KB
/
Copy pathRecognitionClass.py
File metadata and controls
363 lines (283 loc) · 12.7 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import json
from tkinter import messagebox
import cv2
import numpy as np
import face_recognition
import os
import time
from collections import Counter
import copy
import tkinter as tk
from tkinter import *
#from utilities import start_sound
from playsound import playsound
import threading
import time
from tkinter import *
import os
from PIL import Image, ImageTk
import pygame
def play(sound_, vol):
sound = pygame.mixer.Sound(sound_)
sound.set_volume(vol)
sound.play()
def start_sound(sound, vol=1):
x = threading.Thread(target=play, args=(sound,vol))
x.start()
class FaceRecognition:
def __init__(self):
self.class_names = []
self.usersPath = 'users.json'
self.path = 'ImagesAttendance'
self.getClassesImages()
self.encodeListKnown = self.getEncodings()
self.users = self.getUsersList()
def getClassesImages(self):
imgs = []
classes = []
file_list = os.listdir(self.path)
file_list.sort()
print("Class Detected: ", file_list)
for cl in file_list:
cur_img = cv2.imread(f'{self.path}/{cl}')
imgs.append(cur_img)
classes.append(os.path.splitext(cl)[0])
self.class_names = classes
print("Num classes ", len(self.class_names))
return imgs
def findEncodings(self, images):
encodeList = []
for index, img in enumerate(images):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = face_recognition.face_encodings(img)
if result:
encode = result[0]
encodeList.append(encode.tolist())
else:
os.remove('ImagesAttendance/' + self.class_names[index] + '.png')
print(self.class_names[index])
self.class_names.pop(index)
print("Encodings completed")
return encodeList
def saveEncodings(self, encodeList):
with open('encodings.json', 'w') as outfile:
json.dump(encodeList, outfile)
print("Encoding saved")
def getEncodings(self):
with open('encodings.json', 'r+') as f:
encodeList = json.load(f)
print("Num encodings ", len(encodeList))
return encodeList
def recognition(self):
if len(self.encodeListKnown) == 0:
cap = cv2.VideoCapture(-1)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
t_end = time.time() + 5
while time.time() < t_end:
success, img = cap.read()
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
if facesCurFrame:
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
name = "Unknown"
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1)
else:
cv2.putText(img, "Stay in front the camera", (55, 240), cv2.FONT_HERSHEY_DUPLEX, 1.3, (0, 0, 255),2)
cv2.imshow('Webcam', img)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
return self.getUserFromUsername('Unknown')
else:
cap = cv2.VideoCapture(-1)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
t_end = time.time() + 5
prediction_list = []
while time.time() < t_end:
success, img = cap.read()
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
if facesCurFrame:
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
matches = face_recognition.compare_faces(self.encodeListKnown, encodeFace)
faceDis = face_recognition.face_distance(self.encodeListKnown, encodeFace)
matchIndex = np.argmin(faceDis)
if faceDis[matchIndex] < 0.5:
userIndex = self.class_names[matchIndex].split(".")[1]
name = self.users[int(userIndex)]['username']
else:
name = "Unknown"
prediction_list.append(name)
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1)
else:
cv2.putText(img, "Stay in front the camera", (55, 260), cv2.FONT_HERSHEY_COMPLEX , 1.3, (0, 0, 255), 2)
cv2.imshow('Webcam', img)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
c = Counter(prediction_list)
if prediction_list:
username = c.most_common(1)[0][0]
else:
username = "None"
return self.getUserFromUsername(username)
def addNewUser(self):
self.num_pic = 0
# Define the detector
detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# get the last id
last_id = self.users[-1]['id']
username, dominant_hand = self.signIn()
self.saveNewUser(last_id + 1, username, dominant_hand)
cam = cv2.VideoCapture(-1)
img_counter = 0
images = []
count = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(30, 30)
)
pic = copy.copy(frame)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 1)
cv2.putText(frame, "Press Space to take pictures " + str(self.num_pic) + "/6", (30, 30), cv2.FONT_HERSHEY_COMPLEX, 1.0, (0, 0, 255),
2)
cv2.imshow("Registration", frame)
k = cv2.waitKey(1)
if self.num_pic == 6:
break
if k % 256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k % 256 == 32:
# SPACE pressed
count += 1
self.num_pic +=1
img_name = 'ImagesAttendance/User.' + str(last_id + 1) + '.' + str(count) + ".png"
cv2.imwrite(img_name, pic)
images.append(pic)
print("{} written!".format(img_name))
pygame.init()
pygame.mixer.init()
start_sound("screenshot.mp3", 1)
img_counter += 1
cam.release()
cv2.destroyAllWindows()
# Update classes list with the new pics saved
self.getClassesImages()
for x in self.findEncodings(images):
self.encodeListKnown.append(x)
self.saveEncodings(self.encodeListKnown)
return {'id': int(last_id + 1), 'username': username, 'dominant': dominant_hand, 'tabs': []}
def saveNewUser(self, face_id, username, dominant_hand):
self.users.append({'id': int(face_id), 'username': username, 'dominant': dominant_hand, 'tabs': []})
with open(self.usersPath, 'w') as f:
json.dump(self.users, f, indent=4, separators=(',', ': '))
def getUsersList(self):
with open(self.usersPath, 'r+') as f:
users = json.load(f)
return users
def getUserFromUsername(self, username):
for user in self.users:
if user["username"] == username:
return user
def signIn(self):
root = Tk()
root.title("Registration Form")
root.resizable(False, False) # This code helps to disable windows from resizing
root.attributes('-topmost', 'true')
window_height = 420
window_width = 640
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (window_width / 2))
y_cordinate = int((screen_height / 2) - (window_height / 2))
root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
username = StringVar()
dominant_hand = StringVar()
def check():
if str(entry_1.get()) != '' and str(entry_2.get()) == "Right" or str(entry_2.get()) == "Left":
root.destroy()
else:
messagebox.showerror("Error", "Please enter the data in the correct format (Left, Right)")
label_0 = Label(root, text="Registration form", font=("bold", 20))
label_0.pack(side=TOP, pady=10)
label_1 = Label(root, text="Username", font=("bold", 12))
label_1.pack(side=TOP, ipady=10)
entry_1 = Entry(root, textvariable=username)
entry_1.pack(side=TOP, ipady=5, pady=5)
label_2 = Label(root, text="Dominant hand (Left, Right)", font=("bold", 12))
label_2.pack(side=TOP, ipady=10)
entry_2 = Entry(root, textvariable=dominant_hand)
entry_2.pack(side=TOP, ipady=5, pady=5)
Button(root, text='Submit', width=20, bg='brown', fg='white', command=check).pack(side=TOP, pady=10)
root.mainloop()
return str(username.get()), str(dominant_hand.get())
def showErrorNoFaceDetected(self):
Tk().withdraw()
messagebox.showerror("Error", "Please stay in front the camera. Press ok and wait a few seconds and try again")
return
def showInfoNewAttempt(self):
Tk().withdraw()
messagebox.showinfo("New Attempt", "Press ok and wait a few seconds and the camera will start again")
return
def askRegistration(self):
root = Tk()
root.title("Registration request")
root.attributes('-topmost', 'true')
var = IntVar()
def sel():
print(var.get())
if str(var.get()) in ["1", "2", "3"]:
root.destroy()
else:
messagebox.showerror("Error", "Please select an option")
window_height = 420
window_width = 640
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (window_width / 2))
y_cordinate = int((screen_height / 2) - (window_height / 2))
root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
label_0 = Label(root,
text="Seems it is the first time you use this application \n "
"do you want to register?",
font=("bold", 15))
label_0.pack(side=TOP, ipady=10)
# Dictionary to create multiple buttons
values = {"YES ": "1",
"NO ": "2",
"RETRY": "3"}
# Loop is used to create multiple Radiobuttons
# rather than creating each button separately
for (text, value) in values.items():
Radiobutton(root, text=text, variable=var,
value=value).pack(side=TOP, ipady=5)
Button(root, text='Submit', width=20, bg='brown', fg='white', command=sel).pack(side=TOP, pady=10)
label = Label(root)
label.pack()
root.mainloop()
return var.get()