-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_gui.py
254 lines (200 loc) · 9.44 KB
/
main_gui.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
import tkinter as tk
from tkinter import filedialog, messagebox
import firebase_admin
from firebase_admin import credentials, db, storage
import subprocess
from dotenv import load_dotenv
import os
import sys
import cv2
import numpy as np
import pandas as pd
load_dotenv()
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
'databaseURL': os.getenv('FIREBASE_DATABASE_URL'),
'storageBucket': os.getenv('FIREBASE_STORAGE_BUCKET')
})
ref = db.reference('Students')
bucket = storage.bucket()
def open_main_py():
try:
p = sys.executable
subprocess.run([p, "EncodeGenerator.py"], check=True)
subprocess.run([p, "main.py"], check=True)
except Exception as e:
messagebox.showerror("Error", f"Failed to open main.py: {e}")
def open_add_student_form():
def submit_form():
name = name_entry.get()
branch = branch_entry.get() # Get the branch from input
roll_no = roll_no_entry.get()
photo_path = photo_label.cget("text")
if photo_path == "No file selected":
messagebox.showwarning("Warning", "Please upload a photo.")
return
if not branch or not roll_no or not name:
messagebox.showwarning("Warning", "Please fill all the fields")
return
# Save student data to Firebase
student_data = {
"name": name,
"major": branch,
"starting_year": 2023,
"total_attendance": 0,
"standing": "G",
"year": 1,
"last_attendance_time": "2024-04-14 01:10:34"
}
ref.child(roll_no).set(student_data)
# Save photo in ./Images folder
if not os.path.exists('./Images'):
os.makedirs('./Images')
img = cv2.imread(photo_path)
resized_img = cv2.resize(img, (216, 216))
cv2.imwrite(f'./Images/{roll_no}.png', resized_img)
blob = bucket.blob(f'Images/{roll_no}.png')
blob.upload_from_filename(f'./Images/{roll_no}.png')
csv_file_path = f'school_attendance_database/{branch}.csv'
if os.path.exists(csv_file_path):
df = pd.read_csv(csv_file_path)
else:
df = pd.DataFrame(columns=['Roll No.', 'Name'])
df.to_csv(csv_file_path, index=False)
df = pd.read_csv(csv_file_path)
new_row = pd.DataFrame({col: [np.nan] for col in df.columns})
new_row['Roll No.'] = roll_no
new_row['Name'] = student_data["name"]
df = pd.concat([df, new_row], ignore_index=True)
df.to_csv(csv_file_path, index=False)
messagebox.showinfo("Success", "Student data and photo added successfully!")
add_student_window.destroy()
add_student_window = tk.Toplevel(root)
add_student_window.title("Add Student")
tk.Label(add_student_window, text="Name:").grid(row=0, column=0, padx=10, pady=10)
tk.Label(add_student_window, text="Branch:").grid(row=1, column=0, padx=10, pady=10)
tk.Label(add_student_window, text="Roll No.:").grid(row=2, column=0, padx=10, pady=10)
tk.Label(add_student_window, text="Photo:").grid(row=3, column=0, padx=10, pady=10)
name_entry = tk.Entry(add_student_window)
branch_entry = tk.Entry(add_student_window)
roll_no_entry = tk.Entry(add_student_window)
photo_label = tk.Label(add_student_window, text="No file selected")
name_entry.grid(row=0, column=1, padx=10, pady=10)
branch_entry.grid(row=1, column=1, padx=10, pady=10)
roll_no_entry.grid(row=2, column=1, padx=10, pady=10)
photo_label.grid(row=3, column=1, padx=10, pady=10)
def upload_photo():
file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg *.jpeg *.png")])
if file_path:
photo_label.config(text=file_path)
tk.Button(add_student_window, text="Upload Photo", command=upload_photo).grid(row=4, column=0, columnspan=2, pady=10)
tk.Button(add_student_window, text="Submit", command=submit_form).grid(row=5, column=0, columnspan=2, pady=10)
def delete_student():
def submit_delete():
roll_no = roll_no_entry.get()
branch = branch_entry.get()
if not roll_no:
messagebox.showwarning("Warning", "Roll No. cannot be empty.")
return
if not branch:
messagebox.showwarning("Warning", "Branch cannot be empty.")
return
# Delete local image file
local_image_path = f'./Images/{roll_no}.png'
if os.path.exists(local_image_path):
os.remove(local_image_path)
# Delete image from Firebase Storage
blob = bucket.blob(f'Images/{roll_no}.png')
if blob.exists():
blob.delete()
# Delete student record from Firebase Database
ref.child(roll_no).delete()
csv_file_path = f'school_attendance_database/{branch}.csv'
# Delete student record from Attendace sheet
if os.path.exists(csv_file_path):
df = pd.read_csv(csv_file_path)
if 'Roll No.' in df.columns:
df['Roll No.'] = df["Roll No."].astype(str)
df = df[df['Roll No.'] != str(roll_no)]
df.to_csv(csv_file_path, index=False)
messagebox.showinfo("Success", "Student removed from attendance database!")
delete_student_window.destroy()
delete_student_window = tk.Toplevel(root)
delete_student_window.title("Delete Student")
tk.Label(delete_student_window, text="Branch:").grid(row=0, column=0, padx=10, pady=10)
branch_entry = tk.Entry(delete_student_window)
branch_entry.grid(row=0, column=1, padx=10, pady=10)
tk.Label(delete_student_window, text="Roll No:").grid(row=1, column=0, padx=10, pady=10)
roll_no_entry = tk.Entry(delete_student_window)
roll_no_entry.grid(row=1, column=1, padx=10, pady=10)
tk.Button(delete_student_window, text="Submit", command=submit_delete).grid(row=1, column=0, columnspan=2, pady=10)
def look_attendance():
def open_attendance_form():
def submit_branch():
branch = branch_entry.get().strip()
if not branch:
messagebox.showwarning("Warning", "Branch cannot be empty.")
return
csv_file_path = f'school_attendance_database/{branch}.csv'
if os.path.exists(csv_file_path):
try:
os.startfile(csv_file_path)
except Exception as e:
messagebox.showerror("Error", f"Failed to open {csv_file_path}: {e}")
else:
messagebox.showwarning("Warning", f"No attendance file found for branch: {branch}")
branch_window.destroy()
branch_window = tk.Toplevel(root)
branch_window.title("Select Branch")
tk.Label(branch_window, text="Branch:").grid(row=0, column=0, padx=10, pady=10)
branch_entry = tk.Entry(branch_window)
branch_entry.grid(row=0, column=1, padx=10, pady=10)
tk.Button(branch_window, text="Submit", command=submit_branch).grid(row=1, column=0, columnspan=2, pady=10)
open_attendance_form()
# GUI DESIGN SECTION USING TKITER
root = tk.Tk()
root.title("Attendance System")
heading_label = tk.Label(
root,
text="CONTACTLESS ATTENDANCE SYSTEM",
font=("Arial", 24, "bold"),
fg="red",
)
heading_label.pack(padx=20, pady=20, fill=tk.X)
capture_icon = tk.PhotoImage(file="Resources/capture_icon.png")
add_student_icon = tk.PhotoImage(file="Resources/add_student_icon.png")
delete_student_icon = tk.PhotoImage(file="Resources/delete_student.png")
attendance_icon = tk.PhotoImage(file="Resources/attendance_icon.png")
icon_row_frame_1 = tk.Frame(root)
icon_row_frame_1.pack(padx=20, pady=10, fill="x")
capture_frame = tk.Frame(icon_row_frame_1)
capture_frame.grid(row=0, column=0, padx=20, pady=10, sticky="ew")
capture_label = tk.Label(capture_frame, image=capture_icon, cursor="hand2")
capture_label.pack()
tk.Label(capture_frame, text="Detect Faces", font=("Arial", 14)).pack()
capture_label.bind("<Button-1>", lambda e: open_main_py())
attendance_frame = tk.Frame(icon_row_frame_1)
attendance_frame.grid(row=0, column=1, padx=20, pady=10, sticky="ew")
attendance_label = tk.Label(attendance_frame, image=attendance_icon, cursor="hand2")
attendance_label.pack()
tk.Label(attendance_frame, text="Attendance Sheet", font=("Arial", 14)).pack()
attendance_label.bind("<Button-1>", lambda e: look_attendance())
icon_row_frame_1.grid_columnconfigure(0, weight=1)
icon_row_frame_1.grid_columnconfigure(1, weight=1)
icon_row_frame_2 = tk.Frame(root)
icon_row_frame_2.pack(padx=20, pady=10, fill="x")
add_student_frame = tk.Frame(icon_row_frame_2)
add_student_frame.grid(row=0, column=0, padx=20, pady=10, sticky="ew")
add_student_label = tk.Label(add_student_frame, image=add_student_icon, cursor="hand2")
add_student_label.pack()
tk.Label(add_student_frame, text="Add Student", font=("Arial", 14)).pack()
add_student_label.bind("<Button-1>", lambda e: open_add_student_form())
delete_student_frame = tk.Frame(icon_row_frame_2)
delete_student_frame.grid(row=0, column=1, padx=20, pady=10, sticky="ew")
delete_student_label = tk.Label(delete_student_frame, image=delete_student_icon, cursor="hand2")
delete_student_label.pack()
tk.Label(delete_student_frame, text="Delete Student", font=("Arial", 14)).pack()
delete_student_label.bind("<Button-1>", lambda e: delete_student())
icon_row_frame_2.grid_columnconfigure(0, weight=1)
icon_row_frame_2.grid_columnconfigure(1, weight=1)
root.mainloop()