Face Recognition Using Python
Face Recognition Using Python
Face Recognition Using Python
A PROJECT REPORT
Submitted by
BACHELOR OF ENGINEERING
IN
Certified that this project report “Face Recognition Using Python.” is the
bonafide work of “Rajat Maheshwari (20BCS7569) , Sameer Saifi
(20BCS7619) , Sahil Soni (20BCS5412)” who carried out the project work
under my/our supervision.
2|Page
ACKNOWLEDGEMENT
I would like to express my sincere thanks to all those individuals who assisted
me in completing this End Semester Project. The project could not be going
smoothly without the help of these people.
3|Page
ABSTRACT
The face is one of the easiest ways to distinguish the individual identity of each
other. Face recognition is a personal identification system that uses personal
characteristics of a person to identify the person's identity. Human face
recognition procedure basically consists of two phases, namely face detection,
where this process takes place very rapidly in humans, except under conditions
where the object is located at a short distance away, the next is the introduction,
which recognize a face as individuals. Stage is then replicated and developed as
a model for facial image recognition (face recognition) is one of the much-
studied biometrics technology and developed by experts. There are two kinds of
methods that are currently popular in developed face recognition pattern
namely, Eigenface method and Fisherface method. Facial image recognition
Eigenface method is based on the reduction of facedimensional space using
Principal Component Analysis (PCA) for facial features. The main purpose of
the use of PCA on face recognition using Eigen faces was formed (face space)
by finding the eigenvector corresponding to the largest eigenvalue of the face
image. The area of this project face detection system with face recognition is
Image processing. The software requirements for this project is python
software.
Keywords: face detection, Eigen face, PCA, python
Extension: There are vast number of applications from this face detection
project, this project can be extended that the various parts in the face can be
detect which are in various directions and shapes.
4|Page
INDEX
5|Page
Chapter-1
Introduction
6|Page
1.2 FACE DETECTION:
Face detection involves separating image windows into two classes; one
containing faces (tarning the background (clutter). It is difficult because
although commonalities exist between faces, they can vary considerably in
terms of age, skin colour and facial expression. The problem is further
complicated by differing lighting conditions, image qualities and geometries, as
well as the possibility of partial occlusion and disguise. An ideal face detector
would therefore be able to detect the presence of any face under any set of
lighting conditions, upon any background. The face detection task can be
broken down into two steps. The first step is a classification task that takes
some arbitrary image as input and outputs a binary value of yes or no, indicating
whether there are any faces present in the image. The second step is the face
localization task that aims to take an image as input and output the location of
any face or faces within that image as some bounding box with (x, y, width,
height). The face detection system can be divided into the following steps:-
c) Localization: The trained neural network is then used to search for faces
in an image and if present localize them in a bounding box. Various
Feature of Face on which the work has done on:- Position Scale
Orientation Illumination
7|Page
1.3 What Is A Facial Recognition Attendance System?
8|Page
1.6 How Facial Recognition Works?
Almost every big tech company including Amazon, Google, Microsoft, and
Cisco is leading the effort to make face recognition more mainstream. There are
many reasons to adopt the technology.
9|Page
Chapter-2
Literature survey
Face detection is a computer technology that determines the location and size of
human face in arbitrary (digital) image. The facial features are detected and any
other objects like trees, buildings and bodies etc are ignored from the digital
image. It can be regarded as a specific‘ case of object-class detection, where the
task is finding the location and sizes of all objects in an image that belong to a
given class. Face detection, can be regarded as a more general‘ case of face
localization. In face localization, the task is to find the locations and sizes of a
known number of faces (usually one). Basically there are two types of
approaches to detect facial part in the given image i.e. feature base and image
base approach. Feature base approach tries to extract features of the image and
match it against the knowledge of the face features. While image base approach
tries to get best match between training and testing images.
10 | P a g e
2.1 FEATURE BASE APPROCH:
Active Shape Model Active shape models focus on complex non-rigid features
like actual physical and higher level appearance of features Means that Active
Shape Models (ASMs) are aimed at automatically locating landmark points that
define the shape of any statistically modelled object in an image. When of facial
features such as the eyes, lips, nose, mouth and eyebrows. The training stage of
an ASM involves the building of a statistical
b. Snakes: The first type uses a generic active contour called snakes, first
introduced by Kass et al. in 1987 Snakes are used to identify head
boundaries [8,9,10,11,12]. In order to achieve the task, a snake is first
initialized at the proximity around a head boundary. It then locks onto
nearby edges and subsequently assume the shape of the head. The
evolution of a snake is achieved by minimizing an energy function,
Esnake (analogy with physical systems), denoted asEsnake = Einternal +
EExternal Where Einternal and EExternal are internal and external
energy functions. Internal energy is the part that depends on the intrinsic
properties of the snake and defines its natural evolution. The typical
natural evolution in snakes is shrinking or expanding. The external
energy counteracts the internal energy and enables the contours to deviate
from the natural evolution and eventually assume the shape of nearby
features—the head boundary at a state of equilibria. Two main
consideration for forming snakes i.e. selection of energy terms and energy
minimization. Elastic energy is used commonly as internal energy.
Internal energy is vary with the distance between control points on the
snake, through which we get contour an elastic-band characteristic that
causes it to shrink or expand. On other side external energy relay on
image features. Energy minimization process is done by optimization
techniques such as the steepest gradient descent. Which needs highest
computations. Huang and Chen and Lam and Yan both employ fast
iteration methods by greedy algorithms. Snakes have some demerits like
contour often becomes trapped onto false image features and another one
is that snakes are not suitable in extracting non convex features.
11 | P a g e
2.2 LOW LEVEL ANALYSIS:
Based on low level visual features like color, intensity, edges, motion etc. Skin
Color BaseColor is a vital feature of human faces. Using skin-color as a feature
for tracking a face has several advantages. Color processing is much faster than
processing other facial features. Under certain lighting conditions, color is
orientation invariant. This property makes motion estimation much easier
because only a translation model is needed for motion estimation. Tracking
human faces using color as a feature has several problems like the color
representation of a face obtained by a camera is influenced by many factors
(ambient light, object movement, etc
Majorly three different face detection algorithms are available based on RGB,
YCbCr, and HIS color space models. In the implementation of the algorithms
there are three main steps viz.
Crowley and Coutaz suggested simplest skin color algorithms for detecting skin
pixels. The perceived human color varies as a function of the relative direction
to the illumination.
The pixels for skin region can be detected using a normalized color histogram,
and can be normalized for changes in intensity on dividing by luminance.
Converted an [R, G, B] vector is converted into an [r, g] vector of normalized
color which provides a fast means of skin detection. This algorithm fails when
there are some more skin region like legs, arms, etc. Cahi and Ngan [27]
suggested skin color classification algorithm with YCbCr color space. Research
found that pixels belonging to skin region having similar Cb and Cr values. So
that the thresholds be chosen as [Cr1, Cr2] and [Cb1, Cb2], a pixel is classified
to have skin tone if the values [Cr, Cb] fall within the thresholds. The skin color
distribution gives the face portion in the color image. This algorithm is also
having the constraint that the image should be having only face as the skin
region. Kjeldson and Kender defined a color predicatein HSV color space to
separate skin regions from background. Skin color classification in HSI color
space is the same as YCbCr color space but here the responsible values are hue
(H) and saturation (S). Similar to above the threshold be chosen as [H1, S1] and
[H2, S2], and a pixel is classified to have skin tone if the values [H,S] fall
12 | P a g e
within the threshold and this distribution gives the localized face image. Similar
to above two algorithm this algorithm is also having the same constraint.
These algorithms aim to find structural features that exist even when the pose,
viewpoint, or lighting conditions vary, and then use these to locate faces. These
methods are designed mainly for face localization
13 | P a g e
2.5 CONSTELLATION METHOD
All methods discussed so far are able to track faces but still some issue like
locating faces of various poses in complex background is truly difficult. To
reduce this difficulty investigator form a group of facial features in face-like
constellations using more robust modelling approaches such as statistical
analysis. Various types of face constellations have been proposed by Burl et al. .
They establish use of statistical shape theory on the features detected from a
multiscale Gaussian derivative filter. Huang et al. also apply a Gaussian filter
for pre-processing in a framework based on image feature analysis. Image Base
Approach.
14 | P a g e
2.7 OBJECTIVE:
15 | P a g e
Chapter-3
Design flow/Process
a) Single entity: A good team is the first requirement for this project so that the
system should have good user experience. We worked together as a team to
bring this project to fruition as a single entity. We have team of three
members mentioned above. All of us worked hard on this project still
working to make our project user-friendly and attractive as much we can.
b) Life Span: This project has the life span of 4 months from February to May.
The project is divided in two steps. First we will develop the user interface
using different modules and libraries of python. In the next step the project is
tested and reviewed and hosting is to be done.
c) Life Cycle: Since this is a Face Recognition system that combines facial
expression technologies to come up with a real-time intelligent system we
have seen the need to add collaborative methodology to it because it isn’t
enough for the face recognition to work alone in a collaborative system. This
system is developed in a way that it will be able to detect the person’s facial
expression.
To create a complete project on Face Recognition, we work on 3 very distinct
phases:
o Face Detection and Data Gathering
o Train the Recognizer
o Face Recognition
d) Team Spirit: We as team had team spirit from the beginning, from project
selection to learning to implementation. We put our faith in each other to
complete the task at hand, we aided each other as needed. When working on a
16 | P a g e
project as part of a group, team spirit, trust and passion are all important
factors to consider.
e) Risk and Uncertainty: Our project is well defined, but still there remains a
chance of error or uncertainty. Like there may be error in the face detection
and can lead to wrong results. There may be some malfunctioning in the
equipments used resulting in error in results. There may be something's which
lead to errors in the result. But still, even after hard work and hours of
thinking, errors may remain.
a. Cost :
What is the cost of developing the face recognition software from scratch?
And the answer is that it depends on the project requirements. As There are
many variables that comprise the total price of the bespoke face recognition
software developed.
To offer the client the best solution to meet challenges, we enters such
projects with the Investigation Stage aimed at the following:
17 | P a g e
b. Risk :
Facial recognition technology also carries other risks, such as errors, which
could, for example, lead to users being charged incorrectly. And as with any
AI system, we should be concerned about whether the algorithms and data
sets are free from bias, and have clean, complete and representative training
data.
c. Benefit :
Facial recognition is a technology that can benefit society, including
increasing safety and security, preventing crimes, and reducing human
interaction. Here are some pros of facial recognition:
d. Quality :
In ideal conditions, facial recognition systems can have near-perfect accuracy.
Verification algorithms used to match subjects to clear reference images (like
a passport photo or mugshot) can achieve accuracy scores as high as 99.97%
on standard assessments like NIST’s Facial Recognition Vendor Test
(FRVT). This is comparable to the best results of iris scanners. This kind of
face verification has become so reliable that even banks feel comfortable
relying on it to log users into their accounts.
18 | P a g e
e. Scope :
a. In order to prevent the frauds of ATM in India, it is recommended to
prepare the database of all ATM customers with the banks in India &
deployment of high resolution camera and face recognition software at
all ATMs. So, whenever user will enter in ATM his photograph will be
taken to permit the access after it is being matched with stored photo
from the database.
c. Passport and visa verification can also be done using face recognition
technology as explained above.
f. Time :
The project's completion, or final due date for deliverables is May 2022. The
project includes total 3 phases: first phase i.e. requirement gathering includes
the collection of data that can be done , second phase i.e. planning phase.
Writing the main code, implementation and removal of bugs.
19 | P a g e
The project required a great deal of reasoning on how and what highlights
ought to be carried out. Keeping every one of the essential important
prerequisites a venture ought to have as well as drawing out a few less
executed ideas, we have summed up certain highlights the application
contains as follows:
d. Training the dataset : The data set is typically trained with the gathering of
knowledge and available information, collection of images and files. Several
different images of each face are used to train the data set. The thing to be
kept in mind is that the images should be properly labeled for clear
understanding of the data set. The training is done in three phases: training,
validation and testing to determine the accuracy and performance of the
system. More are the number of images provided to the data set, more is the
accuracy.
e. Debugging : The subsequent errors that arise are to be dealt with if needed.
20 | P a g e
analysis is carried out in face identification scenario using large amount of
images from the standard face databases such as AT&T, Georgian Face
database and Head Pose Image database. We analyzed state-of-the art feature
based algorithms such as PCA, log Gabor, DCT and FPLBP and found that,
log Gabor outperforms for larger degree of pose variation with an average
identification rate 82.47% with three training images for Head Pose Image
database.
21 | P a g e
22 | P a g e
Entity Relationship Diagram:
Software Module:
23 | P a g e
Chapter-4
Results analysis and validation
Privacy
Privacy is one of the most prominent concerns raised by critics of FRS. This
is not surprising because, at root, FRS disrupts the flow of information by
connecting facial images with identity, in turn connecting this with whatever
other information is held in a system’s database. Although this need not in
itself be morally problematic, it is important to ascertain, for any given
installation, whether these new connections constitute morally unacceptable
disruptions of entrenched flows (often regarded as violations of privacy) or
whether they can be justified by the needs of the surrounding context.
Fairness
The question of fairness is whether the risks of FRS are borne
disproportionately by, or the benefits flow disproportionately to, any
individual subjects, or groups of subjects. For example, in the evaluations
discussed above, noting that certain systems achieve systematically higher
recognition rates for certain groups over others—older people over youth and
Asians, African-Americans, and other racial minorities over whites—raises
the politically charged suggestion that such systems do not belong in societies
with aspirations of egalitarianism. If, as a result of performance biases,
historically affected racial groups are subjected to disproportionate scrutiny,
particularly if thresholds are set so as to generate high rates of false positives,
24 | P a g e
we are confronted with racial bias similar to problematic practices such as
racial profiling.
Security
Acceptance of facial recognition and other biometric identification systems
has generally been driven by security concerns and the belief that these
technologies offer solutions. Yet, less salient are the security threats posed by
these very systems, particularly threats of harm posed by lax practices dealing
with system databases. Recent incidents in the UK and US suggest that
institutions still do not deserve full public trust in how they safeguard
personal information. In the case of biometric data, this fear is magnified
many times over since it is generally assumed to be a non-falsifiable anchor
of identity. If the biometric template of my face or fingerprint is used to gain
access to a location, it will be difficult for me to argue that it was not me,
given general, if problematic, faith in the claim that “the body never lies.”
Once my face or fingerprint has been digitally encoded, however, it can
potentially be used to act “as if ” it were me and, thus, the security of
biometric data is a pressing matter, usefully considered on a par with DNA
data and evidence. A similar level of caution and security needs to be
established.
25 | P a g e
Chapter-5
Conclusion and future work
The fully automated face detection and recognition system was not robust
enough to achieve a high recognition accuracy. The only reason for this was the
face recognition subsystem did not display even a slight degree of invariance to
scale, rotation or shift errors of the segmented face image. However, if some
sort of further processing, such as an eye detection technique, was implemented
to further normalize the segmented face image, performance will increase to
levels comparable to the manual face detection and recognition system.
Implementing an eye detection technique would be a minor extension to the
implemented system and would not require a great deal of additional research.
All other implemented systems displayed commendable results and reflect well
on the deformable template and Principal Component Analysis strategies. The
most suitable real-world applications for face detection and recognition systems
are for mugshot matching and surveillance. There are better techniques such as
iris or retina recognition and face recognition using the thermal spectrum for
user access and user verification applications since these need a very high
degree of accuracy. The real-time automated pose invariant face detection and
recognition system proposed in chapter seven would be ideal for crowd
surveillance applications. If such a system were widely implemented its
potential for locating and tracking suspects for law enforcement agencies is
immense.
The implemented fully automated face detection and recognition system (with
an eye detection system) could be used for simple surveillance applications such
as ATM user security, while the implemented manual face detection and
automated recognition system is ideal of mugshot matching. Since controlled
26 | P a g e
conditions are present when mugshots are gathered, the frontal view face
recognition scheme should display a recognition accuracy far better than the
results, which were obtained in this study, which was conducted under adverse
conditions.
The automated vision systems implemented in this thesis did not even approach
the performance, nor were they as robust as a human's innate face recognition
system. However, they give an insight into what the future may hold in
computer vision.
REFERANCE
27 | P a g e
• Brunelli, R. and Poggio, T. (1993), Face Recognition: Features versus
Templates. IEEE Transactions on Pattern Analysis and Machine
Intelligence, 15(10):1042-1052
• Craw, I., Ellis, H., and Lishman, J.R. (1987). Automatic extraction of face
features. Pattern Recognition Letters, 5:183-187, February.
• Deffenbacher K.A., Johanson J., and O'Toole A.J. (1998) Facial ageing,
attractiveness, and distinctiveness. Perception. 27(10):1233-1243
• Haralick, R.M. and Shapiro, L.G.. (1992) Computer and Robot Vision,
Volume I. Addison-Wesley
• Haxby, J.V., Ungerleider, L.G., Horwitz, B., Maisog, J.M., Rapoport, S.I.,
and Grady, C.L. (1996). Face encoding and recognition in the human
brain. Proc. Nat. Acad. Sci. 93: 922 - 927.
28 | P a g e
APPENDIX
main.py
import os # accessing the os functions
import check_camera
import Capture_Image
import Train_Image
import Recognize
def title_bar():
os.system('cls') # for windows
print("\t**********************************************")
print("\t***** Face Recognition Attendance System *****")
print("\t**********************************************")
def mainMenu():
title_bar()
print()
print(10 * "*", "WELCOME MENU", 10 * "*")
print("[1] Check Camera")
print("[2] Capture Faces")
print("[3] Train Images")
print("[4] Recognize & Attendance")
print("[5] Auto Mail")
print("[6] Quit")
while True:
try:
choice = int(input("Enter Choice: "))
if choice == 1:
checkCamera()
break
elif choice == 2:
CaptureFaces()
break
elif choice == 3:
Trainimages()
break
elif choice == 4:
RecognizeFaces()
break
elif choice == 5:
os.system("py automail.py")
break
mainMenu()
29 | P a g e
elif choice == 6:
print("Thank You")
break
else:
print("Invalid Choice. Enter 1-4")
mainMenu()
except ValueError:
print("Invalid Choice. Enter 1-4\n Try Again")
exit
# ---------------------------------------------------------
# calling the camera test function from check camera.py file
def checkCamera():
check_camera.camer()
key = input("Enter any key to return main menu")
mainMenu()
# --------------------------------------------------------------
# calling the take image function form capture image.py file
def CaptureFaces():
Capture_Image.takeImages()
key = input("Enter any key to return main menu")
mainMenu()
# -----------------------------------------------------------------
# calling the train images from train_images.py file
def Trainimages():
Train_Image.TrainImages()
key = input("Enter any key to return main menu")
mainMenu()
# --------------------------------------------------------------------
# calling the recognize_attendance from recognize.py file
def RecognizeFaces():
Recognize.recognize_attendence()
key = input("Enter any key to return main menu")
mainMenu()
30 | P a g e
check_camera.py
def camer():
import cv2
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Display
cv2.imshow('Webcam Check', img)
31 | P a g e
Capture_Image.py
import csv
import cv2
import os
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def takeImages():
while(True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5,
minSize=(30,30),flags = cv2.CASCADE_SCALE_IMAGE)
for(x,y,w,h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (10, 159, 255), 2)
#incrementing sample number
sampleNum = sampleNum+1
#saving the captured face in the dataset folder TrainingImage
cv2.imwrite("TrainingImage" + os.sep +name + "."+Id + '.' +
32 | P a g e
str(sampleNum) + ".jpg", gray[y:y+h, x:x+w])
#display the frame
cv2.imshow('frame', img)
#wait for 100 miliseconds
if cv2.waitKey(100) & 0xFF == ord('q'):
break
# break if the sample number is more than 100
elif sampleNum > 100:
break
cam.release()
cv2.destroyAllWindows()
res = "Images Saved for ID : " + Id + " Name : " + name
row = [Id, name]
with open("StudentDetails"+os.sep+"StudentDetails.csv", 'a+') as
csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()
else:
if(is_number(Id)):
print("Enter Alphabetical Name")
if(name.isalpha()):
print("Enter Numeric ID")
33 | P a g e
Train_Image.py
import os
import time
import cv2
import numpy as np
from PIL import Image
from threading import Thread
def getImagesAndLabels(path):
# get the path of all the files in the folder
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
# print(imagePaths)
# Optional, adds a counter for images trained (You can remove it)
def counter_img(path):
imgcounter = 1
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
for imagePath in imagePaths:
print(str(imgcounter) + " Images Trained", end="\r")
34 | P a g e
time.sleep(0.008)
imgcounter += 1
Recognize.py
import datetime
import os
import time
import cv2
import pandas as pd
#-------------------------
def recognize_attendence():
recognizer = cv2.face.LBPHFaceRecognizer_create() #
cv2.createLBPHFaceRecognizer()
recognizer.read("TrainingImageLabel"+os.sep+"Trainner.yml")
harcascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(harcascadePath)
df = pd.read_csv("StudentDetails"+os.sep+"StudentDetails.csv")
font = cv2.FONT_HERSHEY_SIMPLEX
col_names = ['Id', 'Name', 'Date', 'Time']
attendance = pd.DataFrame(columns=col_names)
while True:
ret, im = cam.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.2, 5,minSize = (int(minW),
int(minH)),flags = cv2.CASCADE_SCALE_IMAGE)
for(x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x+w, y+h), (10, 159, 255), 2)
Id, conf = recognizer.predict(gray[y:y+h, x:x+w])
aa = df.loc[df['Id'] == Id]['Name'].values
confstr = " {0}%".format(round(100 - conf))
tt = str(Id)+"-"+aa
else:
Id = ' Unknown '
tt = str(Id)
confstr = " {0}%".format(round(100 - conf))
35 | P a g e
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:
%M:%S')
aa = str(aa)[2:-2]
attendance.loc[len(attendance)] = [Id, aa, date, timeStamp]
tt = str(tt)[2:-2]
if(100-conf) > 67:
tt = tt + " [Pass]"
cv2.putText(im, str(tt), (x+5,y-5), font, 1, (255, 255, 255),
2)
else:
cv2.putText(im, str(tt), (x + 5, y - 5), font, 1, (255, 255,
255), 2)
36 | P a g e
automail.py
import os
import yagmail
# mail information
yag = yagmail.SMTP("your [email protected]", "your password")
37 | P a g e