Face Recognition Attendance System

 Face Recognition Attendance System 

1. Introduction

The Face Recognition Attendance System is an application that automates the attendance process using facial recognition technology. It utilizes facial embeddings for identifying individuals and logging their attendance in a database or file. This project leverages libraries such as OpenCV and face_recognition.

2. Prerequisites

• Python: Install Python 3.x from the official Python website.
• Required Libraries:
  - opencv-python: Install using pip install opencv-python
  - face-recognition: Install using pip install face-recognition
  - numpy: Install using pip install numpy
  - pandas: Install using pip install pandas (for logging attendance)
• A webcam or a camera-enabled device for real-time detection.

3. Project Setup

1. Create a Project Directory:

- Name your project folder, e.g., `FaceRecognitionAttendance`.
- Inside this folder, create the Python script file (`attendance_system.py`).

2. Install Required Libraries:

Ensure OpenCV, face-recognition, numpy, and pandas are installed using `pip`.

4. Writing the Code

Below is an example code snippet for the Face Recognition Attendance System:


import cv2
import face_recognition
import numpy as np
import pandas as pd
from datetime import datetime

# Load known faces and their names
def load_known_faces():
    known_face_encodings = []
    known_face_names = []

    # Load sample images and generate encodings
    sample_images = ["person1.jpg", "person2.jpg"]  # Replace with your image paths
    sample_names = ["John Doe", "Jane Smith"]
    for image_path, name in zip(sample_images, sample_names):
        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)

    return known_face_encodings, known_face_names

# Initialize attendance logger
def log_attendance(name):
    now = datetime.now()
    date_time = now.strftime("%Y-%m-%d %H:%M:%S")
    attendance_data = {"Name": [name], "Date-Time": [date_time]}
    df = pd.DataFrame(attendance_data)
    with open("attendance.csv", "a") as f:
        df.to_csv(f, header=f.tell()==0, index=False)

# Main function
def main():
    known_face_encodings, known_face_names = load_known_faces()

    video_capture = cv2.VideoCapture(0)

    while True:
        ret, frame = video_capture.read()
        rgb_frame = frame[:, :, ::-1]

        face_locations = face_recognition.face_locations(rgb_frame)
        face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

        for face_encoding, face_location in zip(face_encodings, face_locations):
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]
                log_attendance(name)

            top, right, bottom, left = face_location
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
            cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)

        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()
   

5. Key Components

• Face Detection and Recognition: Uses face_recognition library to detect and encode faces.
• Attendance Logging: Logs recognized faces with timestamps into a CSV file.
• Real-Time Processing: Captures live video feed using OpenCV.

6. Testing

1. Ensure sample images of known faces are available in the project directory.

2. Run the script:

   python attendance_system.py

3. Observe live video feed and ensure recognized faces are logged in 'attendance.csv'.

7. Enhancements

• Database Integration: Save attendance data to a database instead of a CSV file.
• Multi-Camera Support: Extend the system to handle multiple camera feeds.
• Improved Accuracy: Use advanced models for better recognition performance.

8. Troubleshooting

• Unrecognized Faces: Ensure proper lighting and clear face images.
• CSV Logging Errors: Verify write permissions for the project directory.
• Camera Issues: Check camera compatibility and driver installation.

9. Conclusion

The Face Recognition Attendance System automates the attendance process using facial recognition. With further improvements, it can be deployed in schools, offices, and other organizations for efficient attendance management.