Click here to Skip to main content
15,444,627 members
Articles / Multimedia / GDI+
Posted 25 Jan 2008


192 bookmarked

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
This article demonstrates how to perform human face and eyes detection on images using OpenCV in .NET


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).


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:

// 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.


Other Articles by This Author


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.

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

Comments and Discussions

Questionnot found FaceDetection.dll Pin
hamedyar222-Dec-11 7:52
Memberhamedyar222-Dec-11 7:52 
Questioneye detection Pin
boragökova20-Nov-11 0:57
Memberboragökova20-Nov-11 0:57 
QuestionApplication doesnt work by default Pin
Ammar Bukhari SEC29-Oct-11 22:53
MemberAmmar Bukhari SEC29-Oct-11 22:53 
Questionneed help rotating face for improving face recognition Pin
mehwish8731-Aug-11 2:56
Membermehwish8731-Aug-11 2:56 
AnswerRe: need help rotating face for improving face recognition Pin
laiza ramal8-Nov-12 3:25
Memberlaiza ramal8-Nov-12 3:25 
QuestionLost file cv.h Pin
Michael.hg.zhou17-Aug-11 19:17
MemberMichael.hg.zhou17-Aug-11 19:17 
GeneralNot bad... A little too hit and miss though. Pin
Danny Mosquito5-Jun-11 6:52
MemberDanny Mosquito5-Jun-11 6:52 
GeneralRe: Not bad... A little too hit and miss though. Pin
Ammar Bukhari SEC29-Oct-11 22:54
MemberAmmar Bukhari SEC29-Oct-11 22:54 
can you send me the code that you used to detect face in pics. I am urgently in need of some thing like that.
Generalcode of our project Pin
vedaprakash.M.G3-May-11 21:19
Membervedaprakash.M.G3-May-11 21:19 
GeneralRe: code of our project Pin
laiza ramal8-Nov-12 3:26
Memberlaiza ramal8-Nov-12 3:26 
Generalcannot find clsFaceDetectionWrapper, missing any assembly reference? Pin
Thaanya24-Apr-11 22:09
MemberThaanya24-Apr-11 22:09 
General0 faces found Pin
1_stev13-Mar-11 10:21
Member1_stev13-Mar-11 10:21 
Generalfacedetection.dll Pin
1_stev13-Mar-11 10:17
Member1_stev13-Mar-11 10:17 
Generalhi Pin
1_stev11-Mar-11 6:28
Member1_stev11-Mar-11 6:28 
Question[ask] expression recognizing Pin
aan8725-Jan-11 20:53
Memberaan8725-Jan-11 20:53 
GeneralErro while building FaceDetection project Pin
SanjayAYadav1-Nov-10 20:49
MemberSanjayAYadav1-Nov-10 20:49 
GeneralError : error LNK1104: cannot open file 'cv.lib' Pin
SanjayAYadav1-Nov-10 19:58
MemberSanjayAYadav1-Nov-10 19:58 
GeneralError in running the sample codes Pin
UpdateAk478-Oct-10 9:55
MemberUpdateAk478-Oct-10 9:55 
Generalvery happay to see article that is posted by my senior Pin
sohaik15-Aug-10 6:51
Membersohaik15-Aug-10 6:51 
Questionopen cv Pin
jyotsnakathuria3-Aug-10 2:35
Memberjyotsnakathuria3-Aug-10 2:35 
GeneralMy vote of 1 Pin
purohit_rashmi26-Jul-10 18:42
Memberpurohit_rashmi26-Jul-10 18:42 
GeneralRe: My vote of 1 Pin
Nasenbaaer31-Aug-10 4:43
MemberNasenbaaer31-Aug-10 4:43 
QuestionPlease help me: face recognition program for absence system Pin
Alveen Scofield28-May-10 20:02
MemberAlveen Scofield28-May-10 20:02 
AnswerRe: Please help me: face recognition program for absence system Pin
Shervin Emami8-Jun-10 3:17
MemberShervin Emami8-Jun-10 3:17 
GeneralRe: Please help me: face recognition program for absence system Pin
Alveen Scofield13-Jun-10 3:15
MemberAlveen Scofield13-Jun-10 3:15 

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.