Skip to content

using redis and insightface models for creating a state of the art facial recogntion attendance system

Notifications You must be signed in to change notification settings

rakibulhaque9954/face_recognition_attendance_system

Repository files navigation

Face Recognition Attendance System

This project is a mock-up of a facial recognition attendance system. It uses Redis and InsightFace models to simulate a state-of-the-art facial recognition system. This project is intended for demonstration and learning purposes only.

Overview

The system uses InsightFace models for face detection and recognition. InsightFace is a deep learning toolkit that provides several pre-trained models for face detection, face recognition, and facial attribute analysis. The system uses these models to detect faces in real-time and recognize them based on a database of known faces.

The recognized faces are then used to mark attendance in a Redis database. Redis is an in-memory data structure store that is used for its high performance and flexibility. In this system, it is used to store the attendance records, which can be accessed and updated in real-time.

Here's a snippet from the fr.py file that shows how the face recognition is done:

def face_prediction(image, df):
    faces = model_l.get(image)
    for face in faces:
        box, prob, landmarks = face.bbox.astype(np.int), face.prob, face.landmark.astype(np.int)
        face = np.squeeze(cv2.resize(image[box[1]:box[3], box[0]:box[2]], (112, 112)))
        emb = model.get_embedding(face).flatten()
        name = 'Unknown'
        role = 'Unknown'
        if len(df) > 0:
            sim = df['Embedding'].apply(lambda x: cosine_similarity([emb], [x])[0][0])
            idx = np.argmax(sim)
            if sim[idx] > 0.4:
                name = df.loc[idx, 'Name']
                role = df.loc[idx, 'Role']
        cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 2)
        cv2.putText(image, name + ', ' + role, (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
    return image

This function takes an image and a dataframe as input. It performs face detection on the image using a model (model_l), and then for each detected face, it computes the bounding box coordinates. If the cosine similarity of the detected face and the faces in the dataframe is above a certain threshold, it assigns the name and role from the dataframe to the detected face. If no match is found, it assigns 'Unknown' to both the name and role.

Working

Cosine similarity is a measure of similarity between two non-zero vectors of an inner product space. It is defined to equal the cosine of the angle between them, which is also the same as the inner product of the same vectors normalized to both have length 1.

In the context of facial recognition, cosine similarity is often used to compare the similarity between two face embeddings. A face embedding is a vector that represents the features of a face. These embeddings are usually generated by a neural network trained on a large dataset of faces.

Here's a step-by-step explanation of how cosine similarity is used in facial recognition:

  1. Face Detection: The first step in facial recognition is detecting faces in an image. This is usually done using a pre-trained model that can identify faces in an image.

  2. Face Embedding: Once a face is detected, the next step is to generate an embedding for the face. This is done using a neural network that has been trained to generate a vector of features for each face. The output is a high-dimensional vector (e.g., 128 dimensions) that represents the unique features of the face.

  3. Comparison: To recognize a face, its embedding is compared with the embeddings of known faces. This is where cosine similarity comes in. The cosine similarity between the embedding of the unknown face and the embeddings of the known faces is calculated. The cosine similarity is a value between -1 and 1, where 1 means the vectors are identical, 0 means the vectors are orthogonal (not similar), and -1 means the vectors are diametrically opposed (very dissimilar).

  4. Recognition: The unknown face is recognized as the known face with which it has the highest cosine similarity, provided that the similarity is above a certain threshold. If the highest cosine similarity is below the threshold, the face is marked as unknown.

This is the way i made the system instead of relying on CNN's, this way provide a faster inference best for real-time systems which was done by leveraging insightface model size large(model_l).

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

Before you begin, ensure you have met the following requirements:

  • You have installed Python 3.8 or higher.
  • You have a Windows/Linux/Mac machine.
  • You have installed Redis.
  • You have installed the InsightFace library.

Installing

To install the project, follow these steps:

Clone the repository to your local machine. Navigate to the project directory. Install the required Python packages using pip:
pip install -r requirements.txt

Behind the Scenes

In the parent directory you will find different notebooks and the backbone of this project.

About

using redis and insightface models for creating a state of the art facial recogntion attendance system

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published