π₯ AI Face Recognition Attendance System in Python
Demo :
Click Video πππ
π Folder Structure:
π Face-Recognition-Attendance
│── π main.py
│── π face_encodings.py
│── π attendance.csv
│── π requirements.txt
π images (Folder to store face images)
π Python Code:
1️⃣ main.py (Face Recognition & Attendance Marking)
import cv2import face_recognitionimport numpy as npimport osimport csvfrom datetime import datetime# Load known facesknown_face_encodings = []known_face_names = []path = "images"for img_name in os.listdir(path):img = face_recognition.load_image_file(f"{path}/{img_name}")encoding = face_recognition.face_encodings(img)[0]known_face_encodings.append(encoding)known_face_names.append(os.path.splitext(img_name)[0])# Initialize webcamvideo_capture = cv2.VideoCapture(0)# Open CSV file for attendancecsv_filename = "attendance.csv"if not os.path.exists(csv_filename):with open(csv_filename, "w", newline="") as file:writer = csv.writer(file)writer.writerow(["Name", "Time"])while True:ret, frame = video_capture.read()rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)face_locations = face_recognition.face_locations(rgb_frame)face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)for face_encoding, (top, right, bottom, left) in zip(face_encodings, face_locations):matches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"if True in matches:match_index = matches.index(True)name = known_face_names[match_index]cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)# Mark attendancewith open(csv_filename, "a", newline="") as file:writer = csv.writer(file)writer.writerow([name, datetime.now().strftime("%Y-%m-%d %H:%M:%S")])cv2.imshow("Face Recognition Attendance", frame)if cv2.waitKey(1) & 0xFF == ord("q"):breakvideo_capture.release()cv2.destroyAllWindows()
Comments
Post a Comment