Click here to Skip to main content
15,892,697 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 976.7K   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

 
AnswerRe: ...so OpenCV? Pin
Zeeshan Ejaz Bhatti22-Mar-08 7:24
Zeeshan Ejaz Bhatti22-Mar-08 7:24 
Questionlook for eyes Pin
ilya sh12-Mar-08 19:42
ilya sh12-Mar-08 19:42 
GeneralCant seem to run it Pin
aethersg8-Mar-08 20:59
aethersg8-Mar-08 20:59 
GeneralRe: Cant seem to run it Pin
Zeeshan Ejaz Bhatti8-Mar-08 22:36
Zeeshan Ejaz Bhatti8-Mar-08 22:36 
GeneralRe: Cant seem to run it Pin
aethersg9-Mar-08 3:19
aethersg9-Mar-08 3:19 
GeneralProblems to run the program Pin
Member 37050634-Mar-08 15:56
Member 37050634-Mar-08 15:56 
GeneralRe: Problems to run the program Pin
Zeeshan Ejaz Bhatti4-Mar-08 21:02
Zeeshan Ejaz Bhatti4-Mar-08 21:02 
GeneralExceptions Pin
Ivan Svogor29-Feb-08 6:30
Ivan Svogor29-Feb-08 6:30 
I'm getting unhandled exceptions when I download and run your project. Can you help me out?

It says:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.IO.FileLoadException: Could not load file or assembly 'FaceDetection, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)
File name: 'FaceDetection, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.Runtime.InteropServices.COMException (0x800736B1): This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)
at TestEyeDetection.frmEyeTest.frmEyeTest_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)




************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
TestEyeDetection
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Documents%20and%20Settings/Ivan/Desktop/TestEyeDetection_-_Release/TestEyeDetection.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitdebugging="true">


When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
GeneralRe: Exceptions Pin
Zeeshan Ejaz Bhatti29-Feb-08 7:45
Zeeshan Ejaz Bhatti29-Feb-08 7:45 
GeneralRe: Exceptions Pin
Ivan Svogor29-Feb-08 23:41
Ivan Svogor29-Feb-08 23:41 
GeneralRe: Exceptions Pin
Zeeshan Ejaz Bhatti1-Mar-08 6:29
Zeeshan Ejaz Bhatti1-Mar-08 6:29 
GeneralRe: Exceptions Pin
Ivan Svogor1-Mar-08 8:20
Ivan Svogor1-Mar-08 8:20 
GeneralRe: Exceptions Pin
Zeeshan Ejaz Bhatti1-Mar-08 8:56
Zeeshan Ejaz Bhatti1-Mar-08 8:56 
GeneralRe: Exceptions Pin
Ivan Svogor1-Mar-08 9:37
Ivan Svogor1-Mar-08 9:37 
GeneralRe: Exceptions Pin
Zeeshan Ejaz Bhatti1-Mar-08 21:25
Zeeshan Ejaz Bhatti1-Mar-08 21:25 
GeneralRe: Exceptions Pin
Ivan Svogor1-Mar-08 21:42
Ivan Svogor1-Mar-08 21:42 
GeneralRe: Exceptions Pin
kennixpeng14-Jul-09 23:07
kennixpeng14-Jul-09 23:07 
QuestionPlease how can i solve this problem? Pin
kjh1052hsh27-Feb-08 21:47
kjh1052hsh27-Feb-08 21:47 
AnswerRe: Please how can i solve this problem? Pin
Zeeshan Ejaz Bhatti29-Feb-08 7:46
Zeeshan Ejaz Bhatti29-Feb-08 7:46 
AnswerRe: Please how can i solve this problem? Pin
Zeeshan Ejaz Bhatti1-Mar-08 6:29
Zeeshan Ejaz Bhatti1-Mar-08 6:29 
AnswerRe: Please how can i solve this problem? Pin
Zeeshan Ejaz Bhatti1-Mar-08 9:03
Zeeshan Ejaz Bhatti1-Mar-08 9:03 

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.