-
Notifications
You must be signed in to change notification settings - Fork 6
/
Face Recognition.py
321 lines (250 loc) · 11.3 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
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
import face_recognition
import numpy as np
from datetime import datetime
import os
import cv2
import keyboard
import pyautogui
import customtkinter as cstk
import tkinter as tk
from tkinter import filedialog, PhotoImage
path = "ImagesAttendance"
only_name = r"only_name"
attend_csv_path = r"Attendance.csv"
# GUIIIII
cstk.set_appearance_mode("dark")
cstk.set_default_color_theme("green")
root = cstk.CTk()
root.geometry("1920x1080")
root.title("Facial Recognition System")
# create the needed variables
images = [] # img to numpy array
global image_names
global filesz
global encodeList
encodeList=[]
filesz=tuple()
image_names = [] # stores people's namesz
mylist = os.listdir(path) # lists all the images in dir
savedImg = []
global attend_dict
attend_dict={}
print(mylist)
global del_names,del_ind
del_names=[]
del_ind=[]
# accessing images in folder
def access():
global images,image_names
for cl in mylist:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
image_names.append(os.path.splitext(cl)[0]) #root path of name [0] ext path [1]
print(image_names)
image_names2 = image_names[:]
def clean():
for f in os.listdir(only_name):
os.remove(fr"{only_name}\{f}")
# return the 128-dimension face encoding for each face in the image.
# A face encoding is basically a way to represent the face using a set of 128 computer-generated measurements.
# Two different pictures of the same person would have similar encoding and
# two different people would have totally different encoding
def find_encodings(images):
for image in images:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(image)[0]
encodeList.append(encode)
return encodeList
# to save the captured image
def save_img(imagesz,nami):
savedImg=os.listdir(only_name)
if nami not in savedImg:
cv2.imwrite(rf"{only_name}+\{nami}.jpg", imagesz)
# to mark the attendace into txt file for a new name
def markAttendance(name):
print(name, "attended")
with open("Attendance.csv", 'r+') as f:
myDataList = f.readlines() # reads every line in attendance list
for line in myDataList:
line = line.strip()
entry = line.split(',')
attend_dict[entry[0]] = entry[1:]
if name not in attend_dict.keys():
now = datetime.now()
dtString = now.strftime("%I:%M %p") # I - 12 hr format() , minute , pm or am
attend_dict[name] = [dtString,""] # writes time
elif name in attend_dict.keys():
now = datetime.now()
dtString = now.strftime("%I:%M %p") # I - 12 hr format() , minute , pm or am
attend_dict[name][1]=dtString
def webcam_scan():
cap = cv2.VideoCapture(0) # starts video capture through webcam
while True:
# img = numpy array , succces= if loaded or not
success,img = cap.read()
# we resizze to 1/4th of size of ease of calculation and faster read time
imgS = cv2.resize(img,(0,0),None,0.25,0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
# no of faces in an frame
facesCurFrame = face_recognition.face_locations(imgS)
encodesCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)
# displays a text below no co ordi where tot font colour size
cv2.putText(img,f'Number of faces detected: {len(facesCurFrame)}', (100, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 255), 2)
# main shittt
for encodeFace,FaceLoc in zip(encodesCurFrame,facesCurFrame):
matches = face_recognition.compare_faces(encodeListKnown,encodeFace,tolerance=0.5) # lower is more strict
faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
matchIndex = np.argmin(faceDis) # gives matchIndex of match name out of all images
if matches[matchIndex]:
name = image_names[matchIndex].upper() # Capitalizes each word
# print(name)
# FaceLoc = up right down left
y1,x2,y2,x1=FaceLoc
# multiply by 4 cuz we decresed the size by 4
# were drwaing on regular image not of reduced size one
y1, x2, y2, x1=y1*4,x2*4,y2*4,x1*4
# draw's rectangle img , loc , colour , size
cv2.rectangle(img, (x1,y1),(x2,y2) ,(255, 255, 0), 2)
cv2.rectangle(img, (x1, y2-35), (x2, y2),(255, 255, 0), cv2.FILLED)
# displays name
cv2.putText(img,name,(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,255),2)
# save img
save_img(img, name)
# call's attendace to add name
markAttendance(name)
else:
name = "UNKNOWN"
# FaceLoc = up right down left
y1, x2, y2, x1 = FaceLoc
# multiply by 4 cuz we decresed the size by 4
# were drwaing on regular image not of reduced size one
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
# draw's rectangle img , loc , colour , size
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 0), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (255, 255, 0), cv2.FILLED)
# displays name
cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 255), 2)
# continouly displays the image
cv2.imshow('webcam',img)
cv2.waitKey(1)
if keyboard.is_pressed('q'):
print("i quit!!")
cv2.destroyWindow('webcam')
break
# mark attendance
def attendance():
ff = open("Attendance.csv", 'w+')
ss = ""
try:
ff.writelines("NAME,ENTRY,EXIT,TIME_SPENT_IN_MIN")
ff.writelines("\n")
del attend_dict['NAME']
del attend_dict['UNKNOWN']
except KeyError:
print()
for i in (attend_dict.keys()):
ss += i
entryy=attend_dict[i][0]
exitt=attend_dict[i][1]
try:
ts=(int(exitt[3:-3]) - int(entryy[3:-3])) + (60*(int(exitt[:2]) - int(entryy[:2]) ))
ss += "," + entryy + "," + exitt + "," + str(ts)
ff.writelines(ss)
ff.writelines("\n")
except ValueError:
print()
ss = ""
ff.close()
os.startfile(r"Attendance.csv")
# take a new pic from webcam
def take_a_pic():
new_name = pyautogui.prompt('What is your name?',title="Name",default="new_image")
if new_name in del_names:
loc=del_ind[del_names.index(new_name)]
image_names[loc]=new_name
new_name += ".jpg"
tk.messagebox.showinfo("Alert", "Look at the Camera in 3 sec !")
result, new_img = cv2.VideoCapture(0).read()
cv2.imwrite(rf"ImagesAttendance\{new_name}", new_img)
cv2.imshow("New Image", new_img)
cv2.waitKey(0)
cv2.destroyWindow('New Image')
else:
new_name+= ".jpg"
tk.messagebox.showinfo("Alert", "Look at the Camera in 3 sec !")
result, new_img = cv2.VideoCapture(0).read()
cv2.imwrite(rf"ImagesAttendance\{new_name}",new_img)
cv2.imshow("New Image",new_img)
cv2.waitKey(0)
cv2.destroyWindow('New Image')
images.append(cv2.imread(fr'ImagesAttendance\{new_name}'))
image_names.append(os.path.splitext(new_name)[0])
print(os.path.splitext(new_name)[0])
encodeList.append(face_recognition.face_encodings(images[-1])[0])
def open_images_to_delete():
L1 = image_names
L2 = []
li2 = os.listdir(r"ImagesAttendance")
filesz = filedialog.askopenfilenames(title = "Select image files", filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print("Selected files:", filesz)
for xx in filesz:
os.remove(xx)
xx = os.path.splitext(xx[xx.find('nce') + 4:])[0]
#set_dif.append(os.path.splitext(xx)[0])
del_ind.append(L1.index(xx))
del_names.append(image_names[L1.index(xx)])
image_names[L1.index(xx)] = "unknown"
print("removed : ", xx)
set_dif = []
for x in li2:
L2.append(os.path.splitext(x)[0])
set_dif = list(set(L1).symmetric_difference(set(L2)))
set_dif = list(filter(lambda t: t != "unknown", set_dif))
removed_names = ""
for j in set_dif:
removed_names += j + " , "
tk.messagebox.showinfo("showinfo", f"Faces removed = {len(set_dif)}\n{removed_names}\nClose the Window")
def delete_a_face():
root1 = tk.Toplevel()
root1.geometry("310x220")
root1.title("delete")
image2 = PhotoImage(file=r"C:\Users\kmusa\PycharmProjects\Face_Recognition-main\other_files\delete.png")
bg1label = tk.Label(root1, image=image2, width=300, height=180)
bg1label.pack()
button9 = tk.Button(root1, text="Select the images", command=open_images_to_delete, width=300,pady=5)
button9.pack()
root1.mainloop()
def show():
os.startfile(r"only_name")
def know_faces():
os.startfile(r"ImagesAttendance")
def about():
os.startfile(r"C:\Users\kmusa\PycharmProjects\Face_Recognition-main\other_files\ABOUT OURSELVES.png")
#############----Mainn-------#############
clean() # empty the known images folder
access() # get the names of images
encodeListKnown = find_encodings(images) # encode all the images
print("Encoding Completed..")
# GUIIIII
imag = tk.PhotoImage(file=r"C:\Users\kmusa\PycharmProjects\Face_Recognition-main\other_files\bg4.png")
frame = cstk.CTkFrame(master=root)
frame.pack(padx=60,pady=20,fill="both",expand=True)
label = cstk.CTkLabel(master=frame,text="Facial Recognition System",font=("Roboto",24),compound="left")
label.pack(pady=12,padx=10)
bglabel = cstk.CTkLabel(master=frame,image=imag,text="", width=1080,height=1080)
bglabel.pack()
button1 = cstk.CTkButton(master=frame,text="Scan face (Webcam)",command=webcam_scan,height=80,width=250,font=("Arial",24))
button1.place(relx=0.3,rely=0.3,anchor="e")
button4 = cstk.CTkButton(master=frame,text="Known Images",command=know_faces,height=80,width=250,font=("Arial",24))
button4.place(relx=0.75,rely=0.3,anchor="w")
button5 = cstk.CTkButton(master=frame,text="Add a new face",command=take_a_pic,height=80,width=250,font=("Arial",24))
button5.place(relx=0.3,rely=0.57,anchor="e")
button6 = cstk.CTkButton(master=frame,text="Delete a face",command=delete_a_face,height=80,width=250,font=("Arial",24))
button6.place(relx=0.75,rely=0.562,anchor="w")
button3 = cstk.CTkButton(master=frame,text="About",command=about,height=80,width=250,font=("Arial",24))
button3.place(relx=0.3,rely=0.85,anchor="e")
button2 = cstk.CTkButton(master=frame,text="Show Scanned Images",command=show,height=80,width=250,font=("Arial",24))
button2.place(relx=0.75,rely=0.85,anchor="w")
button7 = cstk.CTkButton(master=frame,text="Open Attendance",command=attendance,height=80,width=250,font=("Arial",24))
button7.place(relx=0.52,rely=0.5,anchor="center")
root.mainloop()