Click here to Skip to main content
Click here to Skip to main content

Face and Eyes Detection Using OpenCV

By , 7 Aug 2008
 
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:

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

About the Author

Zeeshan Ejaz Bhatti
Web Developer
Pakistan Pakistan
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionfacedection.dllmembernilooofar13 May '13 - 21:04 
QuestionEye detection in VB.netmemberZahid Gul10 Mar '13 - 19:16 
QuestionI want to FaceDetection.dllmemberSorum31 Oct '12 - 21:32 
Questionfacedection.dllmembergsmsdn29 Oct '12 - 21:35 
QuestionPlease I need Facedetection.ddlmemberrichmasua3 Oct '12 - 8:50 
QuestionX64memberprogrammerdon24 Sep '12 - 10:32 
Questionfacedetection.dllmemberCerber24052 Sep '12 - 9:38 
Question[My vote of 2] face detect though MFC?memberMember 842634320 Aug '12 - 22:00 
QuestionAdaboostmemberFadol31 Jul '12 - 23:42 
QuestionThis code in not working on my systemmemberAhmad Usman8 Jul '12 - 16:02 
QuestionWhen Building the project ObjectLocator.h(63) : error C2440: 'return' : cannot convert from 'schar *' to 'char *'memberHashan Randev Fernando2 Mar '12 - 20:42 
AnswerRe: When Building the project ObjectLocator.h(63) : error C2440: 'return' : cannot convert from 'schar *' to 'char *'memberMahendranG3 May '12 - 0:42 
GeneralRe: When Building the project ObjectLocator.h(63) : error C2440: 'return' : cannot convert from 'schar *' to 'char *'memberskingomez13 Aug '12 - 4:22 
Questionnot found the facedetection.dllmemberwudrans27 Feb '12 - 22:30 
Questionnot found FaceDetection.dllmemberhamedyar222 Dec '11 - 7:52 
Questioneye detectionmemberboragökova20 Nov '11 - 0:57 
QuestionApplication doesnt work by defaultmemberAmmarMMG29 Oct '11 - 22:53 
Questionneed help rotating face for improving face recognitionmembermehwish8731 Aug '11 - 2:56 
AnswerRe: need help rotating face for improving face recognitionmemberlaiza ramal8 Nov '12 - 3:25 
QuestionLost file cv.hmemberMichael.hg.zhou17 Aug '11 - 19:17 
GeneralNot bad... A little too hit and miss though.memberDanny Mosquito5 Jun '11 - 6:52 
GeneralRe: Not bad... A little too hit and miss though.memberAmmarMMG29 Oct '11 - 22:54 
Generalcode of our projectmembervedaprakash.M.G3 May '11 - 21:19 
GeneralRe: code of our projectmemberlaiza ramal8 Nov '12 - 3:26 
Generalcannot find clsFaceDetectionWrapper, missing any assembly reference?memberThaanya24 Apr '11 - 22:09 

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

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