-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattendance.py
441 lines (346 loc) · 18.8 KB
/
attendance.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# import re
import re
from sys import path
from tkinter import*
from tkinter import ttk
from PIL import Image,ImageTk
import os
import mysql.connector
import cv2
import numpy as np
from tkinter import messagebox
from time import strftime
from datetime import datetime
import csv
from tkinter import filedialog
#Global variable for importCsv Function
mydata=[]
class Attendance:
def __init__(self,root):
self.root=root
self.root.state('zoomed')
self.root.title("Hệ thống quản lý điểm danh sử dụng nhận dạng khuôn mặt")
#-----------Variables-------------------
self.var_id=StringVar()
self.var_roll=StringVar()
self.var_name=StringVar()
self.var_dep=StringVar()
self.var_time=StringVar()
self.var_date=StringVar()
self.var_attend=StringVar()
# This part is image labels setting start
# first header image
img=Image.open(r"D:\VKU\DoAnCoSo4\Attendance_Management_System_Using_Face_Recognition\Images_GUI\banner2.png")
img=img.resize((1280,130),Image.ANTIALIAS)
self.photoimg=ImageTk.PhotoImage(img)
# set image as lable
f_lb1 = Label(self.root,image=self.photoimg)
f_lb1.place(x=0,y=0,width=1280,height=130)
# backgorund image
bg1=Image.open(r"D:\VKU\DoAnCoSo4\Attendance_Management_System_Using_Face_Recognition\Images_GUI\bg4.jpg")
bg1=bg1.resize((1280,500),Image.ANTIALIAS)
self.photobg1=ImageTk.PhotoImage(bg1)
# set image as lable
bg_img = Label(self.root,image=self.photobg1)
bg_img.place(x=0,y=145,width=1280,height=500)
#title section
title_lb1 = Label(bg_img,text="VKU - Quản lí điểm danh",font=("verdana",20,"bold"),bg="navyblue",fg="white")
title_lb1.place(x=0,y=0,width=1280,height=40)
#========================Section Creating==================================
# Creating Frame
main_frame = Frame(bg_img,bd=2,bg="white") #bd mean border
main_frame.place(x=0,y=40,width=1300,height=480)
# Left Label Frame
left_frame = LabelFrame(main_frame,bd=2,bg="white",relief=RIDGE,text="Sinh Viên",font=("verdana",12,"bold"),fg="navyblue")
left_frame.place(x=10,y=10,width=660,height=480)
# ==================================Text boxes and Combo Boxes====================
#Student id
studentId_label = Label(left_frame,text="ID Sinh Viên:",font=("verdana",12,"bold"),fg="navyblue",bg="white")
studentId_label.grid(row=0,column=0,padx=5,pady=5,sticky=W)
studentId_entry = ttk.Entry(left_frame,textvariable=self.var_id,width=15,font=("verdana",12,"bold"))
studentId_entry.grid(row=0,column=1,padx=5,pady=5,sticky=W)
#Student Roll
student_roll_label = Label(left_frame,text="Mã Sinh Viên:",font=("verdana",12,"bold"),fg="navyblue",bg="white")
student_roll_label.grid(row=0,column=2,padx=5,pady=5,sticky=W)
student_roll_entry = ttk.Entry(left_frame,textvariable=self.var_roll,width=15,font=("verdana",12,"bold"))
student_roll_entry.grid(row=0,column=3,padx=5,pady=5,sticky=W)
#Studnet Name
student_name_label = Label(left_frame,text="Tên Sinh Viên:",font=("verdana",12,"bold"),fg="navyblue",bg="white")
student_name_label.grid(row=1,column=0,padx=5,pady=5,sticky=W)
student_name_entry = ttk.Entry(left_frame,textvariable=self.var_name,width=15,font=("verdana",12,"bold"))
student_name_entry.grid(row=1,column=1,padx=5,pady=5,sticky=W)
#Department
# dep_label = Label(left_frame,text="Department:",font=("verdana",12,"bold"),fg="navyblue",bg="white")
# dep_label.grid(row=1,column=2,padx=5,pady=5,sticky=W)
# dep_entry = ttk.Entry(left_frame,textvariable=self.var_dep,width=15,font=("verdana",12,"bold"))
# dep_entry.grid(row=1,column=3,padx=5,pady=5,sticky=W)
#time
time_label = Label(left_frame,text="Giờ:",font=("verdana",12,"bold"),fg="navyblue",bg="white")
time_label.grid(row=1,column=2,padx=5,pady=5,sticky=W)
time_entry = ttk.Entry(left_frame,textvariable=self.var_time,width=15,font=("verdana",12,"bold"))
time_entry.grid(row=1,column=3,padx=5,pady=5,sticky=W)
#Date
date_label = Label(left_frame,text="Ngày:",font=("verdana",12,"bold"),fg="navyblue",bg="white")
date_label.grid(row=2,column=0,padx=5,pady=5,sticky=W)
date_entry = ttk.Entry(left_frame,textvariable=self.var_date,width=15,font=("verdana",12,"bold"))
date_entry.grid(row=2,column=1,padx=5,pady=5,sticky=W)
#Attendance
student_attend_label = Label(left_frame,text="Trạng thái:",font=("verdana",12,"bold"),fg="navyblue",bg="white")
student_attend_label.grid(row=2,column=2,padx=5,pady=5,sticky=W)
attend_combo=ttk.Combobox(left_frame,textvariable=self.var_attend,width=13,font=("verdana",12,"bold"),state="readonly")
attend_combo["values"]=("Có mặt","Vắng mặt")
attend_combo.current(0)
attend_combo.grid(row=2,column=3,padx=5,pady=5,sticky=W)
# ===============================Table Sql Data View==========================
table_frame = Frame(left_frame,bd=2,bg="white",relief=RIDGE)
table_frame.place(x=10,y=100,width=635,height=280)
#scroll bar
scroll_x = ttk.Scrollbar(table_frame,orient=HORIZONTAL)
scroll_y = ttk.Scrollbar(table_frame,orient=VERTICAL)
#create table
self.attendanceReport_left = ttk.Treeview(table_frame,column=("ID","Roll_No","Name","Time","Date","Attend"),xscrollcommand=scroll_x.set,yscrollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM,fill=X)
scroll_y.pack(side=RIGHT,fill=Y)
scroll_x.config(command=self.attendanceReport_left.xview)
scroll_y.config(command=self.attendanceReport_left.yview)
self.attendanceReport_left.heading("ID",text="ID")
self.attendanceReport_left.heading("Roll_No",text="Mã Sinh Viên")
self.attendanceReport_left.heading("Name",text="Tên")
self.attendanceReport_left.heading("Time",text="Giờ")
self.attendanceReport_left.heading("Date",text="Ngày")
self.attendanceReport_left.heading("Attend",text="Trạng thái")
self.attendanceReport_left["show"]="headings"
# Set Width of Colums
self.attendanceReport_left.column("ID",width=100)
self.attendanceReport_left.column("Roll_No",width=100)
self.attendanceReport_left.column("Name",width=100)
self.attendanceReport_left.column("Time",width=100)
self.attendanceReport_left.column("Date",width=100)
self.attendanceReport_left.column("Attend",width=100)
self.attendanceReport_left.pack(fill=BOTH,expand=1)
self.attendanceReport_left.bind("<ButtonRelease>",self.get_cursor_left)
# =========================button section========================
#Button Frame
btn_frame = Frame(left_frame,bg="white",relief=RIDGE)
btn_frame.place(x=10,y=375,width=635,height=40)
#Improt button
save_btn=Button(btn_frame,command=self.importCsv,text="Nhập CSV",width=12,font=("verdana",12,"bold"),fg="white",bg="navyblue")
save_btn.grid(row=0,column=0,padx=6,pady=10,sticky=W)
#Exprot button
update_btn=Button(btn_frame,command=self.exportCsv,text="Xuất CSV",width=12,font=("verdana",12,"bold"),fg="white",bg="navyblue")
update_btn.grid(row=0,column=1,padx=6,pady=8,sticky=W)
#Update button
del_btn=Button(btn_frame,command=self.action,text="Sửa",width=12,font=("verdana",12,"bold"),fg="white",bg="navyblue")
del_btn.grid(row=0,column=2,padx=6,pady=10,sticky=W)
#reset button
reset_btn=Button(btn_frame,command=self.reset_data,text="Reset",width=12,font=("verdana",12,"bold"),fg="white",bg="navyblue")
reset_btn.grid(row=0,column=3,padx=6,pady=10,sticky=W)
# Right section=======================================================
# Right Label Frame
right_frame = LabelFrame(main_frame,bd=2,bg="white",relief=RIDGE,text="Chi tiết",font=("verdana",12,"bold"),fg="navyblue")
right_frame.place(x=680,y=10,width=660,height=480)
# -----------------------------Table Frame-------------------------------------------------
#Table Frame
#Searching System in Right Label Frame
table_frame = Frame(right_frame,bd=2,bg="white",relief=RIDGE)
table_frame.place(x=10,y=70,width=580,height=360)
#scroll bar
scroll_x = ttk.Scrollbar(table_frame,orient=HORIZONTAL)
scroll_y = ttk.Scrollbar(table_frame,orient=VERTICAL)
#create table
self.attendanceReport = ttk.Treeview(table_frame,column=("ID","Roll_No","Name","Time","Date","Attend"),xscrollcommand=scroll_x.set,yscrollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM,fill=X)
scroll_y.pack(side=RIGHT,fill=Y)
scroll_x.config(command=self.attendanceReport.xview)
scroll_y.config(command=self.attendanceReport.yview)
self.attendanceReport.heading("ID",text="ID")
self.attendanceReport.heading("Roll_No",text="Mã Sinh Viên")
self.attendanceReport.heading("Name",text="Tên")
self.attendanceReport.heading("Time",text="Giờ")
self.attendanceReport.heading("Date",text="Ngày")
self.attendanceReport.heading("Attend",text="Trạng thái")
self.attendanceReport["show"]="headings"
# Set Width of Colums
self.attendanceReport.column("ID",width=30)
self.attendanceReport.column("Roll_No",width=70)
self.attendanceReport.column("Name",width=100)
self.attendanceReport.column("Time",width=75)
self.attendanceReport.column("Date",width=80)
self.attendanceReport.column("Attend",width=120)
self.attendanceReport.pack(fill=BOTH,expand=1)
self.attendanceReport.bind("<ButtonRelease>",self.get_cursor_right)
self.fetch_data()
# =================================update for mysql button================
#Update button
del_btn=Button(right_frame,command=self.update_data,text="Sửa",width=12,font=("verdana",12,"bold"),fg="white",bg="navyblue")
del_btn.grid(row=0,column=1,padx=6,pady=10,sticky=W)
#Update button
del_btn=Button(right_frame,command=self.delete_data,text="Xóa",width=12,font=("verdana",12,"bold"),fg="white",bg="navyblue")
del_btn.grid(row=0,column=2,padx=6,pady=10,sticky=W)
# ===============================update function for mysql database=================
def update_data(self):
if self.var_id.get()=="" or self.var_roll.get=="" or self.var_name.get()=="" or self.var_time.get()=="" or self.var_date.get()=="" or self.var_attend.get()=="Status":
messagebox.showerror("Error","Vui lòng điền vào tất cả các trường được yêu cầu!",parent=self.root)
else:
try:
Update=messagebox.askyesno("Update","Bạn có muốn cập nhật điểm chuyên cần của sinh viên này không!",parent=self.root)
if Update > 0:
conn = mysql.connector.connect(user='root', password='2912002',host='localhost',database='face_recognition',port=3306)
mycursor = conn.cursor()
mycursor.execute("update stdattendance set std_id=%s,std_roll_no=%s,std_name=%s,std_time=%s,std_date=%s,std_attendance=%s where std_id=%s",(
self.var_id.get(),
self.var_roll.get(),
self.var_name.get(),
self.var_time.get(),
self.var_date.get(),
self.var_attend.get(),
self.var_id.get()
))
else:
if not Update:
return
messagebox.showinfo("Success","Cập nhật thành công!",parent=self.root)
conn.commit()
self.fetch_data()
conn.close()
except Exception as es:
messagebox.showerror("Error",f"Due to: {str(es)}",parent=self.root)
# =============================Delete Attendance form my sql============================
def delete_data(self):
if self.var_id.get()=="":
messagebox.showerror("Error","Mã số sinh viên phải được yêu cầu!",parent=self.root)
else:
try:
delete=messagebox.askyesno("Delete","Bạn có muốn xóa không?",parent=self.root)
if delete>0:
conn = mysql.connector.connect(user='root', password='2912002',host='localhost',database='face_recognition',port=3306)
mycursor = conn.cursor()
sql="delete from stdattendance where std_id=%s"
val=(self.var_id.get(),)
mycursor.execute(sql,val)
else:
if not delete:
return
conn.commit()
self.fetch_data()
conn.close()
messagebox.showinfo("Delete","Xóa thành công!",parent=self.root)
except Exception as es:
messagebox.showerror("Error",f"Due to: {str(es)}",parent=self.root)
# ===========================fatch data form mysql attendance===========
def fetch_data(self):
conn = mysql.connector.connect(user='root', password='2912002',host='localhost',database='face_recognition',port=3306)
mycursor = conn.cursor()
mycursor.execute("select * from stdattendance")
data=mycursor.fetchall()
if len(data)!= 0:
self.attendanceReport.delete(*self.attendanceReport.get_children())
for i in data:
self.attendanceReport.insert("",END,values=i)
conn.commit()
conn.close()
#============================Reset Data======================
def reset_data(self):
self.var_id.set("")
self.var_roll.set("")
self.var_name.set("")
self.var_time.set("")
self.var_date.set("")
self.var_attend.set("Status")
# =========================Fetch Data Import data ===============
def fetchData(self,rows):
global mydata
mydata = rows
self.attendanceReport_left.delete(*self.attendanceReport_left.get_children())
for i in rows:
self.attendanceReport_left.insert("",END,values=i)
print(i)
def importCsv(self):
mydata.clear()
fln=filedialog.askopenfilename(initialdir=os.getcwd(),title="Open CSV",filetypes=(("CSV File","*.csv"),("All File","*.*")),parent=self.root)
with open(fln) as myfile:
csvread=csv.reader(myfile,delimiter=",")
for i in csvread:
mydata.append(i)
self.fetchData(mydata)
#==================Experot CSV=============
def exportCsv(self):
try:
if len(mydata)<1:
messagebox.showerror("Error","No Data Found!",parent=self.root)
return False
fln=filedialog.asksaveasfilename(initialdir=os.getcwd(),title="Open CSV",filetypes=(("CSV File","*.csv"),("All File","*.*")),parent=self.root)
with open(fln,mode="w",newline="") as myfile:
exp_write=csv.writer(myfile,delimiter=",")
for i in mydata:
exp_write.writerow(i)
messagebox.showinfo("Successfuly","Xuất dữ liệu thành công!")
except Exception as es:
messagebox.showerror("Error",f"Due to: {str(es)}",parent=self.root)
#=============Cursur Function for CSV========================
def get_cursor_left(self,event=""):
cursor_focus = self.attendanceReport_left.focus()
content = self.attendanceReport_left.item(cursor_focus)
data = content["values"]
self.var_id.set(data[0]),
self.var_roll.set(data[1]),
self.var_name.set(data[2]),
self.var_time.set(data[3]),
self.var_date.set(data[4]),
self.var_attend.set(data[5])
#=============Cursur Function for mysql========================
def get_cursor_right(self,event=""):
cursor_focus = self.attendanceReport.focus()
content = self.attendanceReport.item(cursor_focus)
data = content["values"]
self.var_id.set(data[0]),
self.var_roll.set(data[1]),
self.var_name.set(data[2]),
self.var_time.set(data[3]),
self.var_date.set(data[4]),
self.var_attend.set(data[5])
#=========================================Update CSV============================
# export upadte
def action(self):
if self.var_id.get()=="" or self.var_roll.get=="" or self.var_name.get()=="" or self.var_time.get()=="" or self.var_date.get()=="" or self.var_attend.get()=="Status":
messagebox.showerror("Error","Vui lòng điền vào tất cả các trường là bắt buộc!",parent=self.root)
else:
try:
conn = mysql.connector.connect(user='root', password='2912002',host='localhost',database='face_recognition',port=3306)
mycursor = conn.cursor()
mycursor.execute("insert into stdattendance values(%s,%s,%s,%s,%s,%s)",(
self.var_id.get(),
self.var_roll.get(),
self.var_name.get(),
self.var_time.get(),
self.var_date.get(),
self.var_attend.get()
))
conn.commit()
self.fetch_data()
conn.close()
messagebox.showinfo("Success","Tất cả các bản ghi được lưu trong cơ sở dữ liệu!",parent=self.root)
except Exception as es:
messagebox.showerror("Error",f"Due to: {str(es)}",parent=self.root)
# conn = mysql.connector.connect(user='root', password='2912002',host='localhost',database='face_recognition',port=3306)
# mycursor = conn.cursor()
# if messagebox.askyesno("Confirmation","Are you sure you want to save attendance on database?"):
# for i in mydata:
# uid = i[0]
# uroll = i[1]
# uname = i[2]
# utime = i[3]
# udate = i[4]
# uattend = i[5]
# qury = "INSERT INTO stdattendance(std_id, std_roll_no, std_name, std_time, std_date, std_attendance) VALUES(%s,%s,%s,%s,%s,%s)"
# mycursor.execute(qury,(uid,uroll,uname,utime,udate,uattend))
# conn.commit()
# conn.close()
# messagebox.showinfo("Success","Successfully Updated!",parent=self.root)
# else:
# return False
#
if __name__ == "__main__":
root=Tk()
obj=Attendance(root)
root.mainloop()