Face Detection App
Face Detection App
Face Detection App
1. Introduction 2
1.1. Project Details 2
1.2. Project Purpose 2
1.3. Project Scope 2
1.4. Project Goals 3
2. Details of Tools Used 3
2.1. Introduction to OpenCV 3
2.1.1. Eigen Face 5
2.1.2. Fisher Face 6
2.1.3. HAAR Cascade Classifier 8
2.1.4. LBPH Cascade Classifier 9
3. System Analysis 12
3.1. Existing System 12
3.2. Requirements 12
3.3. Installation in Linux 12
4. System Design 13
4.1. Data Flow Diagram 13
5. Screen Shots 14
6. System Implementation 19
6.1. Face Detection and Recognition using LBPH 20
6.2. Real time Face Recognition using Webcam 23
7. System Testing 26
8. System Evaluation 26
9. Limitations and Future Enhancement 26
10. Conclusion and Bibliography 28
11. Verification Procedure 29
1|Page
1. Introduction
The requirement for reliable personal identification in computerized
access control has resulted in an in- creased interest in biometrics.
Biometrics being investigated include fingerprints, speech, signature
dynamics and face recognition. The face is the identity of a person. The
methods to exploit this physical feature have seen a great change since
the advent of image pro-cessing techniques.
The face of the human is a very complex structure and very useful in many
of social aspect in purpose of security. It needs a hard work to identify a
face correctly. Now a day Face Recognition becomes very popular
research area for the researcher. Human have a very powerful organ brain
that can recognize so many faces in a whole life. The researchers are
trying to make a system that can recognize the face in all the
circumstances. A human faces can modify because of many conditions like
increasing age, wearing a glass, have a beard, and change in hair style that
generate complexity in recognition. Although many methods have been
proposed to detect and recognize human face developing a
computational model for a large data base is still a challenging task.
• Face Detection where the picture is searched to find a face, then the
picture is processed to crop and extract the person’s face for easier
recognition.
• Face Recognition where that detected and processed face is feed into
classifier to decide who that person is.
2|Page
an image and locate the face. Face recognition is making the decision
”whose face is it ? ”, using an image database. In this project both are
accomplished using different techniques and are described below. We
will use HAAR-cascades, Eigen face, Fisher face and Local binary
pattern histogram (LBPH) algorithms. Also, we will incorporate the
real time facial detection and recognition part using Webcam.
3|Page
project was made using an OpenCV Library for Scala. The reason was
that most Face APIs are restricted to doing detection on pictures only,
whereas the project was required to have face detection done on a
live video footage to speed up the process of checking student
attendance and prevent queues before lectures.
The library has more than 2500 optimized algorithms, which includes
a comprehensive set of both classic and state-of-the-art computer
vision and machine learning algorithms. These algorithms can be used
to detect and recognize faces, identify objects, classify human actions
in videos, track camera movements, track moving objects, extract 3D
models of objects, produce 3D point clouds from stereo cameras,
stitch images together to produce a high resolution image of an entire
scene, find similar images from an image database, remove red eyes
from images taken using flash, follow eye movements, recognize
scenery and establish markers to overlay it with augmented reality,
etc. OpenCV has more than 47 thousand people of user community
and estimated number of downloads exceeding 18 million. The library
is used extensively in companies, research groups and by
governmental bodies.
4|Page
correctly. Both of these classifiers process images in gray scales,
basically because we don't need color information to decide if a
picture has a face or not.
Source: https://bit.ly/2I8PJ23
5|Page
EigenFaces recognizer trains itself by extracting principal components,
but it also keeps a record of which ones belong to which person. Thus,
whenever you introduce a new image to the algorithm, it repeats the
same process as follows:
1. Extract the principal components from the new picture.
2. Compare those features with the list of elements stored during
training.
3. Find the ones with the best match.
4. Return the ‘person’ label associated with that best match
component.
EigenFaces algorithm also considers illumination as an important
feature. In consequence, lights and shadows are picked up by
EigenFaces, which classifies them as representing a ‘face.'
Source: https://bit.ly/2I8PJ23
6|Page
Since EigenFaces also finds illumination as a useful component, it will
find this variation very relevant for face recognition and may discard
the features of the other people's faces, considering them less useful.
In the end, the variance that EigenFaces has extracted represents just
one individual's facial features.
Fisher Face Recogniser resolves this issue by extracting principle
components that differentiate one person from others. In that sense,
an individual's components do not dominate (become more useful)
over the others.
Source: https://bit.ly/2I8PJ23
7|Page
2.1.3. HAAR Cascade Classifier:
The Haar Classifier is a machine learning based approach, an algorithm
created by Paul Viola and Michael Jones; which (as mentioned before)
are trained from many many positive images (with faces) and
negatives images (without faces).
It starts by extracting HAAR features from each image as shown by the
windows below:
Source: https://bit.ly/2UxY869
Each window is placed on the picture to calculate a single feature. This
feature is a single value obtained by subtracting the sum of pixels
under the white part of the window from the sum of the pixels under
the black part of the window.Now, all possible sizes of each window
are placed on all possible locations of each image to calculate plenty
of features.
Source: https://bit.ly/2UxY869
8|Page
For example, in above image, we are extracting two features. The first
one focuses on the property that the region of the eyes is often darker
than the area of the nose and cheeks. The second feature relies on the
property that the eyes are darker than the bridge of the nose.
AdaBoost is a training process for face detection, which selects only
those features known to improve the classification (face/non-face)
accuracy of our classifier.
In the end, the algorithm considers the fact that generally: most of the
region in an image is a non-face region. Considering this, it’s a better
idea to have a simple method to check if a window is a non-face
region, and if it's not, discard it right away and don’t process it again.
So we can focus mostly on the area where a face is.
Source: https://bit.ly/2UxY869
9|Page
After you read these 0/1 values under the 3×3 window in a clockwise
order, you will have a binary pattern like 11100011 that is local to a
particular area of the picture. When you finish doing this on the whole
image, you will have a list of local binary patterns.
Source: https://bit.ly/2UxY869
After you get a list of local binary patterns, you convert each one into
a decimal number using binary to decimal conversion (as shown in
above image) and then you make a histogram of all of those decimal
values. A sample histogram looks like this:
In the end, you will have one histogram for each face in the training
data set. That means that if there were 100 images in the training data
set then LBPH will extract 100 histograms after training and store
them for later recognition. Remember, the algorithm also keeps track
of which histogram belongs to which person.
Source: https://bit.ly/2UxY869
10 | P a g e
Later during recognition, the process is as follows:
1. Feed a new image to the recognizer for face recognition.
2. The recognizer generates a histogram for that new picture.
3. It then compares that histogram with the histograms it already has.
4. Finally, it finds the best match and returns the person label associated
with that best match.
Source: https://bit.ly/2UxY869
11 | P a g e
3. System Analysis:
3.1. Existing System Configuration:
OS Windows 10/ Ubuntu
RAM 8.00 GB
3.2. Requirements:
Python 3.3+ or Python 2.7
Linux or Windows
12 | P a g e
4. System Design:
4.1. Flow Chart
13 | P a g e
5. Screenshots
5.1. Face Detection and Recognition Using LBPH
Training Model
14 | P a g e
15 | P a g e
Prediction
GUI
16 | P a g e
Dataset
17 | P a g e
Training Model
Source: https://bit.ly/2KBjYkK
19 | P a g e
6.1. Face Detection and recognition using LBPH
# Face Recognition with OpenCV
import cv2
import os
import numpy as np
face_cascade = cv2.CascadeClassifier('opencv-
files/lbpcascade_frontalface.xml')
faces = face_cascade.detectMultiScale(gray,
scaleFactor=1.2, minNeighbors=5);
if (len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
def prepare_training_data(data_folder_path):
dirs = os.listdir(data_folder_path)
faces = []
labels = []
if image_name.startswith("."):
continue;
image = cv2.imread(image_path)
cv2.imshow("Training on image...",
cv2.resize(image, (400, 500)))
cv2.waitKey(100)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()
print("Preparing data...")
faces, labels = prepare_training_data("training-
data")
print("Data prepared")
21 | P a g e
print("Total labels: ", len(labels))
face_recognizer =
cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces, np.array(labels))
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255,
0), 2)
def predict(test_img):
img = test_img.copy()
face, rect = detect_face(img)
label, confidence =
face_recognizer.predict(face)
label_text = subjects[label]
draw_rectangle(img, rect)
draw_text(img, label_text, rect[0], rect[1]-5)
return img
print("Predicting images...")
test_img1 = cv2.imread("test-data/test1.jpg")
test_img2 = cv2.imread("test-data/test2.jpg")
predicted_img1 = predict(test_img1)
predicted_img2 = predict(test_img2)
print("Prediction complete")
22 | P a g e
cv2.imshow(subjects[1], cv2.resize(predicted_img1,
(400, 500)))
cv2.imshow(subjects[2], cv2.resize(predicted_img2,
(400, 500)))
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()
faceDetect =
cv2.CascadeClassifier('haarcascade_frontalface_defau
lt.xml')
cam = cv2.VideoCapture(0)
id = input('Enter ID : ')
sampleNum = 0
while True:
ret,img = cam.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3,
5);
for (x,y,w,h) in faces:
sampleNum = sampleNum + 1
cv2.imwrite("dataset/User." + str(id) + "."
+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
cv2.rectangle(img, (x,y), (x+w, y+h),
(0,0,255), 2)
cv2.waitKey(100)
pass
cv2.imshow("Faces",img)
23 | P a g e
cv2.waitKey(1)
if sampleNum>100:
break
pass
cam.release()
cv2.destroyAllWindows()
6.2.2. Trainer.py
import os
import cv2
import numpy as np
from PIL import Image
recognizer = cv2.face.LBPHFaceRecognizer_create()
path = 'dataset'
def getImageWithID(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("training", faceNp)
cv2.waitKey(10)
return IDs, faces
24 | P a g e
6.2.3. Detector.py
import cv2
#import numpy as numpy
faceDetect =
cv2.CascadeClassifier('haarcascade_frontalface_defa
ult.xml')
cam = cv2.VideoCapture(0)
rec = cv2.face.LBPHFaceRecognizer_create()
print(rec.read('trainingData.yml'))
id = 0
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret,img = cam.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3,
5);
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h),
(0,0,255), 2)
id, conf = rec.predict(gray[y:y+h, x:x+w])
if id ==1:
cv2.putText(img,"Shailesh",(x,y+h),
font, 4,(255,255,255),2,cv2.LINE_AA)
elif id ==2:
cv2.putText(img,"Akshay",(x,y+h), font,
4,(255,255,255),2,cv2.LINE_AA)
elif id ==4:
cv2.putText(img,"Varshil",(x,y+h),
font, 4,(255,255,255),2,cv2.LINE_AA)
elif id ==5:
cv2.putText(img,"Hrithik",(x,y+h),
font, 4,(255,255,255),2,cv2.LINE_AA)
else:
cv2.putText(img,str(id),(x,y+h), font,
4,(255,255,255),2,cv2.LINE_AA)
pass
25 | P a g e
cv2.imshow("Faces",img)
if(cv2.waitKey(1) == ord('q')):
break
pass
cam.release()
cv2.destroyAllWindows()
7. System Testing:
Our system will take 100 real time photos of each person to store in
database and then the dataset of different persons will be used to train
our LBPH model which in turn create histogram for each of the images.
For Face Recognition it will take the real time photo of person from
webcam and convert it to grayscale and then matches with the available
histogram. The least difference histogram will be chosen and the name
of that histogram will be allocated to the person.
8. System Evaluation:
We are getting around 30 to 70 % of accuracy depending upon the
surrounding environment which is moderate. Also it depends on the
webcam picture quality as well.
26 | P a g e
Below is the Deference table between HAAR and LBPH Classifier:
1. Computationally
complex and slow
2. Longer Training time
3. Less Accurate in black
1. High detection Accuracy
HAAR faces
2. Low false Positive Rate
4. Limitations in difficult
lighting conditions
5. Less robust to
occlusion
1. Computationally simple
and fast
1. Less Accurate
2. Shorter training time
LBP 2. High False Positive
3. Robust to local
Rate
illumination
4. Robust to occlusion
27 | P a g e
10. Conclusion:
Face recognition technologies have been associated generally with very
costly top secure applications. Today the core technologies have evolved
and the cost of equipment is going down dramatically due to the
integration and the increasing processing power. Certain applications of
face recognition technology are now cost effective, reliable and highly
accurate.
28 | P a g e
Bhaskaracharya Institute for Space Applications and Geo-informatics
Department of Science & Technology
Government of Gujarat
Phone: 079 - 23213081 Fax: 079-23213091
29 | P a g e