Face Recognition Attendance System Using Python (With Code)

thetechwriters Last Updated : 07 Oct, 2024
10 min read

Introduction

In this tutorial, you will learn how to build a face recognition system using computer vision in Python. Face recognition is an image classification problem and is a step further to face detection. In face detection, we only detect the location of the human face in an image, but in the face recognition classifier, we make a system that can identify humans.

In this article, you will get to know about a face recognition attendance system using Python. We will explore a face recognition project in Python with source code, detailing how to create an efficient face detection attendance system using Python. This comprehensive guide will help you understand the face recognition attendance system project and its practical applications in various settings.

Face Recognition Attendance System

“Face recognition is a broad challenge of verifying or identifying people in pictures or videos. Big tech giants are still working to make a faster and more accurate face recognition model.”

Learning Objectives

  • Getting an overview of face recognition algorithm in Artificial Intelligence and its real-world applications.
  • Learning to preprocess and organize the data for training the face recognition model.
  • Learning to build the face recognition model using OpenCV and face recognition libraries.
  • Learning how to integrate the face recognition model with an attendance system.

This article was published as a part of the Data Science Blogathon

What is the Face Recognition Attendance System?

A Face Recognition Attendance System is a new way for companies to keep track of when their employees come to work and when they leave. It uses cameras to take pictures of employees’ faces and then compares those pictures to a list of approved faces. If the system recognizes the employee’s face, it automatically marks them as present.This system works like this:

  1. Cameras take pictures of employees as they arrive or leave work.
  2. The system looks at the pictures and tries to match them with the faces it has stored in its database.
  3. If the system finds a match, it records the time the employee arrived or left.

This new system has some advantages:

  1. It saves time because employees don’t have to write down their own attendance or use ID cards.
  2. It’s more accurate because it prevents mistakes and stops employees from clocking in for each other.
  3. Employees can clock in from anywhere, which is helpful for people who work outside the office.
  4. The attendance records are easy to access and use for things like payroll.
  5. The system has security features to make sure only real employees are allowed to clock in.

Real-World Applications of Face Recognition

Face recognition is currently being used to make the world safer, smarter, and more convenient.

Some of its most common use cases include finding missing persons, solving retail crime, security identification, identifying accounts on social media, school attendance systems, and recognizing drivers in cars.

There are several methods to perform facial recognition depending on the performance and complexity.

Traditional Face Recognition Algorithms

During the 1990s, holistic approaches were used for face recognition. Handcrafted local descriptors became popular In the early 1920s, and then the local feature learning approaches were followed in the late 2000s. Nowadays, face recognition and face detection algorithms that are widely used and are implemented in OpenCV are as follows:

Each method follows a different approach to extracting and matching the image information with the input image.

Fischer-faces and Eigenfaces have almost similar approaches as well as SURF and SIFT.

LBPH is a simple yet very efficient method but is slow compared to modern face recognizers.

These algorithms are not faster compared to modern days face-recognition algorithms. Traditional algorithms can’t be trained only by taking a single picture of a person.

Deep Learning for Face Recognition

Some of the widely used Deep Learning-based face recognition systems are:

  • DeepFace
  • DeepID series of systems
  • VGGFace
  • FaceNet

Face recognizers generally take face images and find the important points such as the corner of the mouth, an eyebrow, eyes, nose, lips, etc. The coordinates of these points are called facial feature points. There are 66 such points. In this way, a different technique for finding feature
points give different results.

facial features

How Does a Traditional Face Recognition Model Work?

  1. Face Detection: Face detector algorithms locate faces and draw bounding boxes around faces and keep the coordinates of bounding boxes.
  2. Face Alignments: Normalize the faces to be consistent with the training database.
  3. Feature Extraction: Extract features of faces that will be used for training and recognition tasks.
  4. Face Recognition: Matching the face against one or more known faces in a prepared database.

In the traditional method of face recognition, we had separate modules to perform these 4 steps, which was painful. -In this article, you will see a library that combines all these 4 steps in a single step.

Steps to Build a Face Recognition System

Step 1: Install libraries

We need to install 2 libraries in order to implement face recognition.

dlib: Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real-world problems.

# installing dlib 
pip install dlib

face recognition: The face_recognition library, created and maintained by Adam Geitgey, wraps around dlib facial recognition functionality.

# installing face recognition
pip install face recognition

Opencv for some image pre-processing.

# installing opencv 
pip install opencv

Note: if in case you encounter any error while installing dlib, i would recommend you to install the C++ development toolkit using vs_code community

Step 2: Import libraries

Now that you have downloaded all the important libraries, let’s import them to build the system.

import cv2
import numpy as np
import face_recognition

Step 3: Load images

After importing libraries you need to load an image.

face_recognition library loads images in the form of BGR, in order to
print the image you should convert it into RGB using OpenCV.

imgelon_bgr = face_recognition.load_image_file('elon.jpg')
imgelon_rgb = cv2.cvtColor(imgelon_bgr,cv2.COLOR_BGR2RGB)
cv2.imshow('bgr', imgelon_bgr)
cv2.imshow('rgb', imgelon_rgb)
cv2.waitKey(0)
face recognition

As you see, RGB looks natural, so you will always change the channel to RGB.

Step 4: Find the face location and draw bounding boxes

You need to draw a bounding box around the faces in order to show if the human face has been detected or not.

imgelon =face_recognition.load_image_file('elon.jpg')
imgelon = cv2.cvtColor(imgelon,cv2.COLOR_BGR2RGB)
#----------Finding face Location for drawing bounding boxes-------
face = face_recognition.face_locations(imgelon_rgb)[0]
copy = imgelon.copy()
#-------------------Drawing the Rectangle-------------------------
cv2.rectangle(copy, (face[3], face[0]),(face[1], face[2]), (255,0,255), 2)
cv2.imshow('copy', copy)
cv2.imshow('elon',imgelon)
cv2.waitKey(0)
face recognition shows of Elon Musk

Step 5: Train an image for face recognition

This library is made in such a way that it automatically finds the face and works on only faces, so you don’t need to crop the face out of pictures.

Training:

At this stage, we convert the train image into some encodings and store the encodings with the given name of the person for that image.

train_elon_encodings = face_recognition.face_encodings(imgelon)[0]

Testing:

For testing, we load an image and convert it into encodings, and now match encodings with the stored encodings during training. This matching is based on finding maximum similarity. When you find the encoding matching the test image, you get the name associated with train encodings.

# lets test an image
test = face_recognition.load_image_file('elon_2.jpg')
test = cv2.cvtColor(test, cv2.COLOR_BGR2RGB)
test_encode = face_recognition.face_encodings(test)[0]
print(face_recognition.compare_faces([train_encode],test_encode))

face_recognition.compare_faces returns True if the person in both images is the same, otherwise, it returns False.

Building a Face Recognition System

Step 1: Import the necessary libraries.

import cv2
import face_recognition
import os
import numpy as np
from datetime import datetime
import pickle

Step 2: Define a folder path where your training image dataset will be stored.

path = 'student_images'

Note: for training, we only need to drop the training images in the path directory and the image name must be person_name.jpg/jpeg format.

For example: as you see in my student_images path, I have 6 persons. hence our model can recognize only these 6 persons. you can add more pictures in this directory for more people to be recognized.

images for face recognition

Step 3: Now, create a list to store person_name and image array.

Traverse all image files present in the path directory, read images, and append the image array to the image list and file name to classNames.

images = []
classNames = []mylist = os.listdir(path)
for cl in mylist:
    curImg = cv2.imread(f'{path}/{cl}')
    images.append(curImg)
    classNames.append(os.path.splitext(cl)[0])

Step 4: Create a function to encode all the train images and store them in a variable encoded_face_train. 

def findEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encoded_face = face_recognition.face_encodings(img)[0]
        encodeList.append(encoded_face)
    return encodeList
encoded_face_train = findEncodings(images)

Step 5: Create a function that will create aAttendance.csv file to store the attendance with time.

Note: here you need to create Attendance.csv file manually and give the path in the function

def markAttendance(name):
    with open('Attendance.csv','r+') as f:
        myDataList = f.readlines()
        nameList = []
        for line in myDataList:
            entry = line.split(',')
            nameList.append(entry[0])
        if name not in nameList:
            now = datetime.now()
            time = now.strftime('%I:%M:%S:%p')
            date = now.strftime('%d-%B-%Y')
            f.writelines(f'n{name}, {time}, {date}')

Step 6: With open(“filename.csv”,’r+’) create a file, and ‘r+’ mode is used to open a file for reading and writing.

We first check if the name of the attendee is already available in attendance.csv. We won’t write attendance again. If the attendee’s name is not available in attendance.csv we will write the attendee’s name with a time of function call.

Read Webcam for Real-Time Recognition

# take pictures from webcam 
cap  = cv2.VideoCapture(0)while True:
    success, img = cap.read()
    imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
    faces_in_frame = face_recognition.face_locations(imgS)
    encoded_faces = face_recognition.face_encodings(imgS, faces_in_frame)for encode_face, faceloc in zip(encoded_faces,faces_in_frame):
        matches = face_recognition.compare_faces(encoded_face_train, encode_face)
        faceDist = face_recognition.face_distance(encoded_face_train, encode_face)
        matchIndex = np.argmin(faceDist)
        print(matchIndex)
        if matches[matchIndex]:
            name = classNames[matchIndex].upper().lower()
            y1,x2,y2,x1 = faceloc
            # since we scaled down by 4 times
            y1, x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
            cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
            cv2.rectangle(img, (x1,y2-35),(x2,y2), (0,255,0), cv2.FILLED)
            cv2.putText(img,name, (x1+6,y2-5), cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
            markAttendance(name)
    cv2.imshow('webcam', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  • Resize the image by 1/4 only for the recognition part. output frame will be of the original size.
  • Resizing improves the Frame per Second.
  • face_recognition.face_locations()is called on the resized image(imgS) .for face bounding box coordinates must be multiplied by 4 in order to overlay on the output frame.
  • face_recognition.distance() returns an array of the distance of the test image with all images present in our train directory.
  • The index of the minimum face distance will be the matching face.
  • After finding the matching name, we call the markAttendance function.
  • Draw a bounding box using cv2.rectangle().
  • We put the matching name on the output frame using cv2.putText().
face recognition system

Attendance Report

attendance report

Challenges Faced by Face Recognition Systems

Although building facial recognition seems easy, it is not as easy in real-world images that are being taken without any constraints. There are several challenges that are faced by the Facial Recognitions System, as follows:

  • Illumination: It changes the face’s appearance drastically. It is observed that even slight changes in lighting conditions cause a significant impact on its results.
  • Pose: Facial Recognition systems are highly sensitive to the pose, Which may result in faulty recognition or no recognition if the database is only trained on frontal face view.
  • Facial Expressions: Different expressions of the same individual are another significant factor that needs to be taken into account. Modern Recognizers can easily deal with it, though.
  • Low Resolution: The recognizer must be trained on a good-resolution picture. Otherwise, the model will fail to extract features.
  • Aging: With increasing age, the human face features shape, lines, and texture changes which are yet another challenge.

Face recognition-based attendance system project source code

You can find the source code for a face recognition attendance system on platforms like GitHub. Here’s a basic guide on how to set it up.

Step-by-Step Instructions

  • Find the Code:
    • Look for a GitHub repository. One example is by a user named Patelrahul4884. You can search for “face recognition attendance system GitHub” to find it.
  • Download the Code:
    • Clone or download the repository to your computer.
  • Install Required Software:
    • Make sure you have Python installed on your computer.
    • Open your command prompt or terminal and install the necessary packages. You can usually do this by running:
pip install -r requirements.txt
  • Set Up Your Folder:
    • Create a folder named TrainingImage. This is where you will store the images of the people whose attendance you want to track.
  • Adjust the Code:
    • Open the files attendance.py and automaticAttendance.py in a text editor.
    • Change the file paths in the code to match where you saved your images and other files.
  • Run the Program:
    • Start the system by running the attendance.py file. This will let you register new students, take their pictures, and automatically mark their attendance.

Conclusion

In this article, we discussed how to create a face recognition system using the face_recognition library and made an attendance system. The face recognition model can be trained using a dataset of labeled face images, and the model accuracy can be improved by using techniques such as face alignment and data augmentation. You can further design GUI using Tkinter or Pyqt for the face recognition attendance system.

Hope you like the article! The face recognition attendance system using Python is an innovative project that automates attendance marking through facial detection. This face recognition project in Python with source code enables users to register their faces and automatically log attendance. The face detection attendance system using Python employs libraries like OpenCV, making it efficient and user-friendly. Overall, this face recognition attendance system project enhances accuracy and reduces manual errors in attendance tracking.

Key Takeaways

  • Face recognition technology can be used to build practical systems for attendance tracking, security access control, and more.
  • The face recognition system can be built using Python programming language and popular libraries such as OpenCV and face recognition.
  • Once the face recognition model is built, it can be integrated with an attendance system using Python programming and database management tools.
Q1. What is Haar cascade algorithm for face detection in Python?

A. Haar cascade algorithm is an open-source machine learning-based approach used for object detection in images, commonly used for face detection in Python. It works by training a classifier using positive and negative samples of the object of interest (in this case, faces) and then applying the classifier to new images to detect the object. The algorithm uses a series of features known as Haar features to distinguish between the object and the background. The algorithm is efficient and can detect faces in real-time video streams, making it a popular choice for face detection applications.

Q2. What is the difference between generic and specific classifiers in Python?

A. A generic classifier is useful for detecting a wide range of objects but may not be as accurate for detecting specific objects. A specific classifier, on the other hand, is more accurate for detecting its designated object but may not be useful for detecting other types of objects.

Q3. What is a CNN, and how is it used in facial recognition?

A. A Convolutional Neural Network (CNN) is a type of neural network commonly used for image classification and object recognition. In facial recognition, a CNN can be trained on large datasets of facial images to learn how to identify and recognize different faces.

Q4.Can Python be used for face recognition?

Python excels at face recognition. It has powerful libraries like OpenCV, Dlib, and Face Recognition for tasks like detection, recognition, and analysis. Its clear syntax and large community make it a popular choice for building face recognition systems.

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion

Responses From Readers

Clear

Anna
Anna

Interesting post, thanks for sahring.

Bukan Kucing Biasa
Bukan Kucing Biasa

Hi, Thanks for sharing! I want to ask a question, I've already tried the code above, and the program mark attendees again and again. How to mark attendees in csv file only once a day?

Margaret Wangari
Margaret Wangari

What about when creating a security management system in a school using face recognition?

Anna
Anna

Interesting post, thanks for sharing.

Ajit
Ajit

sire, may i get the full Source code in a zip file...if yes pls....provide me as soon as possible...p

Vinay kumar
Vinay kumar

Amazing post, helpful for me

prath
prath

can you share the source code zip file to me or video link or code link to me

22UCS24 ASHIKA P
22UCS24 ASHIKA P

can you please provide the source code sir?

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details