Building a Real-Time Face Recognition and Attendance System Using OpenCV, Face Recognition, and Google Text-to-Speech

In today's world, face recognition technology has rapidly evolved, making its way into various applications from security systems to mobile devices. One interesting use case is in automated attendance systems, where the time-consuming process of manually taking attendance can be eliminated with the power of facial recognition. In this blog post, we will walk through the steps of building a real-time face recognition and attendance system using OpenCV, the face_recognition library, and Google Text-to-Speech (gTTS).

Project Overview

The goal of this project is to create a system that can recognize faces in real-time using a webcam, identify the person, and then mark their attendance by writing their name and the time they were recognized to a CSV file. Additionally, the system will use text-to-speech technology to announce the name of the recognized person.

Prerequisites

Before we dive into the code, let's make sure you have the necessary tools and libraries installed:

  • Python
  • OpenCV
  • face_recognition
  • gTTS

You can install these libraries using pip:

pip install opencv-python face-recognition gtts

Step 1: Preparing the Training Images

The first step is to gather a set of training images. These images should contain the faces of the individuals you want to recognize. Store these images in a folder named training images. Each image file should be named after the person it contains, as this name will be used to identify the person during recognition.

import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime
from gtts import gTTS
import time

Step 2: Loading and Encoding the Images

The next step is to load the training images and convert them into a format that the face_recognition library can use. This involves reading each image, converting it from BGR (the default format used by OpenCV) to RGB, and then generating a face encoding for each image.

path = 'training images'  # Path to the images folder
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])

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

encodeListKnown = findEncodings(images)

Step 3: Marking Attendance

Once a face is recognized, we need to record the attendance by writing the person’s name and the current time to a CSV file. This is done by checking if the person has already been marked for the day, and if not, appending their information to the file. We also use the gTTS library to announce the name of the person as they are recognized.

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()
            dtString1 = now.strftime('%H:%M:%S')
            dtString2 = now.strftime('%A')
            dtString3 = now.strftime('%d:%m:%Y')
            f.writelines(f'\n{name},{dtString1},{dtString2},{dtString3}')

            # Speak the recognized name
            language = 'en'
            output = gTTS(text=f'hello {name}', lang=language, slow=False)
            output.save("output.mp3")
            os.system("start output.mp3")
            time.sleep(3)
            os.remove('output.mp3')

Step 4: Capturing and Processing Video

The main loop of the program captures frames from the webcam, processes them to detect faces, and then compares these detected faces with the known encodings. If a match is found, the person's name is displayed on the video feed, and their attendance is marked.

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)
    
    facesCurFrame = face_recognition.face_locations(imgS)
    encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
    
    for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
        matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
        faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
        matchIndex = np.argmin(faceDis)
        
        if matches[matchIndex]:
            name = classNames[matchIndex]
            y1, x2, y2, x1 = faceLoc
            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-6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
            markAttendance(name)
    
    cv2.imshow('my video', img)
    if cv2.waitKey(13) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

Step 5: Running the Program

Finally, you can run the program and test it with different faces. Make sure to have your webcam set up and properly configured. When you run the script, it will start capturing video and recognizing faces in real-time. Ensure you have the training images folder with the correct images before running the program.

python face_recognition_attendance.py

Conclusion

This real-time face recognition and attendance system is a great way to explore computer vision and machine learning technologies. By integrating OpenCV, face_recognition, and gTTS, you can build a practical application that demonstrates the power of these technologies. Experiment with the code and adapt it to your own needs!

View on GitHub