Face Recognition Using LBPH Algorithm

 This article will help readers those are having the interest to learn face recognition at the beginner level. Generally, Face recognition describes a biometric technology that recognizes when a human face is present. It actually attempts to establish the identity of the person whose face it is. The process works using a computer application that captures a digital image of an individual’s face (sometimes taken from a video frame) and compares it to images in a dataset While facial recognition is not a100% accurate, it can very accurately determine when there is a strong chance that a person’s face matches someone in the database…

Fig 1.1 General Face recognition structure

Fig1.1 shows the input image that means captured by the camera that will detect the faces using face detection algorithm and convert the original image into a grayscale image for feature extraction. then we match the input image and current image by using verification and identification technique. we get good recognition result. There are different types of face recognition algorithms, for example:

  • Eigenfaces
  • Local Binary Patterns Histograms (LBPH) (1996)
  • Fisherfaces
  • Scale Invariant Feature Transform (SIFT)
  • Speed Up Robust Features (SURF)

Local Binary Patterns Histograms Algorithm (LBPH) :

It is a simple solution for the face recognition problem, which can be recognizing both the front face and side face.LBP is a simple and efficient text description operator which labels the pixels of an image by thresholds the neighborhood of each pixel and which produce the result as a binary number. Then the LBP combined with histogram.we can represent the face images with a simple data vector.

Steps to calculate Local Binary Pattern : 

  • Divide the examined window into cells.
  • For each pixel in a cell,(3X3 pixels) compare the pixel to each of its 8 neighbors (left-top, left-middle, left-bottom and right-top, etc.)Follow the pixels along a circle, i.e. clockwise or counterclockwise. 
  • If the center pixel’s value is greater than the neighbor’s value, then write “1”. otherwise, write “0”. ( eg :250>90 = 1, 90<50 =0 likewise find out the value for each pixels).
  • It gives an 8-digit binary number and which is usually converted to decimal for our convenience. (i.e 10001101 converted into 141).
  • We should find the Computation of histogram for over the cell of the frequency of each “number” occurring and each combination of which pixels are smaller and which are greater than the center). Then normalize the histogram. 
  • We have to Concatenate (normalized) histograms of all cells. its gives the feature vector for the window. 
Fig 1.2 Workflow of LBPH

Code Implementation :

  The LBPH is provided by the OpenCV library.  We have used OpenCV which presents a Haar cascade classifier which is used for face detection. The Haar cascade classifier uses the LBPH algorithm.

Let see the implementation of face recognition in a step by step. 

Fig 1.3 code implementation process

Step 1: Preparing the Dataset: 

Firstly, we have to capture multiple Faces from multiple users to be stored on a DataBase (dataset directory). Faces will be stored on a directory/(if does not exist, please create one) Its create at least 30 sample images of each person. Each face will have a unique numeric integer ID such as 1, 2, 3, etc. The saved pictures are stored in the same folder. we should Make different facial expressions and postures to a scene and detect faces.
Here, The code for preparing the dataset:

Output:

Join our weekly newsletter to receive:

  1. Latest articles & interviews
  2. AI events: updates, free passes and discount codes
  3. Opportunities to join AI Time Journal initiatives


import cv2
import os
 
cam = cv2.VideoCapture(0)
cam.set(3, 640)
cam.set(4, 480)
face_detector = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)
 
face_id = input(‘\n enter user id end press <return> ==>  ‘)
 
print(“\n [INFO] Initializing face capture. Look at the camera and wait …”)
count = 0
 
while(True):
 
ret, img = cam.read()
img = cv2.flip(img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
 
for (x,y,w,h) in faces:
 
        cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
     count += 1
 
     cv2. imwrite(“dataset/User.” + str(face_id) + ‘.’ + str(count) + “.jpg”, gray[y:y+h,x:x+w])
 
     cv2.imshow(‘image’, img)
 
k = cv2.waitKey(100) & 0xff # Press ‘ESC’ for exiting video
if k == 27:
     break
elif count >= 30:
      break
 
print(“\n [INFO] Exiting Program and cleanup stuff”)
cam.release()
cv2.destroyAllWindows()
 


  Fig 1.4The sample images(dataset)

Step 2: Training

After getting the dataset we have to train the  Multiple Faces stored on a Database by using Algorithm.




import cv2
import numpy as np
from PIL import Image
import os

path = ‘dataset’

recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier(“haarcascade_frontalface_default.xml”);

def getImagesAndLabels(path):

    imagePaths = [os.path.join(path,f) for f in os.listdir(path)]     
    faceSamples=[]
    id= []

    for imagePath in imagePaths:

        PIL_img = Image.open(imagePath).convert(‘L’) # convert it to grayscale
        img_numpy = np.array(PIL_img,’uint8′)

        id = int(os.path.split(imagePath)[-1].split(“.”)[1])
        faces = detector.detectMultiScale(img_numpy)

        for (x,y,w,h) in faces:
            faceSamples.append(img_numpy[y:y+h,x:x+w])
            ids.append(id)

    return faceSamples,ids

print (“\n [INFO] Training faces. It will take a few seconds. Wait …”)
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))

recognizer.write(‘trainer/trainer.yml’)

print(“\n [INFO] {0} faces trained. Exiting Program”.format(len(np.unique(ids))))

Output:

>>> 
RESTART: C:\Users\shaam\Desktop\FacialRecognition\02_face_training.py 
 [INFO] Training faces. It will take a few seconds. Wait …
 [INFO] 1 faces trained. Exiting Program
>>> 

Step 3: Recognition


import cv2
import numpy as np
import os 

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(‘trainer/trainer.yml’)
cascadePath = “haarcascade_frontalface_default.xml”
faceCascade = cv2.CascadeClassifier(cascadePath);

font = cv2.FONT_HERSHEY_SIMPLEX

id = 0
names = [‘None’, ‘priya’, ‘kathir’, ‘Ilza’, ‘Z’, ‘W’] 

cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height

minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)

while True:

    ret, img =cam.read()
    img = cv2.flip(img, 1)

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale( 
        gray,
        scaleFactor = 1.2,
        minNeighbors = 5,
        minSize = (int(minW), int(minH)),
     )

     
    for(x,y,w,h) in faces:

        cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,255), 2)

        id, confidence = recognizer.predict(gray[y:y+h,x:x+w])

        if (confidence < 100):
            id = names[1]
            id2=name[2]
            con=confidence
            confidence = ”  {0}%”.format(round(100 – confidence))
            if(id==’priya’ and con<60):
       cv2.putText(img, ‘shaam’, (x+5,y+25), font, 1, (0,0,255), 1)
        else:
            id = “unknown”
            confidence = ”  {0}%”.format(round(100 – confidence))
        
        cv2.putText(img, str(id), (x+5,y-5), font, 1, (255,255,255), 2)
        cv2.putText(img, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1)  
    
    cv2.imshow(‘camera’,img) 

    k = cv2.waitKey(10) & 0xff # Press ‘ESC’ for exiting video
    if k == 27:
        break

print(“\n [INFO] Exiting Program and cleanup stuff”)
cam.release()
cv2.destroyAllWindows()

Output:

Fig 1.5 Recognition Result

Conclusion :

Nowadays, Face recognition plays a major role in computer vision and having much importance in many applications such as security, surveillance, banking, marketing and so on. In this article, I have shared about a basic face recognition using LBPH algorithm.   LBPH algorithm is the simplest and best for face recognition for beginners.


Opinions expressed by contributors are their own.

About Shanmugapriya Balamurugan

Editorial Staff Intern Pandian Saraswathi Yadav Engineering College, Interested in Machine Learning and Data science.

View all posts by Shanmugapriya Balamurugan →