How to do Face Recognition Using LBPH Algorithm

How to do Face Recognition Using LBPH Algorithm

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.

Images & Pixel

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)

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.

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.

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.

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

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.

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.

Code:

from PIL import Image
import cv2
import numpy as np        

  • This code imports the required libraries PIL (Python Imaging Library), cv2 (OpenCV), and numpy.

import zipfile
path = 'Datasets/yalefaces.zip'
zip_object = zipfile.ZipFile(file=path, mode = 'r')
zip_object.extractall('./')
zip_object.close()        

  • This code extracts the contents of the 'yalefaces.zip' file into the current directory.

##Pre-processing the images

import os
print(os.listdir('yalefaces\\train'))        

  • This code lists the contents of the 'yalefaces/train' directory.

def get_image_data():
  paths = [os.path.join('yalefaces\\train', f) for f in os.listdir('yalefaces\\train')]
  print(paths)
  faces = []
  ids = []
  for path in paths:
    #print(path)
    image = Image.open(path).convert('L')
    #print(type(image))
    image_np = np.array(image, 'uint8')
    #print(type(image_np))
    id = int(os.path.split(path)[1].split('.')[0].replace('subject', ''))
    #print(id)
    ids.append(id)
    faces.append(image_np)

  return np.array(ids), faces        

  • This function get_image_data() loads the images from the 'yalefaces/train' directory.
  • It converts each image to grayscale ('L' mode) using PIL, converts it to a NumPy array, and extracts the ID from the image filename.
  • It returns two NumPy arrays: ids containing the IDs of the subjects and faces containing the image data.

ids, faces = get_image_data()        

This code calls the get_image_data() function to load the image data into the ids and faces variables.

##Training the LBPH classifier

lbph_classifier = cv2.face.LBPHFaceRecognizer_create(radius = 4, neighbors=14, grid_x = 9, grid_y = 9)
lbph_classifier.train(faces, ids)
lbph_classifier.write('lbph_classifier.yml')        

  • This code initializes the LBPH (Local Binary Patterns Histograms) face recognizer using cv2.face.LBPHFaceRecognizer_create() and trains it using the face images (faces) and corresponding IDs (ids).
  • The trained classifier is then saved to a file named 'lbph_classifier.yml'.

##Recognizing faces

lbph_face_classifier = cv2.face.LBPHFaceRecognizer_create()
lbph_face_classifier.read('lbph_classifier.yml')        

  • This code initializes another LBPH face recognizer and reads the trained classifier from the file 'lbph_classifier.yml'.

test_image = 'yalefaces/test/subject09.sad.gif'
image = Image.open(test_image).convert('L')
image_np = np.array(image, 'uint8')
prediction = lbph_face_classifier.predict(image_np)        

  • This code reads a test image, converts it to grayscale, and converts it to a NumPy array.
  • It then uses the LBPH face recognizer (lbph_face_classifier) to predict the ID of the subject in the test image.

expected_output = int(os.path.split(test_image)[1].split('.')[0].replace('subject', ''))
expected_output

cv2.putText(image_np, 'Pred: ' + str(prediction[0]), (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,255,0))
cv2.putText(image_np, 'Exp: ' + str(expected_output), (10, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,255,0))        

  • This code prepares the test image with prediction results.
  • It draws text on the image indicating the predicted ID ('Pred') and the expected ID ('Exp').
  • The predicted ID is retrieved from prediction[0], and the expected ID is calculated from the filename.

#To Display image
if image is not None:
    # Display the image
    cv2.imshow('image', image_np)

    # Wait for a key press and close the window
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("Error: Unable to read the image.")        

  • This code displays the test image with prediction results using OpenCV's cv2.imshow() function.
  • It waits for a key press (cv2.waitKey(0)) and closes the window when any key is pressed (cv2.destroyAllWindows()).
  • If the image loading fails, it prints an error message.


Full Code: https://github.com/TejasShastrakar/Computer_Vision.git


要查看或添加评论,请登录

Tejas Shastrakar的更多文章

社区洞察

其他会员也浏览了