Click here to Skip to main content
13,257,276 members (54,005 online)
Click here to Skip to main content
Add your own
alternative version


187 bookmarked
Posted 25 Jan 2008

Face and Eyes Detection Using OpenCV

, 7 Aug 2008
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Zeeshan Ejaz Bhatti
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

You may also be interested in...


Comments and Discussions

QuestionFacedetection.dll Pin
Member 131655251-May-17 21:39
memberMember 131655251-May-17 21:39 
Questionfacedetection.dll Pin
Member 122759631-Feb-16 1:31
memberMember 122759631-Feb-16 1:31 
Questionfacedetection.dll Pin
Member 1210492326-Nov-15 10:12
memberMember 1210492326-Nov-15 10:12 
QuestionFaceDetection.dll Pin
MAQSOODHYDER15-Apr-15 8:39
memberMAQSOODHYDER15-Apr-15 8:39 
QuestionFacedetection.dll Pin
Member 35171792-Dec-14 2:31
memberMember 35171792-Dec-14 2:31 
Questionface detection dll Pin
Ajith Premalal19-Nov-14 21:04
memberAjith Premalal19-Nov-14 21:04 
Questiondll Pin
Member 1081470614-May-14 1:33
memberMember 1081470614-May-14 1:33 
GeneralEmotion Detection Software Pin
shane007117-Apr-14 8:16
membershane007117-Apr-14 8:16 
QuestionNeed more explaination Pin
LogicPlayer18-Mar-14 22:04
memberLogicPlayer18-Mar-14 22:04 
QuestionPlease provide FaceDetection.dll Pin
jeffy6653-Feb-14 3:40
memberjeffy6653-Feb-14 3:40 
QuestionCompiling Error : error C1083: Cannot open include file: 'cv.h': No such file or directory Pin
Nehal4Monto25-Oct-13 4:09
memberNehal4Monto25-Oct-13 4:09 
QuestionFacedetection dll Pin
Vishal Raje19-Oct-13 22:22
memberVishal Raje19-Oct-13 22:22 
Questioncannot find faceDetection.dll Pin
Member 1033630414-Oct-13 18:56
memberMember 1033630414-Oct-13 18:56 
GeneralMy vote of 5 Pin
Member 1023866828-Aug-13 12:21
memberMember 1023866828-Aug-13 12:21 
Questionfacedection.dll Pin
nilooofar13-May-13 22:04
membernilooofar13-May-13 22:04 
QuestionEye detection in Pin
Zahid Gul10-Mar-13 20:16
memberZahid Gul10-Mar-13 20:16 
QuestionI want to FaceDetection.dll Pin
Sorum31-Oct-12 22:32
memberSorum31-Oct-12 22:32 
Questionfacedection.dll Pin
gsmsdn29-Oct-12 22:35
membergsmsdn29-Oct-12 22:35 
QuestionPlease I need Facedetection.ddl Pin
richmasua3-Oct-12 9:50
memberrichmasua3-Oct-12 9:50 
QuestionX64 Pin
programmerdon24-Sep-12 11:32
memberprogrammerdon24-Sep-12 11:32 
Questionfacedetection.dll Pin
Cerber24052-Sep-12 10:38
memberCerber24052-Sep-12 10:38 
AnswerRe: facedetection.dll Pin
Member 1030893022-Dec-13 0:02
memberMember 1030893022-Dec-13 0:02 
Question[My vote of 2] face detect though MFC? Pin
Member 842634320-Aug-12 23:00
memberMember 842634320-Aug-12 23:00 
QuestionAdaboost Pin
Fadol1-Aug-12 0:42
memberFadol1-Aug-12 0:42 
QuestionThis code in not working on my system Pin
Ahmad Usman8-Jul-12 17:02
memberAhmad Usman8-Jul-12 17:02 
Hi Zeeshan this program is not working with my Windows Vista base system. Is there any compatibility issues?
QuestionWhen Building the project ObjectLocator.h(63) : error C2440: 'return' : cannot convert from 'schar *' to 'char *' Pin
Hashan Randev Fernando2-Mar-12 21:42
memberHashan Randev Fernando2-Mar-12 21:42 
AnswerRe: When Building the project ObjectLocator.h(63) : error C2440: 'return' : cannot convert from 'schar *' to 'char *' Pin
MahendranG3-May-12 1:42
memberMahendranG3-May-12 1:42 
GeneralRe: When Building the project ObjectLocator.h(63) : error C2440: 'return' : cannot convert from 'schar *' to 'char *' Pin
skingomez13-Aug-12 5:22
memberskingomez13-Aug-12 5:22 
Questionnot found the facedetection.dll Pin
wudrans27-Feb-12 23:30
memberwudrans27-Feb-12 23:30 
Questionnot found FaceDetection.dll Pin
hamedyar222-Dec-11 8:52
memberhamedyar222-Dec-11 8:52 
Questioneye detection Pin
boragökova20-Nov-11 1:57
memberboragökova20-Nov-11 1:57 
QuestionApplication doesnt work by default Pin
AmmarMMG29-Oct-11 23:53
memberAmmarMMG29-Oct-11 23:53 
Questionneed help rotating face for improving face recognition Pin
mehwish8731-Aug-11 3:56
membermehwish8731-Aug-11 3:56 
AnswerRe: need help rotating face for improving face recognition Pin
laiza ramal8-Nov-12 4:25
memberlaiza ramal8-Nov-12 4:25 
QuestionLost file cv.h Pin
Michael.hg.zhou17-Aug-11 20:17
memberMichael.hg.zhou17-Aug-11 20:17 
GeneralNot bad... A little too hit and miss though. Pin
Danny Mosquito5-Jun-11 7:52
memberDanny Mosquito5-Jun-11 7:52 
GeneralRe: Not bad... A little too hit and miss though. Pin
AmmarMMG29-Oct-11 23:54
memberAmmarMMG29-Oct-11 23:54 
Generalcode of our project Pin
vedaprakash.M.G3-May-11 22:19
membervedaprakash.M.G3-May-11 22:19 
GeneralRe: code of our project Pin
laiza ramal8-Nov-12 4:26
memberlaiza ramal8-Nov-12 4:26 
Generalcannot find clsFaceDetectionWrapper, missing any assembly reference? Pin
Thaanya24-Apr-11 23:09
memberThaanya24-Apr-11 23:09 
General0 faces found Pin
1_stev13-Mar-11 11:21
member1_stev13-Mar-11 11:21 
Generalfacedetection.dll Pin
1_stev13-Mar-11 11:17
member1_stev13-Mar-11 11:17 
Generalhi Pin
1_stev11-Mar-11 7:28
member1_stev11-Mar-11 7:28 
Question[ask] expression recognizing Pin
aan8725-Jan-11 21:53
memberaan8725-Jan-11 21:53 
GeneralErro while building FaceDetection project Pin
SanjayAYadav1-Nov-10 21:49
memberSanjayAYadav1-Nov-10 21:49 
GeneralError : error LNK1104: cannot open file 'cv.lib' Pin
SanjayAYadav1-Nov-10 20:58
memberSanjayAYadav1-Nov-10 20:58 
GeneralError in running the sample codes Pin
UpdateAk478-Oct-10 10:55
memberUpdateAk478-Oct-10 10:55 
Generalvery happay to see article that is posted by my senior Pin
sohaik15-Aug-10 7:51
membersohaik15-Aug-10 7:51 
Questionopen cv Pin
jyotsnakathuria3-Aug-10 3:35
memberjyotsnakathuria3-Aug-10 3:35 
GeneralMy vote of 1 Pin
purohit_rashmi26-Jul-10 19:42
memberpurohit_rashmi26-Jul-10 19:42 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 7 Aug 2008
Article Copyright 2008 by Zeeshan Ejaz Bhatti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid