Click here to Skip to main content
15,896,912 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 978.6K   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

 
GeneralRe: מי עונה ראשון????לא כולם ביחד!!!! Pin
uvik28-Sep-09 5:06
uvik28-Sep-09 5:06 
GeneralRe: מי עונה ראשון????לא כולם ביחד!!!! Pin
uvik28-Sep-09 5:50
uvik28-Sep-09 5:50 
GeneralRe: מי עונה ראשון????לא כולם ביחד!!!! Pin
uvik28-Sep-09 5:52
uvik28-Sep-09 5:52 
Generalתחרות מי עונה לי ראשון Pin
tziporah124-Jul-09 4:38
tziporah124-Jul-09 4:38 
Generalשאלה לישראלים הגאונים Pin
tziporah124-Jul-09 4:36
tziporah124-Jul-09 4:36 
Questionthis project hoo can help me??? Pin
tziporah124-Jul-09 4:31
tziporah124-Jul-09 4:31 
Generalask for a help Pin
ittoronto22-Jul-09 5:01
ittoronto22-Jul-09 5:01 
GeneralCould not load file or assembly 'FaceDetection....' Pin
ittoronto21-Jul-09 5:33
ittoronto21-Jul-09 5:33 
Hi,

I got exceptions while I run the 'TestDetection'. Here is the content of my running directory following by the exceptions. Did any of you have met the same problem? I am working on Windows XP.

I notice that in the errors FaceDetection version is 0.0.0.0, but it is 1.0.0.0 in loaded. Is it the matter? How could I change it?

Thanks a lot in advance,

PS:

content of the running directory:

07/20/2009 11:13 AM <DIR> Cascades
10/17/2008 10:59 PM 876,544 cv110.dll
10/17/2008 10:59 PM 471,040 cvaux110.dll
10/17/2008 10:52 PM 958,464 cxcore110.dll
10/17/2008 10:52 PM 118,784 cxts001.dll
07/21/2009 07:16 AM 45,568 FaceDetection.dll
07/21/2009 07:16 AM 0 FaceDetection.ilk
07/21/2009 07:16 AM 297,984 FaceDetection.pdb
10/17/2008 10:57 PM 3,813,376 ffopencv110.dll
03/16/2005 07:18 PM 946,032 haarcascade_frontalface_alt.xml
10/17/2008 10:52 PM 630,784 highgui110.dll
10/17/2008 10:52 PM 241,664 ml110.dll
12/01/2006 10:03 PM 548,864 msvcp80.dll
12/01/2006 10:03 PM 626,688 msvcr80.dll
11/03/2006 07:13 PM 1,424,660 ojoD.xml
11/03/2006 07:13 PM 1,078,120 ojoI.xml
07/21/2009 07:16 AM 24,576 TestDetection.exe
12/02/2006 12:46 AM 65,536 vcomp.dll


Exceptions:

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)

************** Loaded Assemblies **************
TestDetection
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/data/DetectEyePosition/TestDetection_v.2/bin/debug/TestDetection.exe
----------------------------------------
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
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
GeneralRe: Could not load file or assembly 'FaceDetection....' Pin
ittoronto22-Jul-09 4:56
ittoronto22-Jul-09 4:56 
Questionget exceptions while run'TestDetection.exe' Pin
ittoronto20-Jul-09 7:43
ittoronto20-Jul-09 7:43 
AnswerRe: get exceptions while run'TestDetection.exe' Pin
Zeeshan Ejaz Bhatti20-Jul-09 8:11
Zeeshan Ejaz Bhatti20-Jul-09 8:11 
GeneralRe: get exceptions while run'TestDetection.exe' Pin
ittoronto20-Jul-09 9:29
ittoronto20-Jul-09 9:29 
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 

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.