Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
LBPH and Viola Jones Algorithm
A PROJECT REPORT
By
Pavani Vulusala
Roll number:R141457
Bandi Sujeevana
Roll number:R141657
CERTIFICATE
This is to certify that the project work titled “Real Time Face Recognition of Human Faces by
using LBPH and Viola Jones Algorithm” is a bonafide project work submitted by V.Pavani,
R141457 and B.Sujeevana, R141657 in the department of ELECTRONICS AND
COMMUNICATION ENGINEERING in partial fulfillment of requirements for the award of degree
of Bachelor of Technology in Electronics and Communication engineering for the year 2018-2019
carried out the work under the supervision
The satisfaction that accompanies the successful completion of any task would be incomplete
without the mention of the people who made it possible and whose constant guidance and
encouragement crown all the efforts success.
We are extremely grateful to our respected Director, Dr.Sudarshan Rao for fostering an
excellent academic climate in our institution.
We also express my sincere gratitude to our respected Head of the Department Mr. A.
Sreekanth Reddy for his encouragement, overall guidance in viewing this project a good asset and
effort in bringing out this project.
We would like to convey thanks to our guide at college Mr. Venkateswarlu Naik for his
guidance, encouragement, co-operation and kindness during the entire duration of the course and
academics.
Our sincere thanks to all the members who helped me directly and indirectly in the completion
of project work. We express my profound gratitude to all our friends and family members for their
encouragement.
INDEX:
I.INTRODUCTION
II.METHODOLOGY
III. CODE
Although it sounds like a very simple task for us, it has proven to be a complex task for a
computer, as it has many variables that can impair the accuracy of the methods, for example:
illumination variation, low resolution, occlusion, amongst other.In computer science, face
recognition is basically the task of recognizing a person based on its facial image. It has become
very popular in the last two decades, mainly because of the new methods developed and the high
quality of the current videos/cameras.
In our social life face is the primitive center for consideration, which plays a vital
task for identification and an emotion. Live Face Recognition has attained enormously
consideration in security systems, due to its non intrusiveness, accurate and fast results etc. Real
time human Face recognition executes in two levels, such as Face detection and Face recognition.
For face detection we are using viola-jones algorithm because of its high precision and high real
time permit rate which is deployed in opencv by python language. In Face recognition two
categories are considered: training phase and evaluation phase. In training phase, the algorithm is
trained with the samples of the image to be learned and In estimation phase, the test image is
compared among all the trained samples in the dataset. The facial features is extracted with the
detected faces from live stream by Local Binary Patterns Histogram (LBPH). By using the LBPH,
face recognition is finished using Euclidean distance classifier.
I.INTRODUCTION
Face recognition has become a widespread topic of study in recent times since rise in plea
designed for security along with the swift development of mobile devices. Face recognition is
efficient to various applications such as entrance control, personality authentication, security
systems, surveillance systems unlocking of smart phone and social media networks etc. Entrance
control includes offices, computers, phones, ATMs, etc. Most of these practices at present do not
use face recognition as the standard form of conceding entry, but with advanced technologies in
computers along with suitable algorithms, facial recognition is achieving more consideration in
replacing passwords and fingerprint scanners.
In this paper face recognition for human faces are employed through Viola Jones algorithm
for face identification , LBPH in favor of feature extraction ,Euclidean distance classifier is for face
recognition. In general the steps employed to achieve this are the following: creating a dataset, face
acquisition, feature extraction, and finally classification. The entire work is done in opencv using
python.
II.METHODOLOGY
Face recognition system encompasses three main phases which are face detection, feature
extraction, face recognition.
1) Face Detection: Face acquisition and localisation from an image is detecting with Viola-Jones
Algorithm. preprocessing of human faces are separated from the objects present in an image.
2) Feature Extraction: From the detected face we are extracting the features through Local Binary
pattern Histogram. In LBPH, first we compute the local binary pattern images and then histograms
are created.
3) Face Recognition: The extracted features are fed to the classifier which recognizes or classifies
by using Machine Learning algorithm. The classifier compares the test image with the images saved
in the database can be done with supervised machine learning classifier.
facial images from a dataset with the aim to find the user that matches that face. It is
(a)
(b)
As shown in the fig 1 the proposed algorithm is used to examine the sub-window which is
capable of detecting the faces from a given input image. The proposed model deploys in two phases
such as enrollment and testing. Each phase encounters face acquisition, preprocessing, feature
extraction. After enrollment and testing phases a classifier is worn to organize whether the test
image and dataset image are matching. The standard image processing advance probable to rescale
the input image to unusual sizes and afterward run the fixed size detector during these images. This
access turns exposed to be relatively time consuming caused by the calculation of diverse size
images.
The datasets are created using the webcam or the camera attached to the computer. We take
the 100 samples per person and store it in the dataset. We can store the innumerable person’s
samples. We will give one id number to each person in the dataset.
2. Face detection
The next step is the face detection .The faces are detected by using the Viola-Jones face
detection algorithm.
i. Haar-like features
All human faces share some comparable properties. Haar like features are used to detect difference
in the black and white portion of the image. These regularities may be detected using Haar Features.
Some of the types are shown in fig 3.
We use the two rectangle Haar like feature. Some properties common to human faces are:
We take the image and convert it into 24X24 window and smear each Haar feature to that
window pixel by pixel.Each feature is related to a specific location in the sub-window. The value is
calculated by applying Haar features is
The second step of the Viola-Jones face detection algorithm is to convert an input image into an
integral image is shown in fig 4. The integral image at the location (x,y) contains the sum of the
pixels to the above and to the left of (x ,y).
(a) (b)
This makes the calculation of the addition to the entire pixels within any specified
rectangle using only four values. In the integral image, these values are the pixels that resemble
with the edges of the rectangle in the input image.
Viola-jones algorithm[5] uses a 24X24 window as the base window size to evaluate all
the features in an image. It will results in a 160,000+ features, all the features are not important to
us. So we eliminate the features which are not important.Adaboost is a machine learning algorithm
which helps in umpiring only the most outstanding features from 160,000+ features. After these
features forms a weighted arrangement of all the features which are used in gaging and deciding any
given window has face or not. These features are called weak classifiers.
The cascading classifier is an assembly of stages that contains a strong classifier. The work of this
stage is to combine the weak classifiers and extraction is shown in fig
input image detected face extracted face
3. Feature Extraction:
• Eigenfaces (1991)
• Fisherfaces (1997)
Each method has a different approach to extract the image information and perform the matching
with the input image. However, the methods Eigenfaces and Fisherfaces have a similar approach as
Step-by-Step
Now that we know a little more about face recognition and the LBPH, let’s go further and
see the steps of the algorithm:
• Radius: the radius is used to build the circular local binary pattern and represents the
radius around the central pixel. It is usually set to 1.
• Neighbors: the number of sample points to build the circular local binary pattern.
Keep in mind: the more sample points you include, the higher the computational cost.
It is usually set to 8.
• Grid X: the number of cells in the horizontal direction. The more cells, the finer the
grid, the higher the dimensionality of the resulting feature vector. It is usually set to 8.
• Grid Y: the number of cells in the vertical direction. The more cells, the finer the
grid, the higher the dimensionality of the resulting feature vector. It is usually set to
8.
The first computational step of the LBPH is to create an intermediate image that
describes the original image in a better way, by highlighting the facial characteristics.
To do so, the algorithm uses a concept of a sliding window, based on the parameters
radius and neighbors.
From the detected face, we extract the features by using Local Binary Pattern
Histogram(LBPH) is shown completely in fig 6.The LBPH feature vector, is computed which is
given below:
➢ Divide the examined window into cells (e.g. 8x8 pixels for each cell).
➢ For each pixel in a cell keeping center pixel value as the reference, compare the pixel to
each of its 8 neighbors (on its left-top, left-middle, left-bottom,right top, etc.).
➢ when the center pixel's value is greater than the neighbor's value, assign "1". Otherwise,
assign "0". This gives an 8-digit binary number (which is usually converted to decimal).
➢ Compute the histogram, over the cell, of the frequency of each "number" occurring (i.e.,
each combination of which pixels are smaller and which are greater than the center).
The extracted features are fed to the classifier. For classification we use the K-nearest
neighbor classifier. The simplest K-nearest neighbor classifier is the Euclidean distance classifier.
The Euclidean distance is calculated by comparing the test image features with features stored in
the dataset. The minimum distance value between the test image feature value and feature values
stored in the dataset gives the recognition rate.
Now, using the image generated in the last step, we can use the Grid X and Grid Y
parameters to divide the image into multiple grids, as can be seen in the following image:
Based on the image above, we can extract the histogram of each region as follows:
• As we have an image in grayscale, each histogram (from each grid) will contain only
256 positions (0~255) representing the occurrences of each pixel intensity.
• Then, we need to concatenate each histogram to create a new and bigger histogram.
Supposing we have 8x8 grids, we will have 8x8x256=16.384 positions in the final
histogram. The final histogram represents the characteristics of the image original
image.
In this step, the algorithm is already trained. Each histogram created is used to represent
each image from the training dataset. So, given an input image, we perform the steps again
for this new image and creates a histogram which represents the image.
• So to find the image that matches the input image we just need to compare two
histograms and return the image with the closest histogram.
• We can use various approaches to compare the histograms (calculate the distance
between two histograms), for example: euclidean distance, chi-square, absolute
value, etc. In this example, we can use the Euclidean distance (which is quite known)
based on the following formula:
• So the algorithm output is the ID from the image with the closest histogram. The
algorithm should also return the calculated distance, which can be used as a
‘confidence’ measurement. Note: don’t be fooled about the ‘confidence’ name, as
lower confidences are better because it means the distance between the two
histograms is closer.
• We can then use a threshold and the ‘confidence’ to automatically estimate if the
algorithm has correctly recognized the image. We can assume that the algorithm has
import cv2
cam = cv2.VideoCapture(0)
detector=cv2.CascadeClassifier('Classifiers/face.xml')
i=0
offset=50
name=input('enter your id')
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=detector.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100),
flags=cv2.CASCADE_SCALE_IMAGE)
for(x,y,w,h) in faces:
i=i+1
cv2.imwrite("dataSet/face-"+name +'.'+ str(i) + ".jpg", gray[y-offset:y+h+offset,x-
offset:x+w+offset])
cv2.rectangle(im,(x-50,y-50),(x+w+50,y+h+50),(225,0,0),2)
cv2.imshow('im',im[y-offset:y+h+offset,x-offset:x+w+offset])
cv2.waitKey(100)
if i>20:
cam.release()
cv2.destroyAllWindows()
break
FOR TRAINING
import os
import numpy as np
import cv2
from PIL import Image # For face recognition we will the the LBPH Face Recognizer
recognizer = cv2.face.createLBPHFaceRecognizer();
path='dataSet'
def getImagesWithID(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faces = []
IDs = []
for imagePath in imagePaths:
faceImg = Image.open(imagePath).convert('L')
faceNP = np.array(faceImg, 'uint8')
ID= int(os.path.split(imagePath)[1].split(".")[1])
print(ID)
faces.append(faceNP)
IDs.append(ID)
cv2.imshow("Adding faces for traning",faceNP)
cv2.waitKey(50) return faces, IDs
faces,Ids = getImagesWithID(path)
print(Ids)
recognizer.train(faces,np.array(Ids))
recognizer.save('trainer/trainer.yml')
cv2.destroyAllWindows()
FOR FACE RECOGNITION
#importing numpy &cv2 libraries
import numpy as np
import cv2
id_name=["","not matched","hiranmai","sonia"]
facecascade = cv2.CascadeClassifier('Classifiers/face.xml')
cap = cv2.VideoCapture(0)
recognizer = cv2.face.createLBPHFaceRecognizer();
recognizer.load('trainer/trainer.yml')
font = cv2.FONT_HERSHEY_SIMPLEX,5,1,0,4
while True:
ret, img =cap.read()
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=facecascade.detectMultiScale(gray, 1.2,5)
for(x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(225,0,0),2)
result=cv2.face.MinDistancePredictCollector()
recognizer.predict(gray[y:y+h,x:x+w],result,0)
ID=result.getLabel()
name=id_name[ID]
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,name,(x,y+h),font, 1, 255)
cv2.imshow('Face',img)
if cv2.waitKey(20) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
IV. RESULTS AND DISCUSSION
While creating the dataset, we assign id number to each person along with his/her name.
During recognition,if the test person is found in the dataset the classifier classifies shows the name
of the person.The feature extraction by using the LBPH , the different illuminations of faces are
captured in a certain time period.
Fig 9 shows detected image though a full picture,later in training stage the detected image is
converted into gray then system generates histogram which is easy to calculate by through this
method.
In this project face detection is carried out by using Viola-Jones face detection algorithm,
feature extraction by using Local Binary Patterns Histograms and classification by using Euclidean
distance classifier. The proposed system is implemented using Opencv and anaconda. From the
above graphs I can conclude that LBPH and Euclidean distance has better recognition rate.
On the base of analysis carried out in the present examine, the following suggestions are
accessible for further research and it is valuable to concentrate on the following issues in future
work. The proposed system is modified to support in all the conditions such as brightness, wearing
goggles, beard, in case of twins by minimum distance classifier. The proposed method is tailored in
the evolution of genetic properties for facial expressions is studied for different security metrics is
helpful in securing government confidential databases and criminal detection.
REFERENCES:-
• https://ieeexplore.ieee.org/document/8124508/references#references
• https://towardsdatascience.com/face-recognition-how-lbph-works-90ec258c3d6b