Click here to Skip to main content
15,881,873 members
Articles / Multimedia / GDI+

Face and Eyes Detection Using OpenCV

Rate me:
Please Sign up or sign in to vote.
3.58/5 (38 votes)
7 Aug 2008CPOL2 min read 972.9K   82.8K   193   200
This article demonstrates how to perform human face and eyes detection on images using OpenCV in .NET
Cascades

Introduction

This article is an extension of an article that I posted on CodeProject earlier. This project demonstrates how to perform human face and eye detection using OpenCV in .NET. The detection is performed using Haar Cascades that I acquired from two different sources (see References).

Background

While working on a face recognition project, I stumbled across a huge problem; the preprocessing phase required face alignment for improved result. This step required eye coordinates in order to stretch/rotate the image to a standard size before performing actual face recognition training/projection. For experimentation, the eye-coordinates could be entered manually but that would never be a practical solution. Exploring my options, I found OpenCV and the Haar Cascades to perform this task for me.

Using the Code

The project is mainly a managed VC++.NET class library, that can be consumed from C#/VC++ .NET. A Test application demonstrates the typical use of this library. It is to be noted that Version 2 requires the 'Cascades' folder in the Current Execution Path folder.

There are mainly two functions that are used to perform face and eye detection. Observe the following code:

C#
// the variable faces contains the number of detected faces in the image
int faces = objFaceDetector.WrapDetectFaces(lastPic);

int lx, ly, rx, ry, res;

for(int f = 0; f < faces; f++)
{ 
    //The face coordinates are indexed, max index == 4
    objFaceDetector.WrapGetFaceCordinates(f, &lx, &ly, &rx, &ry);
<< Source Code For Drawing Face Rectangle >>
    //returns -1 if cannot detect eyes for this face index
    res = objFaceDetector.WrapGetEyeCordinates(f, &lx, &ly, &rx, &ry);
<< Source Code For Drawing Eye Crosses >>
}

The use of this library is quite simple and may find its application in face recognition and human interaction projects. Someone suggested to use this for detecting if the driver is dozing while driving, BUT let me remind you that there are no guarantees from me, Opencv, Opencv developer community or any other party for the results of this code. The author will not be liable for any harm, injury and/or loss caused directly or indirectly by using this code in any way.

Points of Interest

The accuracy of Eye cascades was further corrected by a writing a code snippet that simply checks if the suggested eye coordinates really fall in the face region. Furthermore, the face region is logically divided into four equal height horizontal strips, the second strip from the top contains the eyes. This strip can be divided into two halves vertically so that each eye will fall in one half. If the detected eye coordinates do not fall in their respective halves, they are discarded. In case of multiple detections in the same region, the object with smaller size is preferred because of empirical reasons. The eye detection is performed using two different Cascades, this proved to give better results than using the Cascade that claims to detect both eyes in one go. Further research may lead you to differ from my suggested approach, you can always provide a feedback.

References

Other Articles by This Author

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
BCSE - Software Engineering (2000 - 2004)
Foundation University Institute of Management and Computer Sciences.
Pakistan.

MS - Computer Sciences (2004 - 2005)
Lahore Univeristy of Management Sciences
Pakistan.

Comments and Discussions

 
GeneralMy vote of 1 Pin
praveenpragasam13-Jul-09 2:07
praveenpragasam13-Jul-09 2:07 
GeneralRe: My vote of 1 Pin
Zeeshan Ejaz Bhatti20-Jul-09 8:12
Zeeshan Ejaz Bhatti20-Jul-09 8:12 
GeneralError LNK1181 Pin
waybow21-Jun-09 16:37
waybow21-Jun-09 16:37 
GeneralRe: Error LNK1181 Pin
Zeeshan Ejaz Bhatti20-Jul-09 8:13
Zeeshan Ejaz Bhatti20-Jul-09 8:13 
Generalwould be better if you use MRA Pin
Member 30348228-May-09 9:56
Member 30348228-May-09 9:56 
GeneralRe: would be better if you use MRA Pin
Zeeshan Ejaz Bhatti20-Jul-09 8:17
Zeeshan Ejaz Bhatti20-Jul-09 8:17 
GeneralSolution 2 for can not convert schar to char Pin
thebiggerd18-Mar-09 7:28
thebiggerd18-Mar-09 7:28 
GeneralRe: Solution 2 for can not convert schar to char Pin
Zeeshan Ejaz Bhatti20-Jul-09 8:14
Zeeshan Ejaz Bhatti20-Jul-09 8:14 
GeneralSolution for cannot convert from 'schar *' to 'char *' Pin
thebiggerd18-Mar-09 4:57
thebiggerd18-Mar-09 4:57 
Questionhelp Pin
4Year13-Mar-09 6:18
4Year13-Mar-09 6:18 
AnswerRe: help Pin
Zeeshan Ejaz Bhatti20-Jul-09 8:15
Zeeshan Ejaz Bhatti20-Jul-09 8:15 
GeneralErro Pin
Asad42023-Feb-09 7:41
Asad42023-Feb-09 7:41 
GeneralRe: Erro Pin
Zeeshan Ejaz Bhatti20-Jul-09 8:16
Zeeshan Ejaz Bhatti20-Jul-09 8:16 
Questionhow to work with this project? Pin
anithanithi30-Jan-09 1:50
anithanithi30-Jan-09 1:50 
AnswerRe: how to work with this project? Pin
Zeeshan Ejaz Bhatti1-Feb-09 22:30
Zeeshan Ejaz Bhatti1-Feb-09 22:30 
GeneralError in entry point main programin the line " Application.Run(new frmEyeTest()); " Pin
Member 314727927-Jan-09 18:10
Member 314727927-Jan-09 18:10 
GeneralRe: Error in entry point main programin the line " Application.Run(new frmEyeTest()); " Pin
Zeeshan Ejaz Bhatti1-Feb-09 22:28
Zeeshan Ejaz Bhatti1-Feb-09 22:28 
GeneralRe: Error in entry point main programin the line " Application.Run(new frmEyeTest()); " Pin
kambiz rahbar20-Feb-09 23:08
kambiz rahbar20-Feb-09 23:08 
GeneralRe: Error in entry point main programin the line " Application.Run(new frmEyeTest()); " Pin
huyhd198229-May-11 17:15
huyhd198229-May-11 17:15 
Questionerror occured in unsafe Pin
sudan prajapati15-Jan-09 23:32
sudan prajapati15-Jan-09 23:32 
AnswerRe: error occured in unsafe Pin
Zeeshan Ejaz Bhatti1-Feb-09 22:29
Zeeshan Ejaz Bhatti1-Feb-09 22:29 
Generaleyes are not detected Pin
Member 372268517-Dec-08 21:09
Member 372268517-Dec-08 21:09 
GeneralRe: eyes are not detected Pin
Zeeshan Ejaz Bhatti23-Dec-08 6:24
Zeeshan Ejaz Bhatti23-Dec-08 6:24 
GeneralRe: eyes are not detected Pin
Josh Susskind21-Jan-09 19:05
Josh Susskind21-Jan-09 19:05 
Questionhow do u detect objects from an image? Pin
ritikkhattu17-Nov-08 3:19
ritikkhattu17-Nov-08 3:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.