-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomaticAttedance.py
359 lines (331 loc) · 13.4 KB
/
automaticAttedance.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
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
import smtplib
import tkinter as tk
from tkinter import *
import os, cv2
import shutil
import csv
import numpy as np
from PIL import ImageTk, Image
import pandas as pd
import datetime
import time
import tkinter.ttk as tkk
import tkinter.font as font
haarcasecade_path = "C:\\Users\\HP\\Dropbox\\PC\\Desktop\\Final-Project\\New folder\\Attendance-Management-system-using-face-recognition-master\\haarcascade_frontalface_default.xml"
trainimagelabel_path = (
"C:\\Users\\HP\\Dropbox\\PC\\Desktop\\Final-Project\\New folder\\Attendance-Management-system-using-face-recognition-master\\TrainingImageLabel\\Trainner.yml"
)
trainimage_path = "C:\\Users\\HP\\Dropbox\\PC\\Desktop\\Final-Project\\New folder\\Attendance-Management-system-using-face-recognition-master\\TrainingImage"
studentdetail_path = (
"C:\\Users\\HP\\Dropbox\\PC\\Desktop\\Final-Project\\New folder\\Attendance-Management-system-using-face-recognition-master\\StudentDetails\\studentdetails.csv"
)
attendance_path = "C:\\Users\\HP\\Dropbox\\PC\\Desktop\\Final-Project\\New folder\\Attendance-Management-system-using-face-recognition-master\\Attendance"
# for choose subject and fill attendance
def subjectChoose(text_to_speech):
def FillAttendance():
sub = selected_option.get()
now = time.time()
future = now + 20
print(now)
print(future)
if sub == "":
t = "Please enter the subject name!!!"
text_to_speech(t)
else:
try:
recognizer = cv2.face.LBPHFaceRecognizer_create()
try:
recognizer.read(trainimagelabel_path)
except:
e = "Model not found,please train model"
Notifica.configure(
text=e,
bg="black",
fg="yellow",
width=33,
font=("times", 15, "bold"),
)
Notifica.place(x=20, y=250)
text_to_speech(e)
facecasCade = cv2.CascadeClassifier(haarcasecade_path)
df = pd.read_csv(studentdetail_path)
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
col_names = ["Enrollment", "Name"]
attendance = pd.DataFrame(columns=col_names)
while True:
___, im = cam.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = facecasCade.detectMultiScale(gray, 1.2, 5)
for (x, y, w, h) in faces:
global Id
Id, conf = recognizer.predict(gray[y : y + h, x : x + w])
if conf < 70:
print(conf)
global Subject
global aa
global date
global timeStamp
Subject = selected_option.get()
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime(
"%Y-%m-%d"
)
timeStamp = datetime.datetime.fromtimestamp(ts).strftime(
"%H:%M:%S"
)
aa = df.loc[df["Enrollment"] == Id]["Name"].values
global tt
tt = str(Id) + "-" + aa
# En='1604501160'+str(Id)
attendance.loc[len(attendance)] = [
Id,
aa,
]
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 260, 0), 4)
cv2.putText(
im, str(tt), (x + h, y), font, 1, (255, 255, 0,), 4
)
else:
Id = "Unknown"
tt = str(Id)
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 25, 255), 7)
cv2.putText(
im, str(tt), (x + h, y), font, 1, (0, 25, 255), 4
)
if time.time() > future:
break
attendance = attendance.drop_duplicates(
["Enrollment"], keep="first"
)
cv2.imshow("Filling Attendance...", im)
key = cv2.waitKey(30) & 0xFF
if key == 27:
break
# ts = time.time()
# print(aa)
# # attendance["date"] = date
# # attendance["Attendance"] = "P"
# attendance[date] = 1
# date = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d")
# timeStamp = datetime.datetime.fromtimestamp(ts).strftime("%H:%M:%S")
# Hour, Minute, Second = timeStamp.split(":")
# # fileName = "Attendance/" + Subject + ".csv"
# path = os.path.join(attendance_path, Subject)
# fileName = (
# f"{path}/"
# + Subject
# + "_"
# + date
# + "_"
# + Hour
# + "-"
# + Minute
# + "-"
# + Second
# + ".csv"
# )
# attendance = attendance.drop_duplicates(["Enrollment"], keep="first")
# print(attendance)
# attendance.to_csv(fileName, index=False)
ts = time.time()
attendance = attendance.drop_duplicates(["Enrollment"], keep="first")
# Get student email from studentdetail.csv
email = df.loc[df["Enrollment"] == Id]["Email"].values[0]
attendance[date+"("+timeStamp+")"] = 1
date = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d")
timeStamp = datetime.datetime.fromtimestamp(ts).strftime("%H:%M:%S")
Hour, Minute, Second = timeStamp.split(":")
path = os.path.join(attendance_path, Subject)
fileName = (
f"{path}/"
+ Subject
+ "_"
+ date
+ "_"
+ Hour
+ "-"
+ Minute
+ "-"
+ Second
+ ".csv"
)
attendance = attendance.drop_duplicates(["Enrollment"], keep="first")
attendance.to_csv(fileName, index=False)
m = "Attendance Filled Successfully of " + Subject
Notifica.configure(
text=m,
bg="black",
fg="yellow",
width=33,
relief=RIDGE,
bd=5,
font=("times", 15, "bold"),
)
text_to_speech(m)
Notifica.place(x=20, y=250)
cam.release()
cv2.destroyAllWindows()
# Send email to the student
sender_email = "frbasadmi2023@gmail.com" # Replace with your email
receiver_email = email
password = "zxdswaqjibzhsqec" # Replace with your email password
# message = f"""\
# subject: Attendance Filled Successfully
# body = Your attendance for {sub} has been successfully filled on {date} at {timeStamp}."""
subject = "Attendance Filled Successfully"
# Replace with the body of the email
body = f"Dear Student,\n\nYour attendance for Subject {sub} has been successfully filled on {date} at {timeStamp}.\n\nThank you for your attention.\n\nSincerely,\nTeacher"
# Construct the message
message = f"Subject: {subject}\n\n{body}"
try:
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
print(f"Email sent to {receiver_email} successfully.")
except Exception as e:
print(f"Error: {e}")
print(f"Email to {receiver_email} could not be sent.")
# m = "Attendance Filled Successfully of " + Subject
# Notifica.configure(
# text=m,
# bg="black",
# fg="yellow",
# width=33,
# relief=RIDGE,
# bd=5,
# font=("times", 15, "bold"),
# )
# text_to_speech(m)
# Notifica.place(x=20, y=250)
# cam.release()
# cv2.destroyAllWindows()
import csv
import tkinter
root = tkinter.Tk()
root.title("Attendance of " + Subject)
root.configure(background="black")
cs = os.path.join(path, fileName)
print(cs)
with open(cs, newline="") as file:
reader = csv.reader(file)
r = 0
for col in reader:
c = 0
for row in col:
label = tkinter.Label(
root,
width=10,
height=1,
fg="yellow",
font=("times", 15, " bold "),
bg="black",
text=row,
relief=tkinter.RIDGE,
)
label.grid(row=r, column=c)
c += 1
r += 1
root.mainloop()
print(attendance)
except:
f = "No Face found for attendance"
text_to_speech(f)
cv2.destroyAllWindows()
###windo is frame for subject chooser
subject = Tk()
# windo.iconbitmap("AMS.ico")
subject.title("Subject...")
subject.geometry("580x320")
subject.resizable(0, 0)
subject.configure(background="#3a5978")
# subject_logo = Image.open("UI_Image/0004.png")
# subject_logo = subject_logo.resize((50, 47), Image.ANTIALIAS)
# subject_logo1 = ImageTk.PhotoImage(subject_logo)
titl = tk.Label(subject, bg="black", relief=RIDGE, bd=10, font=("arial", 30))
titl.pack(fill=X)
# l1 = tk.Label(subject, image=subject_logo1, bg="black",)
# l1.place(x=100, y=10)
titl = tk.Label(
subject,
text="Enter the Subject Name",
bg="black",
fg="yellow",
font=("arial", 25,"bold"),
)
titl.place(x=160, y=12)
Notifica = tk.Label(
subject,
text="Attendance filled Successfully",
bg="yellow",
fg="black",
width=33,
height=2,
font=("times", 15, "bold"),
)
def Attf():
sub = selected_option.get()
if sub == "":
t = "Please enter the subject name!!!"
text_to_speech(t)
else:
os.startfile(
f"C:\\Users\\HP\\Dropbox\\PC\\Desktop\\Final-Project\\New folder\\Attendance-Management-system-using-face-recognition-master\\Attendance\\{sub}"
)
attf = tk.Button(
subject,
text="Check Sheets",
command=Attf,
bd=7,
font=("times new roman", 15),
bg="black",
fg="yellow",
height=2,
width=10,
relief=RIDGE,
)
attf.place(x=360, y=170)
sub = tk.Label(
subject,
text="Enter Subject",
width=10,
height=2,
bg="black",
fg="yellow",
bd=5,
relief=RIDGE,
font=("times new roman", 15),
)
sub.place(x=50, y=100)
options = ["Select Subject","Javascript", "CCS", "CNS", "Linux","ETCMIT","GUI.NET"]
selected_option = tk.StringVar(subject)
selected_option.set(options[0])
tx = tk.OptionMenu(
subject,
selected_option,
*options,
)
tx.config(
width=15,
bd=5,
bg="#3a5978",
fg="yellow",
relief=tk.RIDGE,
font=("times", 30, "bold"),
)
tx.place(x=190, y=100)
fill_a = tk.Button(
subject,
text="Fill Attendance",
command=FillAttendance,
bd=7,
font=("times new roman", 15),
bg="black",
fg="yellow",
height=2,
width=12,
relief=RIDGE,
)
fill_a.place(x=195, y=170)
subject.mainloop()