Face attendance + face recognition with Python | Computer vision tutorial
Table of Contents
Introduction
In this tutorial, we will create a face attendance system using Python and computer vision techniques. This project utilizes face recognition technology to automate attendance tracking, making it highly relevant for educational institutions and workplaces. By the end of this guide, you will have a functional system that can recognize faces and log attendance efficiently.
Step 1: Set Up Your Environment
Before starting the coding process, ensure your development environment is ready. Follow these sub-steps:
-
Install Python: Make sure you have Python 3 installed on your machine. You can download it from python.org.
-
Install Required Libraries: Use pip to install the necessary libraries. Open your terminal and run the following commands:
pip install face_recognition pip install opencv-python pip install numpy
-
Clone the Project Repository: Download the code for the face attendance system from GitHub. Run:
git clone https://github.com/computervisioneng/face-attendance-system.git
Step 2: Prepare Your Data
To enable the system to recognize faces, you need to prepare a dataset of known faces.
-
Create a Folder for Images: Inside the cloned repository, create a folder named
known_faces
. -
Add Images: Place images of the individuals you want to recognize into the
known_faces
folder. Ensure the images are clear and frontal for better recognition accuracy. -
Label the Images: Name the image files using the format
name.jpg
, wherename
is the person's name. This will help in identifying them later in the attendance logs.
Step 3: Write the Attendance System Code
Now it’s time to write the code that will handle face recognition and attendance logging.
-
Import Libraries: Start by importing the necessary libraries in your Python script:
import face_recognition import cv2 import numpy as np import os
-
Load Known Faces: Load the images from the
known_faces
folder and encode them:known_face_encodings = [] known_face_names = [] for filename in os.listdir('known_faces'): if filename.endswith('.jpg'): image = face_recognition.load_image_file(f'known_faces/{filename}') encoding = face_recognition.face_encodings(image)[0] known_face_encodings.append(encoding) known_face_names.append(os.path.splitext(filename)[0])
-
Capture Video: Set up a video capture to read from your webcam:
video_capture = cv2.VideoCapture(0)
-
Recognition Loop: Implement a loop to continuously capture frames and process them:
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 (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): 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 print(f'Attendance logged for: {name}') # Draw rectangle and label on the frame cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
Conclusion
Congratulations! You have successfully built a face attendance system using Python and computer vision techniques. Key takeaways include:
- Setting up the Python environment and necessary libraries.
- Preparing a dataset of images for face recognition.
- Writing a Python script to capture video, recognize faces, and log attendance.
As next steps, consider enhancing the system by integrating a database for attendance records or adding features like email notifications. Happy coding!