Face Recognize Code
Face Recognize Code
import face_recognition
import numpy as np
import pandas as pd
import threading
import os
root = Tk()
root.geometry("700x750")
root.resizable(False, False)
school_logo = Image.open(school_logo_path)
school_logo_img = ImageTk.PhotoImage(school_logo)
known_face_encodings = []
known_face_names = []
attendance_file = 'Attendance_Data_File.xlsx'
if os.path.exists(attendance_file):
attendance_df = pd.read_excel(attendance_file)
else:
try:
image = face_recognition.load_image_file(image_path)
encoding = face_recognition.face_encodings(image)[0]
known_face_encodings.append(encoding)
known_face_names.append(name)
except Exception as e:
def initialize_faces():
initialize_faces()
def save_attendance_async():
try:
attendance_df.to_excel(writer, index=False)
except PermissionError:
messagebox.showerror("Permission Error", f"Unable to write to {attendance_file}. Please ensure the file is not open in another
program.")
def start_face_recognition():
global attendance_df
video_capture = cv2.VideoCapture(0)
while True:
if not ret:
break
face_locations = face_recognition.face_locations(rgb_small_frame)
name = "Unknown"
class_name = "Unknown"
section = "Unknown"
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
if name in attendance_df['Name'].values:
attendance_column = None
if pd.isna(attendance_df.at[student_index, col]):
attendance_column = col
break
if not attendance_column:
# Update attendance
else:
new_row = pd.DataFrame([{
"Name": name,
"Class": class_name,
"Section": section,
"Time": datetime.now().strftime('%H:%M:%S')
}])
threading.Thread(target=save_attendance_async).start()
else:
root.after(0, update_status_label, "Face Not Recognized", "red")
break
video_capture.release()
cv2.destroyAllWindows()
status_label.config(text=text, fg=color)
def show_attendance():
global attendance_df
attendance_window = Toplevel(root)
attendance_window.title("Attendance Log")
attendance_window.geometry("700x500")
date_label.pack(pady=5)
attendance_text.pack(padx=10, pady=10)
attendance_text.delete(1.0, END)
attendance_text.config(state=DISABLED)
def show_registered_data():
registered_window = Toplevel(root)
registered_window.title("Registered Data")
registered_window.geometry("600x400")
registered_tree.heading("Name", text="Name")
registered_tree.heading("Class", text="Class")
registered_tree.heading("Section", text="Section")
for i in range(len(known_face_names)):
def stop_system():
root.quit()
logo_label.pack(pady=10)
title_label = Label(root, text="Face Recognition Attendance System", font=("Helvetica", 16, "bold"), fg="#333", bg="#f7f7f7")
title_label.pack(pady=10)
start_button.pack(pady=10)
show_attendance_button.pack(pady=10)
show_registered_button.pack(pady=10)
# Stop Button
stop_button.pack(pady=20)
# Status Label
status_label = Label(root, text="Status: Waiting for input...", font=("Helvetica", 12), fg="black", bg="#f7f7f7")
status_label.pack(pady=10)
root.mainloop()