πŸ”₯ 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 cv2
import face_recognition
import numpy as np
import os
import csv
from datetime import datetime

# Load known faces
known_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 webcam
video_capture = cv2.VideoCapture(0)

# Open CSV file for attendance
csv_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 attendance
        with 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"):
        break

video_capture.release()
cv2.destroyAllWindows()

Comments

Popular posts from this blog

πŸš€ Simple Login & Registration System in Python Tkinter πŸ“±

πŸš€ Create a Python Screen Recorder with Audio (Complete Code)

πŸ“‘ Fuzzu Packet Sniffer – Python GUI for Real-Time IP Monitoring | Tkinter + Scapy