You are on page 1of 5

Aldel Education Trust’s

St. John College of Engineering and Management, Palghar


(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING

Experiment 06
Title: Detecting and Recognizing Faces
Aim: Detecting and Recognizing Faces Conceptualizing Haar cascades, Getting Haar cascade
data, Using OpenCV to perform face detection, Performing face detection on a still image.
Theory :

OpenCV has a built-in facility to perform face detection, which has virtually infinite applications in the
real world in all sorts of contexts, from security to entertainment.OpenCV's face detection
functionalities, along with the data files define particular types of trackable objects. Specifically, we
look at Haar cascade classifiers, which analyze contrast between adjacent image regions to determine
whether or not a given image or sub-image matches a known type. We consider how to combine multiple
Haar cascade classifiers in a hierarchy, such that one classifier identifies a parent region (for our
purposes, a face), and other classifiers identify child regions (eyes, nose, and mouth).
Conceptualizing Haar cascades :
Photographic images, even from a webcam, may contain a lot of detail for our (human)
viewing pleasure. However, image detail tends to be unstable with respect to variations in
lighting, viewing angle, viewing distance, camera shake, and digital noise. Moreover, even real
differences in physical detail might not interest us for the purpose of classification.
Abstracting image detail is useful in producing stable classification and tracking results. The
abstractions are called features, which are said to be extracted from the image data. There
should be far fewer features than pixels, though any pixel might influence multiple features.
For example, the distance might be defined in terms of spatial coordinates or color coordinates.
Haar-like features are one type of feature that is often applied to real-time face tracking. Each
Haar-like feature describes the patternof contrast among adjacent image regions. For example,
edges, vertices, and thin lineseach generate distinctive features.

Getting Haar cascade data :


This folder contains all the XML files used by the OpenCV face detection engine to detect
faces in still images, videos, and camera feeds. Once we find haar cascades, create a directory
for your project; in this folder, create a subfolder called cascades, and copy the following files
from haar cascades into cascades:
haarcascade_profileface.xml

Om Bhamare BE B 05
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING
haarcascade_righteye_2splits.xml
haarcascade_russian_plate_number.xml
haarcascade_smile.xml haarcascade_upperbody.xml
As their names suggest, these cascades are for tracking faces, eyes, noses, and mouths.They
require a frontal, upright view of the subject. We will use them later when building a face
detector.
Using OpenCV to perform face detection :
Performing face detection on a still image or a video feed is an extremely similar operation.
The latter is just the sequential version of the former: face detection on videos is simply face
detection applied to each frame read into the program from the camera. Naturally, a whole host
of concepts are applied to video face detection such as tracking, which does not apply to still
images, but it's always good to know that the underlying theory is the same.
Performing face detection on a still image :
The first and most basic way to perform face detection is to load an image and detect faces in
it. To make the result visually meaningful, we will draw rectangles around faces on the original
image.
The parameters passed are scaleFactor and minNeighbors, which determine the percentage
reduction of the image at each iteration of the face detection process, and the minimum number
of neighbors retained by each face rectangle at each iteration. This may all seem a little complex
in the beginning but you can check all the options out in the official documentation.The value
returned from the detection operation is anarray of tuples that represent the face rectangles. The
utility method, cv2.rectangle, allows us to draw rectangles at the specified coordinates (x and
y represent the left andtop coordinates, w and h represent the width and height of the face
rectangle).

Program :
Using OpenCV to perform face detection :

import cv2
#Importing HARR CASCADE XML file
face_cascade =
cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #Uploading
test image
img =
cv2.imread('Test.jpg')
#Converting to grey scale
gray = cv2.cvtColor(img,
cv2.COLOR_BGR2GRAY)#Allowing multiple
scale(Multiple size) detection faces =
face_cascade.detectMultiScale(gray, 1.1, 6)
#Creating Rectangle around face

Om Bhamare BE B 05
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING
for(x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 250),
2)#Displaying the image
cv2.imshow('Detected Face Image', img)
#Waiting for escape key for image to close
cv2.waitKey()
Performing face detection on a still image:

import cv2
filename =
('download.jpg')def
detect(filename):
face_cascade
=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') img =
cv2.imread(filename)
gray = cv2.cvtColor(img,
cv2.COLOR_BGR2GRAY)faces =
face_cascade.detectMultiScale(gray, 1.3, 5) for
(x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.namedWindow('Vikings Detected!!')
cv2.imshow('Vikings Detected!!', img)
cv2.imwrite('download.jpg', img)
cv2.waitKey(
0)
detect(filename)
Output :
Using OpenCV to perform face detection :

Om Bhamare BE B 05
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING

Om Bhamare BE B 05
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING
Performing face detection on a still image:

Conclusion:

Hence we should have a good understanding of how face detection and face recognition work, and how
to implement them in Python and OpenCV 3. Face detection and face recognition are constantly evolving
branches of computer vision, with algorithms being developed continuously, and they will evolve even
faster in the near future with the emphasis posed on robotics and the Internet of things.

Om Bhamare BE B 05

You might also like