Building a Face Recognition Attendance System with Python

Introduction: In today's digital era, technology has transformed various aspects of our lives, including how we manage attendance in different settings. Traditional methods of attendance tracking, such as paper-based sign-in sheets or manual entry into digital systems, can be time-consuming and prone to errors. However, with advancements in computer vision and machine learning, we can now create more efficient and accurate attendance systems using face recognition technology.

What is Face Recognition? Face recognition is a biometric technology that identifies or verifies individuals by analyzing and comparing patterns in their facial features. It involves capturing an image or video of a person's face, extracting unique facial features, and matching them against a database of known faces to make a positive identification.

Building a Face Recognition Attendance System: In this blog post, we'll explore how to build a simple face recognition attendance system using Python and popular libraries such as OpenCV and face_recognition. The system will capture live video from a webcam, detect faces in the video stream, and match them against a database of known faces to mark attendance automatically.

Step 1: Installing Required Libraries: Before we begin, make sure you have the necessary libraries installed. You can install them using pip, the Python package manager, with the following commands:

bashCopy code

pip install opencv-python pip install face-recognition

Step 2: Collecting Face Data: The first step is to collect facial data for the individuals whose attendance we want to track. This involves taking high-quality images of their faces from different angles. We then use these images to create facial encodings, which are numerical representations of facial features.

Step 3: Face Recognition Setup: Once we have the facial data, we can set up the face recognition system. This involves loading the known face encodings into memory and specifying the corresponding names for each face.

Step 4: Capturing Live Video: Next, we capture live video from a webcam using OpenCV. We continuously process each frame of the video stream to detect faces using the face_recognition library.

Step 5: Recognizing Faces and Marking Attendance: For each detected face, we compare its encoding with the known face encodings. If a match is found, we identify the person and mark their attendance. We also record the timestamp of their attendance.

Step 6: Displaying Results: Finally, we display the live video stream with attendance information overlaid on the screen. This allows users to monitor attendance in real-time.

Conclusion: In conclusion, building a face recognition attendance system with Python is a powerful way to automate attendance tracking in various settings, such as classrooms, workplaces, or events. By leveraging the capabilities of computer vision and machine learning, we can create efficient, accurate, and user-friendly solutions that streamline attendance management processes. With further enhancements and customization, such systems have the potential to revolutionize how we track attendance in the future.

References:

  • OpenCV Documentation: https://opencv.org/
  • face_recognition Documentation: https://github.com/ageitgey/face_recognition

Note: Remember to handle privacy and data security concerns appropriately when implementing face recognition systems, and ensure compliance with relevant regulations and policies.

 

Code:

import face_recognition
import cv2
import numpy as np
import csv
from datetime import datetime

video_Capture = cv2.VideoCapture(0)

rajeeb_image = face_recognition.load_image_file(r"D:\GATE Python PW\Code\rajeeb.jpg")
rajeeb_encoding = face_recognition.face_encodings(rajeeb_image)[0]

akhaya_image = face_recognition.load_image_file(r"D:\GATE Python PW\Code\akhaya.jpg")
akhaya_encoding = face_recognition.face_encodings(akhaya_image)[0]

known_face_encoding = [rajeeb_encoding, akhaya_encoding]
known_face_name = ["Rajeeb", "Akhaya Kumar"]

# List of expected students
student = known_face_name.copy()

# Get current date and time
now = datetime.now()
current_date = now.strftime("%Y-%m-%d")

f = open(f"{current_date}.csv", "w+", newline="")
lnwriter = csv.writer(f)

while True:
    _, frame = video_Capture.read()
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

    # Recognize faces
    face_locations = face_recognition.face_locations(rgb_small_frame)
    face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

    for face_encoding in face_encodings:
        matches = face_recognition.compare_faces(known_face_encoding, face_encoding)
        face_distance = face_recognition.face_distance(known_face_encoding, face_encoding)
        best_match_index = np.argmin(face_distance)

        if matches[best_match_index]:
            name = known_face_name[best_match_index]
            # Add the text if the person is present
            if name in known_face_name:
                font = cv2.FONT_HERSHEY_SIMPLEX
                bottomLeftCorner = (10, 100)
                fontScale = 1.5
                fontColor = (0, 255, 0)
                thickness = 3
                lineType = 2
                cv2.putText(frame, name + " Present", bottomLeftCorner, font, fontScale, fontColor, thickness, lineType)

                if name in student:
                    student.remove(name)
                    current_time = now.strftime("%H-%M-%S")
                    lnwriter.writerow([name, current_time])

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

video_Capture.release()
cv2.destroyAllWindows()
f.close()

 

Comments

Popular posts from this blog

Write A Python Program, Create n Empty Dictionary Allow 4 Friends To, Enter Their Favorite Programming Language As values and User Keys as their Name Assume That The Names Are Unique.

Write Python Program To Store Seven Fruits Name In a List Enter By The User