Facial Recognition Attendance System Using Python | Face recognition Attendance system Source Code
Science & Technology
Introduction
In this article, we will discuss how to create a Python script that automates the attendance-taking process using facial recognition technology. Traditionally, attendance is marked manually by viewing images of individuals, which can be time-consuming and inefficient. Our Python-based solution will detect faces in real-time, compare them against a pre-defined database, and mark attendance accordingly.
Overview of Requirements
Before diving into coding, we need to install the necessary libraries. OpenCV will be used for video capture, the face-recognition
library for detecting and recognizing faces, and numpy
and pandas
for data manipulation. To install these libraries, you can run the following command in your terminal or command prompt:
pip install opencv-python face-recognition numpy pandas
Once the libraries are installed, we can start developing our attendance system.
Loading Known Faces
The first step in our script is to load images of individuals whose attendance we want to track. We will use the face-recognition
library to generate encodings for these images, which will allow us to recognize the faces later. We will store the generated face encodings in a list called known_face_encodings
and the corresponding names in another list called known_face_names
.
You can replace person1.jpg
and person2.jpg
with the image files of the individuals you want to recognize.
import face_recognition
import numpy as np
import pandas as pd
import cv2
import os
known_face_encodings = []
known_face_names = []
## Introduction
for filename in os.listdir('path_to_your_images'):
image = face_recognition.load_image_file(f'path_to_your_images/(filename)')
encoding = face_recognition.face_encodings(image)[0]
known_face_encodings.append(encoding)
known_face_names.append(filename.split('.')[0]) # Assuming filename is like person1.jpg
Attendance Logging Function
Next, we create a function that logs attendance every time a face is recognized. This function will maintain a record of the person’s name and the time they were detected. We will utilize a CSV file to keep track of attendance.
def mark_attendance(name):
current_time = pd.Timestamp.now()
attendance_df = pd.read_csv('attendance.csv')
if name not in attendance_df['Name'].values:
attendance_df = attendance_df.append(('Name': name, 'Time': current_time), ignore_index=True)
attendance_df.to_csv('attendance.csv', index=False)
Real-Time Face Recognition
To recognize faces in real-time, we will capture video from the webcam, detect faces within the frame, and compare them against the known faces and encodings. The process involves using the OpenCV VideoCapture
function to access the webcam and resizing each frame for faster processing.
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
rgb_frame = frame[:, :, ::-1] # Convert BGR to RGB
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]
mark_attendance(name)
top, right, bottom, left = face_location
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
In this code snippet, we continuously read frames from the webcam, detect faces, and check for matches. If a match is found, the attendance is logged, and a rectangle is drawn around the recognized face along with the display of the person’s name.
Conclusion
With this setup, we have successfully built a functional attendance system using Python that leverages facial recognition technology to automate the process. This system can greatly increase efficiency and accuracy in attendance tracking. If you found this guide helpful, please consider liking and subscribing for more coding tutorials. Additionally, feel free to share any project ideas you’d like to see in the comments.
Keywords
- Python
- Facial Recognition
- Attendance System
- OpenCV
- Face Recognition
- Numpy
- Pandas
- Real-time Video Capture
FAQ
Q: What libraries do I need to install?
A: You need to install OpenCV, face-recognition, numpy, and pandas using the command pip install opencv-python face-recognition numpy pandas
.
Q: How does the attendance logging work?
A: The attendance is logged in a CSV file. Every time a recognized face appears, the person's name and the current timestamp are added to the file if they are not already present.
Q: Can this system work in real-time?
A: Yes, the system captures video from your webcam in real-time and detects faces as they appear.
Q: How do I stop the video feed?
A: You can stop the video feed by pressing the 'Q' key on your keyboard.
Q: Can I modify the list of known faces?
A: Yes, you can easily add or replace image files in the code to update the list of known faces. Make sure to include their corresponding names.