Understanding Face Recognition Using LBPH Algorithm

Parth Last Updated : 21 Oct, 2024
8 min read

LBPH (Local Binary Pattern Histogram) is a Face-Recognition algorithm it is used to recognize the face of a person. It is known for its performance and how it is able to recognize the face of a person from both front face and side face.

Before starting the intuition behind the LBPH algorithm, let’s first understand a little bit about the basics of Images and pixels to understand how images are represented before we start the content about face recognition. So, let’s get started understanding images and pixels.

LBPH (Local Binary Pattern Histogram)

Learning Objectives

  1. Understand the intuition behind the LBPH (Local Binary Patterns Histograms) algorithm for face recognition, which involves analyzing pixel patterns and creating histograms for image representation.
  2. Learn about the representation of images using pixels and matrices, along with the basics of images, pixels, and colour channels.
  3. Explore the concept of histograms in statistics and their application in the LBPH algorithm for counting colour occurrences in image squares.
  4. Gain insights into implementing the LBPH algorithm for face recognition using Python and OpenCV, including data gathering, cleaning, model training, and face recognition.
  5. Understand the process of testing the LBPH face recognition model on test images and interpreting the model predictions and expected outputs.

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

Images & Pixel

Face Recognition using LBPH algorithm

All images are represented in the Matrix formats, as you can see here, which are composed of rows and columns. The basic component of an image is the pixel. An image is made up of a set of pixels. Each one of these is small squares. By placing them side by side, we can form the complete image. A single pixel is considered to be the least possible information in an image. For every image, the value of pixels ranges between 0 to 255.

This image here is 32 pixels wide and 32 pixels high.

And when we multiply 32 by 32, the result is 1024, which is the total number of pixels in the image. Each pixel is composed of Three values are R, G, and B, which are the basic colours red, green, and blue. The combination of these three basic colours will create all these colours here in the image so we conclude that a single pixel has three channels, one channel for each one of the basic colours.

As now we have some understanding of images and pixels, now it will be easier to understand the intuition behind the LBPH algorithm. So let’s get started with the intuition of the LBPH algorithm.

LBPH(Local Binary Patterns Histograms)

LBPH(Local Binary Patterns Histograms)

Let’s start by analyzing a matrix representing a piece of the image. As you learned earlier, an image is represented in these formats. In this example, we have three rows and three columns, and the total number of pixels is nine. Select the central pixel here, value eight, and apply a condition. If the value is greater or equal to 8, the result is ‘1’ otherwise, if the value is less than eight, the result is zero. After applying the conditioner, the matrix will now look like this.

LBPH(Local Binary Patterns Histograms)

The basic calculation of this algorithm is to apply this condition, selecting the centre element of the matrix. Now, we need to generate a binary value. Binary value = 11100010. The algorithm will start applying the condition from the top left corner element that goes up to the 1 element of the 2nd row, thinking it is making a circle like this.

LBPH(Local Binary Patterns Histograms)

After converting the Binary value to the decimal value, we get Decimal Value = 226. It indicates that all these pixels around the central value equal 226.

This algorithm is robust when it comes to lightning. If you put a flashlight on the image, the value of the pixels will increase. The higher the values, the brighter the image; when values are lower, the darker the image will be. For this reason, this algorithm has good results in light and dark images because when the image becomes lighter or darker, all the pixels in the neighborhood here will be changed. After putting the light on the image, the matrix will look like this. After applying the above condition, we will get the binary value the same as above i.e, 11100010.

LBPH(Local Binary Patterns Histograms)

Let’s consider this another image here. To better understand how the algorithm will recognize a person’s face.

LBPH(Local Binary Patterns Histograms)

We have the image of a face here, and the algorithm will create several squares, as you can see here. And within each one of these squares, we have the representation of the previous light. For example, this square here does not represent only one pixel but is set with multiple pixels: three rows and four columns. Three by four is equal to twelve pixels in total in these squares. Here, in each of these squares, there are twelve pixels. Then, we apply that condition to each one. Considering the central pixel.

The next step is to create a histogram, which is a concept of statistics that will count how many times each color appears in each square. This is the representation of the histogram.

LBPH(Local Binary Patterns Histograms)

For example, if the value 110 appears 50 times a bar like this will be created with this size equal to 50; if 201 appears 110 times, the other bar will be created in this histogram with this size equal to 100. Based on the comparison of the histograms, the algorithm will be able to identify the edges and edges of the images. For example, we don’t have information about the person’s face in this first square here. So, the histogram will be different from this other square that has the border of the face. In short, the algorithm knows which histograms represent borders and which histograms represent the person’s main features, such as the colour of the eye, the shape of the mouth, and so on.

So this is the basic theory of this algorithm, which is based on the creation and comparison of histograms.

Now Let’s Start the Coding Part

Note:- If you are facing an error while importing the cv2 library like “No module named ‘cv2.cv2”. Then, you can write code in Google Colab. I have written this code in Google Colab.

I will be using the Yaleface dataset

Importing Libraries

import os
import cv2

import zipfile

import numpy as np

from google.colab.patches import cv2_imshow

Data Gathering

# Data Gathering
# Extracting face images from the provided dataset for further processing in the facial recognition system.
path = "/content/drive/MyDrive/Datasets/yalefaces.zip"
zip_obj = zipfile.ZipFile(file = path,mode='r')
zip_obj.extractall('./')
zip_obj.close()

Data Cleaning

Before giving data to the model these images are in .gif format so we need order to convert them into ndarray so we need to use the following code:

# Preprocessing the images by converting them into suitable formats for feature extraction and model training.
from PIL import Image

def get_image_data() :
                  paths = [os.path.join("/content/yalefaces/train",f)for f in os.listdir(path="/content/yalefaces/train")]
                  # faces will contain the px of the images
                   # path will contain the path of the images
 
                  faces = []
                  ids = []
                  for path in paths :
                     #image processing
                      #input image
                      image = Image.open(path).convert('L')
                      image_np = np.array(image,'uint8')
                      id = int(os.path.split(path)[1].split(".")[0].replace("subject"," "))
                      ids.append(id)
                      faces.append(image_np)
                    
                    return np.array(ids),faces
ids , faces = get_image_data()

Model Training

This process involves extracting facial features from the images and training the classifier to recognize patterns in these feature vectors that are associated with different individuals.

# Initialize LBPH (Local Binary Pattern Histogram(lbp))  face recognizer object using OpenCV's computer vision library
lbph_classifier = cv2.face.LBPHFaceRecognizer_create()

# Train the LBPH classifier using the provided face images (faces) and corresponding labels (ids)
lbph_classifier.train(faces,ids)

# Below line will store the histograms for each one of the images
lbph_classifier.write('lbph_classifier.yml')

Recognizing Faces

#Utilizing the trained model for real-time facial recognition on a test image in the Python environment.

lbph_face_classifier = cv2.face.LBPHFaceRecognizer_create()
lbph_face_classifier.read("/content/lbph_classifier.yml")

#Now we will check the performance of the model by predicting on a test image

test_image = "/content/yalefaces/test/subject03.leftlight.gif"
image = Image.open(test_image).convert('L')
image_np = np.array(image,'uint8')

# Before giving the image to the model, let's visualize it first
cv2_imshow(image_np)
# Using the trained model to predict identity of the person in the test image
predictions = lbph_face_classifier.predict(image_np)
print(predictions)

# Retrieving the expected output (ground truth) from the test image file name
expected_output = int(os.path.split(test_image)[1].split('.')[0].replace("subject"," "))
print(expected_output)
3<-That's our output
Face Recognition using LBPH algorithm

This is the image we will be testing

Face Recognition using LBPH algorithm

The First parameter gives the face is detected, 2nd the parameter gives the confidence. This is the output we get after from print(predictions)

# Displaying the predicted(face detection) and expected outputs for comparison
cv2.putText(image_np, 'Pred.' +str(predictions[0]),(10,30),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,0))
cv2.putText(image_np, 'Expec.' +str(expected_output),(10,50),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,0)
)
cv2_imshow(image_np)

Final Result

Face Recognition using LBPH algorithm

Conclusion

In this article, we covered the following:-

  • What is the LBPH algorithm?
  • How LBPH algorithm recognize face and do calculations
  • Understand code how to recognize face using LBPH algorithm

Key Takeaways from LBPH ALgorithm

  1. LBPH is a robust face recognition algorithm known for its ability to recognize faces from both front and side angles.
  2. Images are represented in matrix format, with pixels containing colour information represented by intensity values ranging from 0 to 255.
  3. LBPH algorithm applies binary conditions to pixel neighbourhoods to generate binary patterns, which are then converted into histograms for image representation.
  4. Implementation of the LBPH algorithm in Python involves data gathering, cleaning, model training, and testing on test images to evaluate performance.
  5. The LBPH face recognition model outputs predictions showing the recognized face and its confidence level. This provides insights into the algorithm’s effectiveness in accurately recognizing faces.

Frequently Asked Questions

Q1. Why Use LBPH for Facial Recognition?

Ans. LBPH (Local Binary Pattern Histogram) is preferred for facial recognition due to its effectiveness in facial recognition, artificial intelligence, and biometric applications. It extracts facial features through pattern recognition, employing a descriptor that efficiently captures the unique characteristics of faces. LBPH excels in detecting faces and performing face identification by analyzing local image patterns, making it a robust choice for biometric authentication and pattern recognition tasks in artificial intelligence applications.

Q2. How does the LBPH algorithm handle variations in lighting and facial expressions during face recognition?

Ans. LBPH incorporates the LBP operator to capture local patterns and gradients, enabling it to handle variations in lighting and facial expressions effectively. This, combined with dimensionality reduction techniques and the Haar cascade classifier, ensures robustness and accuracy in real-time face recognition and object detection.

Q3. Which algorithm is best for face recognition?

Ans. Deep learning algorithms, particularly convolutional neural networks and object detection like YOLO, are considered the best for face recognition tasks due to their ability to learn complex patterns and features.

Q4. What are the current challenges and limitations of face detection and recognition research?

Ans. Challenges in face detection and recognition research include limitations in handling diverse human face appearances, scalability issues in large attendance systems, and constraints of methods like Eigenfaces. Techniques such as Histogram of Oriented Gradients (HOG) and grayscale image processing show promise but face challenges in real-world scenarios. Further research is needed to enhance the robustness of these systems in computer science, as highlighted by various studies (DOI).

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

Hello! I'm Parth, a data science enthusiast with a passion for leveraging the latest technologies to solve real-world problems. As an accomplished data scientist with expertise in ML, DL, Computer Vision, NLP, and more, I've had the pleasure of working on a wide range of projects across different industries.

My goal is to use my skills and knowledge to make a positive impact on society, particularly in the areas of healthcare and sustainability. I've written several technical blogs on Medium and Analytics Vidhya, showcasing my expertise in data science and technology.

Responses From Readers

Clear

kani khan
kani khan

wow man awesome explanation but the above figure binary value is wrong can you please correct it this is the value you entered (10001101 ) but the correct form of the binary value is (10011010) and when we convert this binary value value into decimal value the answer is 154 but you mentioned 141

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