Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/267426877

Facial Recognition using OpenCV

Article · March 2012

CITATIONS READS

22 16,018

2 authors, including:

Shervin Emami
The University of Queensland
7 PUBLICATIONS 66 CITATIONS

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

FaceBots View project

All content following this page was uploaded by Shervin Emami on 12 August 2015.

The user has requested enhancement of the downloaded file.


www.jmeds.eu

Facial Recognition using OpenCV


Shervin EMAMI1, Valentin Petruț SUCIU2
1
Senior Systems Software Engineer
Mobile Computer Vision Team, NVIDIA
AUSTRALIA
2
IT&C Security Master
Department of Economic Informatics and Cybernetics
Bucharest University of Economic Studies
ROMANIA
shervin.emami@gmail.com, petrut.suciu@gmail.com

Abstract: The growing interest in computer vision of the past decade. Fueled by the steady doubling rate of
computing power every 13 months, face detection and recognition has transcended from an esoteric to a popular
area of research in computer vision and one of the better and successful applications of image analysis and
algorithm based understanding. Because of the intrinsic nature of the problem, computer vision is not only a
computer science area of research, but also the object of neuro-scientific and psychological studies, mainly
because of the general opinion that advances in computer image processing and understanding research will
provide insights into how our brain work and vice versa.
Because of general curiosity and interest in the matter, the author has proposed to create an application that
would allow user access to a particular machine based on an in-depth analysis of a person’s facial features. This
application will be developed using Intel’s open source computer vision project, OpenCV and Microsoft’s .NET
framework.

Key-Words: face, detection, recognition, system, OpenCV, Eigenface

1. Introduction 2. Problem Definition


The goal of this article is to provide an Over the past decade face detection and
easier human-machine interaction recognition have transcended from
routine when user authentication is esoteric to popular areas of research in
needed through face detection and computer vision and one of the better
recognition. and successful applications of image
analysis and algorithm based
With the aid of a regular web camera, a understanding. Because of the intrinsic
machine is able to detect and recognize nature of the problem, computer vision
a person’s face; a custom login screen is not only a computer science area of
with the ability to filter user access research, but also the object of neuro-
based on the users’ facial features will scientific and psychological studies also,
be developed. mainly because of the general opinion
that advances in computer image
The objectives of this thesis are to processing and understanding research
provide a set of detection algorithms will provide insights into how our brain
that can be later packaged in an easily- work and vice versa.
portable framework amongst the A general statement of the face
different processor architectures we see recognition problem (in computer vision)
in machines (computers) today. These can be formulated as follows: given still
algorithms must provide at least a 95% or video images of a scene, identify or
successful recognition rate, out of which verify one or more persons in the scene
less than 3% of the detected faces are using a stored database of faces.
false positives. Facial recognition generally involves two
stages:
Face Detection where a photo is
searched to find a face, then the image
38
Journal of Mobile, Embedded and Distributed Systems, vol. IV, no. 1, 2012
ISSN 2067 – 4074

is processed to crop and extract the and commercial community and began
person’s face for easier recognition. actively supporting OpenCV, with Gary
Face Recognition where that detected and Vadim once again leading the effort
and processed face is compared to a [2].
database of known faces, to decide who Intel's open-source computer-vision
that person is. library can greatly simplify computer-
Since 2002, face detection can be vision programming. It includes
performed fairly easily and reliably with advanced capabilities - face detection,
Intel’s open source framework called face tracking, face recognition, Kalman
OpenCV [1]. This framework has an in- filtering, and a variety of artificial-
built Face Detector that works in roughly intelligence (AI) methods - in ready-to-
90-95% of clear photos of a person use form. In addition, it provides many
looking forward at the camera. However, basic computer-vision algorithms via its
detecting a person’s face when that lower-level APIs.
person is viewed from an angle is OpenCV has the advantage of being a
usually harder, sometimes requiring 3D multi-platform framework; it supports
Head Pose Estimation. Also, lack of both Windows and Linux, and more
proper brightness of an image can recently, Mac OS X.
greatly increase the difficulty of OpenCV has so many capabilities it can
detecting a face, or increased contrast in seem overwhelming at first. A good
shadows on the face, or maybe the understanding of how these methods
picture is blurry, or the person is work is the key to getting good results
wearing glasses, etc. when using OpenCV. Fortunately, only a
select few need to be known beforehand
Face recognition however is much less to get started.
reliable than face detection, with an OpenCV's functionality that will be used
accuracy of 30-70% in general. Face for facial recognition is contained within
recognition has been a strong field of several modules. Following is a short
research since the 1990s, but is still a description of the key namespaces:
far way away from a reliable method of CXCORE namespace contains basic data
user authentication. More and more type definitions, linear algebra and
techniques are being developed each statistics methods, the persistence
year. The Eigenface technique is functions and the error handlers.
considered the simplest method of Somewhat oddly, the graphics functions
accurate face recognition, but many for drawing on images are located here
other (much more complicated) methods as well.
or combinations of multiple methods are CV namespace contains image
slightly more accurate. processing and camera calibration
methods. The computational geometry
OpenCV was started at Intel in 1999 by functions are also located here.
Gary Bradski for the purposes of CVAUX namespace is described in
accelerating research in and commercial OpenCV's documentation as containing
applications of computer vision in the obsolete and experimental code.
world and, for Intel, creating a demand However, the simplest interfaces for face
for ever more powerful computers by recognition are in this module. The code
such applications. Vadim Pisarevsky behind them is specialized for face
joined Gary to manage Intel's Russian recognition, and they're widely used for
software OpenCV team. Over time the that purpose.
OpenCV team moved on to other ML namespace contains machine-
companies and other Research. Several learning interfaces.
of the original team eventually ended up HighGUI namespace contains the basic
working in robotics and found their way I/O interfaces and multi-platform
to Willow Garage. In 2008, Willow windowing capabilities.
Garage saw the need to rapidly advance CVCAM namespace contains interfaces
robotic perception capabilities in an open for video access through DirectX on 32-
way that leverages the entire research bit Windows platforms.
39
www.jmeds.eu

applying face recognition. You should


Eigenfaces is considered the simplest also do things like removing the pixels
method of accurate face recognition, but around the face that aren't used, such
many other (much more complicated) as with an elliptical mask to only show
methods or combinations of multiple the inner face region, not the hair and
methods are slightly more accurate. image background, since they change
Most resources on face recognition are more than the face does.
for basic Neural Networks, which usually
don't work as well as Eigenfaces does. For simplicity, the face recognition
And unfortunately there are only some system presented in this paper is
basic explanations for better type of face Eigenfaces using grayscale images. The
recognition than Eigenfaces, such as paper demonstrates how easily is to
recognition from video and other convert color images to grayscale (also
techniques at the Face Recognition called 'grayscale'), and then to
Homepage [4] or 3D Face Recognition apply Histogram Equalization [8] as a
Wikipedia page [5] and Active very simple method of automatically
Appearance Models page [6]. But for standardizing the brightness and
other techniques, you should read some contrast of your facial images. For better
recent computer vision research papers results, you could use color face
from CVPR and other computer vision recognition (ideally with color histogram
conferences. Most computer vision or fitting in HSV or another color space
machine vision conferences include new instead of RGB), or apply more
advances in face detection and face processing stages such as edge
recognition that give slightly better enhancement, contour detection, motion
accuracy. So for example you can look detection, etc. Also, this code is resizing
for the CVPR10 and CVPR09 conferences images to a standard size, but this might
[7]. change the aspect ratio of the face. In
[9] is described a method on how to
3. Proposed Solution resize an image while keeping its aspect
ratio the same.
When image quality is taken into OpenCV uses a type of face detector
consideration, there is a plethora of called a Haar Cascade classifier.
factors that influence the system’s Given an image, which can come from a
accuracy. file or from live video, the face detector
It is extremely important to apply examines each image location and
various image pre-processing techniques classifies it as "Face" or "Not Face."
to standardize the images that you Classification assumes a fixed scale for
supply to a face recognition system. the face, say 50x50 pixels. Since faces
Most face recognition algorithms are in an image might be smaller or larger
extremely sensitive to lighting than this, the classifier runs over the
conditions, so that if it was trained to image several times, to search for faces
recognize a person when they are in a across a range of scales. This may seem
dark room, it probably won’t recognize an enormous amount of processing, but
them in a bright room, etc. This problem thanks to algorithmic tricks, explained in
is referred to as "lumination dependent", the sidebar, classification is very fast,
and there are also many other issues, even when it's applied at several scales.
such as the face should also be in a very The classifier uses data stored in an XML
consistent position within the images file to decide how to classify each image
(such as the eyes being in the same location. The OpenCV download includes
pixel coordinates), consistent size, four flavors of XML data for frontal face
rotation angle, hair and makeup, detection, and one for profile faces. It
emotion (smiling, angry, etc), position of also includes three non-face XML files -
lights (to the left or above, etc). This is one for full body (pedestrian) detection,
why it is so important to use a good one for upper body, and one for lower
image preprocessing filters before body.

40
Journal of Mobile, Embedded and Distributed Systems, vol. IV, no. 1, 2012
ISSN 2067 – 4074

You'll need to tell the classifier where to


// Get the first frame, to make sure the
find the data file you want it to use. The camera is initialized.
one I'll be using is called frame = cvQueryFrame( camera );
haarcascade_frontalface_default.xml. In
OpenCV version 1.0, it's located at if (frame) {
w = frame->width;
[OPENCV_ROOT]/data/haarcascades/haa h = frame->height;
rcascade_frontalface_default.xml where printf("Got the camera at %dx%d
[OPENCV_ROOT] is the path to your resolution.\n", w, h);
OpenCV installation. For example, if }
you're on Windows XP, and you selected // Wait a little, so that the camera can
the default installation location, you'd auto-adjust its brightness.
use [OPENCV_ROOT] = "C:/Program Sleep(1000); // (in milliseconds)
Files/OpenCV" (If you're working with an }
older, 16-bit version of Windows, you'd // Wait until the next camera frame is
use '\' as the directory separator, ready, then grab it.
instead of '/'.) frame = cvQueryFrame( camera );
It's a good idea to locate the XML file if (!frame) {
you want to use, and make sure your printf("Couldn't grab a camera
path to it is correct, before you code the frame.\n");
rest of your face-detection program. exit(1);
}
It is very easy to use a webcam stream return frame;
as input to the face recognition system }
instead of a file list. Basically you just
need to grab frames from a camera This function can be used like this:
instead of from a file, and you run
forever until the user wants to quit, CvCapture* camera = 0;
instead of just running until the file list // The camera device.
has run out.
// Quit on "Escape" key.
while ( cvWaitKey(10) != 27 ) {
Grabbing frames from a webcam can be IplImage *frame =
implemented easily using this function: getCameraFrame(camera);

// Grab the next camera frame. Waits …


until the next frame is ready, and
// provides direct access to it, so do }
NOT modify or free the returned image! // Free the camera.
// Will automatically initialize the cvReleaseCapture( &camera );
camera on the first frame.
IplImage* getCameraFrame(CvCapture*
&camera) 4. Conclusions
{
IplImage *frame;
int w, h; To improve the recognition performance,
there are MANY things that can be
// If the camera hasn't been improved here, some of them being
initialized, then open it. fairly easy to implement. For example,
if (!camera) {
printf("Acessing the camera ...\n"); you could add color processing, edge
camera = cvCreateCameraCapture( 0 ); detection, etc.
if (!camera) {
printf("Couldn't access the camera.\n");
You can usually improve the face
exit(1);
} recognition accuracy by using more
input images, at least 50 per person, by
// Try to set the camera resolution to taking more photos of each person,
320 x 240.
particularly from different angles and
cvSetCaptureProperty(camera, lighting conditions. If you can’t take
CV_CAP_PROP_FRAME_WIDTH, 320); more photos, there are several simple
techniques you could use to obtain more
cvSetCaptureProperty(camera,
CV_CAP_PROP_FRAME_HEIGHT, 240);

41
www.jmeds.eu

training images, by generating new is that at the heart of the algorithm, it is


images from your existing ones: matching images by basically doing the
You could create mirror copies of your equivalent of subtracting the testing
facial images, so that you will have twice image with a training image to see how
as many training images and it won’t similar they are. This would work fairly
have a bias towards left or right. well if a human performed it, but the
You could translate or resize or rotate computer just thinks in terms of pixels
your facial images slightly to produce and numbers. So if you imagine that it is
many alternative images for training, so looking at one pixel in the test image,
that it will be less sensitive to exact and subtracting the gray scale value of
conditions. that pixel with the value of the pixel in
You could add image noise to have more the EXACT same location of each
training images that improve the training image, and the lower the
tolerance to noise. difference then the better the match. So
if you just move an image by a few
It is important to have a lot of variation pixels across, or use an image that is
of conditions for each person, so that just a few pixels bigger or has a few
the classifier will be able to recognize more pixels of the forehead showing
the person in different lighting conditions than the other image, etc, then it will
and positions, instead of looking for think they are completely different
specific conditions. But it’s also images! This is also true if the
important to make sure that a set of background is different, because the
images for a person is not too varied, code doesn't know the difference
such as if you rotated some images by between background and foreground
90 degrees. This would make the (face), which is why its important to
classifier to be too generic and also give crop away as much of the background as
very bad results, so if you think you will you can, such as by only using a small
have a set of images with too much section inside the face that doesn't
variance (such as rotation more than 20 include any background at all.
degrees), then you could create
separate sets of training images for each Since the images should be almost
person. For example, you could train a perfectly aligned, it actually means that
classifier to recognize in many cases, using small low-res
"John_Facing_Forward" and another one images (such as by shrinking the images
for "John_Facing_Left" and other ones to thumbnail size) can give better
"Mary_Facing_Forward", recognition results than large hi-res
"Mary_Facing_Left", etc. Then each images!
classifier can have a lot of variance but
not too much, and you simply need to Also, even if the images are perfectly
associate the different classifiers for aligned, if the testing image is a bit
each person with that one person (ie: brighter than the training image then it
"John" or "Mary"). will still think there is not much of a
match. Histogram Equalization can help
That's why you can often get very bad in many cases but it can also make
results if you don't use good things worse in other cases, so
preprocessing on your images. As I differences in lighting is a difficult &
mentioned earlier, Histogram common problem. There are also issues
Equalization is a very basic image such as if there was a shadow on the left
preprocessing method that can make of the nose in the training image and on
things worse in some situations, so you the right in the test image then it will
will probably have to combine several often cause a bad match, etc.
different methods until you get decent
results. That's why face recognition is relatively
easy to do in real-time if you are
And something important to understand training on someone and then instantly

42
Journal of Mobile, Embedded and Distributed Systems, vol. IV, no. 1, 2012
ISSN 2067 – 4074

trying to recognize them after, since it n.html, Published by Shervin Emami,


will be the same camera, and 2010
background will be the same, their [2] Seeing with OpenCV, Article,
expressions will be almost the same, the http://www.cognotics.com/opencv/servo
lighting will be the same, and the _2007_series/part_1/index.html,
direction you are viewing them from will Published by Robin Hewitt, 2010
be the same. So you will often get good
[3] OpenCV Homepage,
recognition results at that moment. But
http://opencv.willowgarage.com
once you try to recognize them from a
different direction or from a different [4] Face Recognition Homepage,
room or outside or on a different time of http://www.face-rec.org/algorithms/
the day, it will often give bad results! [5] Wikipedia, Three-dimensional face
So it’s important to do a lot of recognition,
experimentation if you want better http://en.wikipedia.org/wiki/Three-
results, and if you still can't get good dimensional_face_recognition
results after trying many things, perhaps [6] Wikipedia, Active appearance model,
you will need a more complicated face http://en.wikipedia.org/wiki/Active_appe
recognition algorithm than PCA arance_model
(Eigenfaces), such as 3D Face
[7] Computer Vision Papers,
Recognition or Active Appearance
http://www.cvpapers.com/
Models, mentioned below.
[8] Wikipedia, Histogram equalization,
http://en.wikipedia.org/wiki/Histogram_
References equalization
[1] Face Detection and Recognition
using OpenCV, Article, [9] Shervin Emami, Rotating or Resizing
http://shervinemami.info/faceRecognitio an Image in OpenCV,
http://shervinemami.info/imageTransfor
ms.html

43

View publication stats

You might also like