OpenCV Eye Detection

Feb 13, 2009 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

In this article I will show you how to detect eyes using OpenCV's boosted cascade of haar-like features. Source code, XML eye classifier, and sample images are available for download.

This project was much inspired by the work of Dr. Matthias Wimmer and his team. They have a project named EyeFinder that looks great. Unfortunately they don't provide the source code, only the eye classifiers are available. So I try to write similar application to detect human eyes using the eye classifier they provided.

To detect human eyes, first we have to detect the face. We can easily do this using OpenCV face classifier. After we have successfully detect the face, we estimate the location of the eyes and perform eye detection. We need to estimate the eyes location in order to reduce false detections.

The main function is pretty simple. It initialize some variables, load the image, call the detectEyes function, and display the image. I will explain the eye detection function below.

Eye Detection Function Explained

Given an image, the function will try to detect human face in it. If it find any, it will continue with the eye detection. Otherwise it will stop.

Listing 1: Detect human face

  1. void detectEyes(IplImage *img)
  2. {
  3.     /* detect faces */
  4.     CvSeq *faces = cvHaarDetectObjects(
  5.         img,            /* the source image */
  6.         cascade_f,      /* the face classifier */
  7.         storage,        /* memory buffer, created with cvMemStorage */
  8.         1.1, 3, 0,      /* special parameters, tune for your app */
  9.         cvSize(40, 40)  /* minimum detection scale */
  10.     );
  11.  
  12.     /* return if not found */
  13.     if (faces->total == 0) return;
  14.  

The face detector will put the list of detected faces into faces. To make things simple, we assume that there is only one face in the image so we'll take the first detected face in faces.

Listing 2: Get the first detected face

  1.     /* get the first detected face */
  2.     CvRect *face = (CvRect*)cvGetSeqElem(faces, 0);
  3.  
  4.     /* draw a red rectangle */
  5.     cvRectangle(
  6.         img,
  7.         cvPoint(face->x, face->y),
  8.         cvPoint(
  9.             face->x + face->width,
  10.             face->y + face->height
  11.         ),
  12.         CV_RGB(255, 0, 0),
  13.         1, 8, 0
  14.     );
  15.  
  16.     /* reset buffer for the next object detection */
  17.     cvClearMemStorage(storage);
  18.  

Now that we have a detected human face, we estimate the location of the eyes before try to find them. We simply estimate the location as: 1/3 of the face height, a few pixels from the top.

Listing 3: Estimate the eyes' position

  1.     /* Set the Region of Interest: estimate the eyes' position */
  2.     cvSetImageROI(
  3.         img,                    /* the source image */
  4.         cvRect(
  5.             face->x,            /* x = start from leftmost */
  6.             face->y + (face->height/5.5), /* y = a few pixels from the top */
  7.             face->width,        /* width = same width with the face */
  8.             face->height/3.0    /* height = 1/3 of face height */
  9.         )
  10.     );
  11.  

The code above sets the Region of Interest (ROI) of the image, so the next object detection will be performed only on that particular location. This will not only increase the speed, but also reduce false detections. Next we detect the eyes.

Listing 4: Detect the eyes

  1.     /* detect the eyes */
  2.     CvSeq *eyes = cvHaarDetectObjects(
  3.         img,            /* the source image, with the
  4.                            estimated location defined */
  5.         cascade_e,      /* the eye classifier */
  6.         storage,        /* memory buffer */
  7.         1.15, 3, 0,     /* tune for your app */
  8.         cvSize(25, 15)  /* minimum detection scale */
  9.     );
  10.  

The object detector will put the list of detected eyes into eyes. Now we can simply draw a rectangle for each eye available in eyes.

Listing 5: Draw rectangles

  1.     int i;
  2.    
  3.     /* draw a rectangle for each detected eye */
  4.     for( i = 0; i < (eyes ? eyes->total : 0); i++ ) {
  5.         /* get one eye */
  6.         CvRect *eye = (CvRect*)cvGetSeqElem(eyes, i);
  7.        
  8.         /* draw a red rectangle */
  9.         cvRectangle(
  10.             img,
  11.             cvPoint(eye->x, eye->y),
  12.             cvPoint(eye->x + eye->width, eye->y + eye->height),
  13.             CV_RGB(255, 0, 0),
  14.             1, 8, 0
  15.         );
  16.     }
  17.  

Don't forget to reset the Region of Interest, otherwise OpenCV will only display the detected eyes when you call cvShowImage. And we don't want that!

Listing 6: Final touch

  1.     /* reset region of interest */
  2.     cvResetImageROI(img);
  3. }
  4. /* end of detectEyes() */
  5.  

Image Gallery

Here are some sample images that was tested using my code. In case you are curious, she's my gf.

Sample images and results.
Fig 1. Sample images and results.

As we can see from the images above, the eyes were successfully detected. Not so perfect, but enough I think.

Related Articles

Recommended Books

The Downloads

77 Comments

ABHAY SHANKAR on Feb 17, 2009:

well i am abhay from india. iam doing a research on face detection algorithms. i have already developed a wrapper in c which identifies faces and draws a rectangle over each of the human face.

now the main problem where iam stuck is to compare another face with those that are already present in the actual picture. i need to calculate that how much( in percentage) that second picture matches the first one containing lots of faces. i need to develop a wrapper (in c or c++) for that. can u help me.

Nash on Feb 18, 2009:

You can compare both images with template matching. But I'm not sure that it will give proper result since both images --even face of the same person-- will vary alot.

Btw, from what you're saying, it seems like you are doing face recognition rather than face detection. Unfortunately I have a very little experience with the subject.

Krishna Chaitanya Kotra on Feb 17, 2009:

Hello,
Is there any code for facial expressions. It is very helpfull if anyone post this code.
Thank you,
Regards,
Chaitanya

Kalani on Mar 4, 2009:

Hi,
I need to compare two faces.The second face might differ in size and a little in lightning conditions.I'm very new to OpenCV and image processing so any kind of help or advice is appreciated.

scliem on Mar 5, 2009:

Hi,
I would like to ask, which type of compiler you use? I use Bloodshed Dev-C++, but it keep mention "Source File Not Compiled", and I look into the code, I can't locate where is the input for the picture..

Nash on Mar 5, 2009:

@Kalani

I have a very limited experience about face comparison. Sorry.

Nash on Mar 5, 2009:

@scliem

I use MinGW, a free gcc port for Windows. Note that Dev-C++ is a text editor, not a compiler. But they provide a package with MinGW included.

I suspect that you haven't setup Dev-C++ correctly for compiling OpenCV programs. For more information about this, please read how to configure Dev-Cpp under windows.

For manual compilation, type the command below from your DOS box (assuming you have installed OpenCV in C:\OpenCV):
gcc -I"C:\OpenCV\cv\include" -I"C:\OpenCV\cxcore\include" -I"C:\OpenCV\otherlibs\highgui" eyedetect.c -o eyedetect -L"C:\OpenCV\lib" -lcxcore -lcv -lhighgui

vasanth on Mar 5, 2009:

hey i am interested on this... i have no basics on it ... i am student on electronics what i have to do first..

Nash on Mar 5, 2009:

Install OpenCV first
Learn the basics from OpenCV wiki
Try to compile the code above

Kalani on Mar 16, 2009:

Hi, Is there a possibility to get the eye locations and calculate the gap between eyes? (To compare two faces which will differ in size)

Nash on Mar 16, 2009:

I think it's possible. But you need to be able to estimate the real length of an object (in centimeters or inches) given its length in pixels. A friend of mine have done something like this using stereo camera system. I'll ask him about this.

Kalani on Mar 18, 2009:

I can give the length of the image in pixels. I saw in an article FaceIt® SDK can be used to get the locations of eyes but it has to be purchased. I would very much appreciate if you could let me know any method.(Looking forward to see your friend's reply)
Thanks in advance

hany on Mar 18, 2009:

hello
i am hany from indonesia, i want to know how to make a database like haarcascade_ eye.xml.
can you help me?

Nash on Mar 18, 2009:

@Kalani

Dude, I've got a bad news. My friend's project is about estimating the distance between the camera and the object, not the length/width/height of the object.

Nash on Mar 18, 2009:

@hany

Hi, I'm from Indonesia too
I didn't make the eye classifier, I obtained it from the site of EyeFinder. They provide the eye classifier in old format, and I converted it to XML file.

If you really want to make such a classifier, here's How-to build a cascade of boosted classifiers based on Haar-like features.

vamsee damarla on Mar 19, 2009:

Hi, I'm from India
i am doing a project on face & eye detection using opencv
I am using visual c++ 2008 express edition,while debugging the code i got error like "assertion failed if(argc==2) at line 14"
i think that error is regarding the input image
so where i have to place that image? besides where i should place the haarcascades classifiers?
my debug/output directory is in "documentsvisual studio projects"
I want detailed information abt how to implement.
pls reply soon.
thks.

Nash on Mar 19, 2009:

The program is designed to be used from command line. e.g:

eyedetect myphoto.jpg

It takes a parameter, the filename of the source image. When you fail to provide the parameter, the program will stop (the system will say 'assertion failed'). Since you run the program from VC++, you can't provide the parameter as above.

So here's what you have to do:
1. Remove the line: assert(argc == 2);
2. Replace the line

img = cvLoadImage(argv[1], 1);

with this:

img = cvLoadImage("myphoto.jpg", 1);

This will make the program automatically load myphoto.jpg without reading the command line parameters. You can also specify the image's path like C:/images/myphoto.jpg.

Tips: put all of the files in the same directory to make things simple.

hany on Mar 20, 2009:

can you give me the old format of eye classifier in http://wwwradig.in.tum.de/people/wimmerm/se/project.eyefinder/ because it's url is no longer exist. and can you explain each tag in haarcascade_eye.xml
and also can you explain your method about estimate the distance between camera and object

thanks

Nash on Mar 21, 2009:

Too bad, the url is no longer exist. I've sent you the eye classifier in old format to your email. convert it to xml using cascade2xml.c provided in the files section of the yahoo opencv group.

I think the tags in haarcascade_eye.xml are not really important. What important are, the __structures__ created based on the file. You can view the detailed description of the Boosted Haar Classifier structures in OpenCV Reference Manual.

Currently I'm still learning about distance estimation, so I don't have much to share yet.

Annie Sun on Mar 25, 2009:

Hi,Nash. Thank you very much for your sharing the code with us. I test the eye detect code, It is very cool. But sometimes, Only one eye can be detected? Have you ever been meet this problem. What do you think the reason of this.
Another Problem is if you have the mouth and nose classifier? Have you ever been consider the eyes detect with eyeclasses or sunclasses? Thank you very much for your reply!

Nash on Mar 26, 2009:

Hi Annie,
Yes, sometimes only one eye can be detected or even none at all! This happens especially for small images. For medium and large images like the samples above, the eye detector works just fine. Maybe we have to train the classifier with more images to make it better.

vamsee damarla on Mar 26, 2009:

It's great ,works fine but there is problem with the images having eyeglasses.
thanks a lot.

Jap on Apr 2, 2009:

Please help me. I appreciate it.

1) I use the php wrapper for OpenCV from http://www.xarg.org/project/php-facedetect/
How can I use "eyedetect" through with this wrapper?

2) I compiled the eyedetect.c without errors, but when I run it as ./eyedetect myphoto.jpg, I get error:
./eyedetect: error while loading shared libraries: libcxcore.so.1: cannot open shared object file: No such file or directory
where am I supposed to place this file?

3) can this func return array of x,y,w,h of eyes? TY

Nash on Apr 3, 2009:

I have never tried PHP facedetect extension before. But from the manual, you can just replace the face classifier with the eye classifier. However, the extension lacks other OpenCV functions, such as Image of Interest. So it is almost impossible to port the code above into PHP.

---

It looks like your system cannot find the OpenCV shared library. There are 2 ways to workaround this.

Option 1: set the OpenCV library path through the environment variable LD_LIBRARY_PATH. Say your OpenCV library path is /opt/opencv-1.0/lib,

$ LD_LIBRARY_PATH=/opt/opencv-1.0/lib:$LD_LIBRARY_PATH
$ export LD_LIBRARY_PATH


Option 2: Compile the the code above using static library rather than shared library.

$ gcc -Wall -static `pkg-config --cflags opencv` eyedetect.c -o eyedetect `pkg-config --libs opencv`

---

Note that the code above returning the x, y, w, h of eyes. See this line:

r = (CvRect*)cvGetSeqElem( eyes, i );

r is a CvRect type. the x,y,w,h are used to draw red boxes by the lines below it.

Jap on Apr 3, 2009:

Dear Nash

Than you very much for quick help. Finally, I got rid of all the compile errors and I ran it as ./eyedetect upclose.jpg

Then I got this error:
(upclose.jpg:938): Gtk-WARNING **: cannot open display:

I am using shell command-line to execute this eyedetect on the web server.

I like to get rid off the display/window stuff, BUT I like to return the "r" array for both eyes as an output.

Nash, could you please modify the eyedetect.c to do this for me? Thank you in advanced.

peace

Nash on Apr 4, 2009:

It's easy. To get rid off the GUI stuff, remove the GUI related code. Here they are:

cvNamedWindow(argv[1], 1);
cvShowImage(argv[1], img);
cvDestroyWindow(argv[1]);

jap on Apr 6, 2009:

Dear Nash

I removed those and compiled without errors. After running the code from command line, there is no changes on the image although coordinates are there. However, I realized something else. The coordinates are not correct even if it can draw a red line. I tried this with many different images, we still couldn't get the correct coordinates. Could you please help me again? Sorry being annoying to you.

/* draw a rectangle for each eye found */
for( i = 0; i < (eyes ? eyes->total : 0); i++ ) {
  r = (CvRect*)cvGetSeqElem( eyes, i );
       
  printf("x:%d;y:%d;w:%d;h:%d\n", r->x, r->y, r->width, r->height);
       
  cvRectangle(
    img,
    cvPoint(r->x, r->y),
    cvPoint(r->x + r->width, r->y + r->height),
    CV_RGB(255, 0, 0),
    1, 8, 0
  );
}
Wrong Coordinates:
[root@localhost opencv-1.0.0]# ./eyedetect ery2.jpg
x:131;y:19;w:60;h:36
x:38;y:36;w:43;h:26

Nash on Apr 6, 2009:

That's because the image's Region of Interest. Keep in mind that we had set the ROI to estimate the eyes' location and we haven't release it yet. To get the correct coordinates, you have to adjust it with the ROI area.

Replace the line:

printf("x:%d;y:%d;w:%d;h:%dn", r->x, r->y, r->width, r->height);

with this:

/* get the region of interest */
  CvRect roi = cvGetImageROI(img);

  /* print coordinates */
  printf(
    "x=%d y=%d w=%d h=%dn",
    roi.x + r->x,
    roi.y + r->y,
    r->width,
    r->height
  );
This should solve your problem.

Jap on Apr 7, 2009:

For those who wants to use Nash's eye library in the PHP code, here is the quick solution.

exec("/path/to/lib/opencv-1.0.0/eyedetect $path_to_img_file", $coordinates, $status);

* as long as you printf the coordinates from eyedetect.c, then you can capture it as an array in PHP, then you can do some image processing with ImageMagick or GD library. I found this library pretty powerful.

Shwet on Apr 11, 2009:

Hi. Can anyone suggest other (as many as possible) algorithms for eye detection please. I need code for eye detection which uses two different algorithms and then I have to compare results obtained from the 2. Please I really need help, only 1 day left. Thanks.

Najib on May 19, 2009:

Hi, can i have a look at the algo of the eye detection method that u use above? i can have access to the paper that u said being ur main reference... or maybe u can provide us with the paper?

Nicolas Bourré on May 27, 2009:

Hi,

I haven't coded in C for a while. I'm getting compilation error under VS 2008. I configured all the necessary includes and libraries (well I think). But I still get the error at the line

CvRect *r = (cvRect*)cvGetSeqElem(faces, 0);

The compiler says :
error C2275: 'CvRect' : illegal use of this type as an expression d:\sources\OpenCV_Eye_Detection\eyedetect.c 62

What does that mean?

Nash on May 27, 2009:

VC++ is buggy. use mingw instead.

ProfessionalAmatuer on Jun 2, 2009:

does anybody know if when using the eye detector with live video feed will it detect closed eyes. can the person blink to switch off the detector?

Pisal on Jun 4, 2009:

@Nicolas

Try renaming the file from eyedetect.c to eyedetect.cpp. VS seems to treat .c and .cpp files differently.

jen on Jun 15, 2009:

thanks for the sample code. was of great help.
i have one question though, how do you train the classifier with more images to make it better?
im using face detection only, there are times when some faces are not detected.
again, thanks a lot.

Nash on Jun 15, 2009:

Hi,
I obtained the classifier from somewhere else and I don't have any experience on how to build one
But here's the howto.

chocobo on Jun 22, 2009:

hey

i think what you've done is very good

n by providing your source code definitely shed some light on me on this topic. yet i can't seem to compile it. I've gone through all the process in opencvwiki on setup. btw, im using devc++ - n when i compile it, there is no error, but when i try to run it, it wont work.
there is an error message saying, "the source code is not compiled" n I've tried more than 5 times but its still the same.

any suggestion?

Nash on Jun 22, 2009:

Hi,
try to compile the code from command line. see this comment for detail.

felix on Jun 24, 2009:

Hi Nash,

Pls can you share any code you have on facial expressions with me? I would really appreciate it bro.

Thanks in advance.

Nash on Jun 24, 2009:

Hi,
Sorry but I don't have one. btw, what do you mean with facial expression code? is it like when we load an image with an angry person in it, then the program will say "Man, this guy is pissed off!", or what?

felix on Jun 24, 2009:

Nash,

What I mean by facial expression - when u smile the program will recognize it as "smile", when you're angry, it will recognize it as as "angry" etc based on your response I think you have an idea. I'll appreciate if i have something from u.

Thank u.

Rishi Anand on Jun 26, 2009:

Dear Nash, I'm from India, I'm a student of computer science engineering. I am currently doing an internship at a software firm in New Delhi. I have just started using opencv. I was facing difficulty in loading images using the following statement:

IplImage *img=cvLoadImage("imagename.jpg", CV_LOAD_IMAGE_COLOR);

I was however able to load the image by giving the path of the image in the command arguments section in the properties and then using:

img = cvLoadImage(argv[1]);

Is there anything that i have to implement to make this statement work. Also can you please explain to me how one can load more than one image and then display both. Thanks

Nash on Jun 26, 2009:

Make sure that the image is in the current directory, or just supply the full path. If your image is C:\vision\images\photo.jpg, then load the image with:

IplImage* img = cvLoadImage("C:/vision/images/photo.jpg", CV_LOAD_IMAGE_COLOR);

---

To load and display many images, e.g:

eyedetect image1.jpg image2.jpg image3.jpg

You can use this code in the main():

/* ...initilization here... */

for(i = 1; i < argc; i++) {
  IplImage* img = cvLoadImage(argv[i], CV_LOAD_IMAGE_COLOR);
  detectEyes(img);
  cvNamedWindow(argv[i], CV_WINDOW_AUTOSIZE);
  cvShowImage(argv[i], img);
}

/* ...memory deallocation here... */

htchien on Jun 29, 2009:

Hi Nash:

Nice sample, it helped a lot. Keep up the good work.


@Nicolas Bourre:

You should change this code:

CvRect *r = (cvRect*)cvGetSeqElem(faces, 0);

to this code:

CvRect *r = (CvRect*)cvGetSeqElem(faces, 0);

then it should work.

cvRect() is a function call in OpenCV to make a instance of CvRect object. That's why VC++ told you it's a illegal use.

Regards.

k09 on Jul 3, 2009:

Sorry for trouble you but I can't run this code on Linux. It was compiled well but couldn't run. What's the problem? (I copied haar and eyes classfier to the same folder with exe file).

I used this code for tracking eye with webcam but it wasn't cool at all. To track eyes with webcam, what's the best method? Can you help me figure out it?

Thank you so much!

Nash on Jul 4, 2009:

What is the message from your Linux system? It should complain about something.

---

The eye classifier performs very bad for live video feed, that's why I used static images instead .
There are several papers out there about eye detection, and I'm trying to implement some of them. I hope I can find the algorithm that could work with live video feed.

Updated July 16, 2009:
Check this one out: Real Time Eye Tracking and Blink Detection.

Nasir on Oct 15, 2009:

Dear sir,

Upon compile your program;

CvSeq* eyes = cvHaarDetectObjects(
  img,
  cascade_e,
  storage,
  1.15,
  3,
  0,
  cvSize ( 25, 15 )
);


i got this error;
Error 15 error C2275: 'CvSeq' : illegal use of this type as an expression

May i know how to resolve this problem?

Thank you

Nash on Oct 15, 2009:

You know, VC++ is suck. Get MingGW and start hacking OpenCV.

Nasir on Oct 19, 2009:

Dear Sir,

I learnt a lot from your website.
Currently, i am creating an XML file to detect my mouth.

Have you ever created your own xml files?
Coz the recognition for my own set of mouth seem to be rather poor.

Thank you

Nash on Oct 19, 2009:

I've never create one. btw, why don't you share your XML in this forum.

rameez on Oct 20, 2009:

can anyone of u plz tell me ..........how to get all coordinates values or image data values of detected face or eyes...........not whole data.......plz reply quickly as quickly as u can.............

Nash on Oct 20, 2009:

This might help:
#comment-27
#comment-28

rameez on Oct 21, 2009:

yes i have got all 4 side of coordinates of rectangle........but i want the data or pixel value or coordinate value of all the data contains in the rectangle.......i m working on face recognition so i want to compare two detected faces.............so i need all the data within the rectangle.......so can u plz tell me............??

Nash on Oct 26, 2009:

I've added some examples about accessing ROI pixels in my other post: OpenCV Region of Interest (ROI). Hope that helps.

Bobby on Nov 1, 2009:

hi

i am doing a project on eyes detection. Using a webcam to detect yr eyes whether you are sleeping and if your eyes is closed for more then 10 seconds then there will be a alarm sound produced. So it there any way you can help me?

Nash on Nov 1, 2009:

My blink detection code will be a good start.

Nirav on Feb 18, 2010:

hi, hope u r doing well... We are working on facial expression project... as a part of that, to detect the eye, when i execute your eye detection code in visual studio 2008, I am geting the following error, "Assertion failed: cascade_f && caccade_e && storage && img, on line 34" Can you please suggest some solution? Thanking you in advance... Nirav

Nash on Feb 19, 2010:

Place the files:

haarcascade_eye.xml
haarcascade_frontalface_alt.xml

in the same directory with the main code. Also see this comment.

Nirav on Feb 22, 2010:

hi Nash,

I got the solution....Thanks a lot....

Nirav

Md. Abdul Momin on Feb 28, 2010:

Dear Sir,

I am trying to detect surface defects of citron using openCV. But I do not understand how can I choose the specific defect portion area or how I can select the target objects for image processing as the shape of citrus is circular. I tried using drawing commands but it only shows the circle on the image but I want to analysis this area like ROI but I failed...........Therefore I solicit your kind help in this regards. If possible pls let me know the code of how to select circular ROI for a image.
Thanking you and I am eagerly waiting for your response.

sumon on Mar 2, 2010:

hi nash,

hope u r fine. I am beginner to opencv. If possible please let me know, how can I detect RGB values of an image at different positions using opencv. It will be highly appriciated.

Thanks in advance.

Nash on Mar 3, 2010:

@Abdul Momin:

I've write a sample code for selecting circular ROI using OpenCV. Check it out.

@sumon:

Use cvPtr2D or cvGet2D.

sumon on Mar 4, 2010:

Dear Sir,

Thanks for your cordial reply. As a beginner (truly), I have a special request to you,if possible please write a code for me to detect RGB values of an image at different positions using opencv. Really I will appreciate it. Please try to ur best to help me.

Best regards,
Sumon

Md. Abdul Momin on Mar 4, 2010:

Dear Sir,
Thanks a lot for writing code on selecting circular ROI using OpenCV. Really its great useful for me. Later I will try to learn more from you to solve my project.
Thanks
Momin

sabrina on Mar 4, 2010:

Hi Nash,

Now I am trying to learn opencv. In this connection I am reading your all documents related opencv. Could you let me know what is the meaning of 1,8,0 in the following line.

cvRectangle(
  img,
  cvPoint(eye->x, eye->y),
  cvPoint(eye->x + eye->width, eye->y + eye->height),
  CV_RGB(255, 0, 0),
  1, 8, 0
);


Thanks in advance.
Sabrina

Nash on Mar 4, 2010:

They're the default values for thickness, line_type, and shift. Read the docs.

neeha on Mar 4, 2010:

Hi Nash, Thanks for the tutorial . Could you send me the eye classifier code? I couldn't find it online. Thanks.

Nash on Mar 5, 2010:

@Sumon:
Here's another opencv utility.

@Neeha:
Its on the download links above.

zetha on May 8, 2010:

Hi,

I'm trying to implement your code to python but I'm stuck on the ROI thing. Python doesn't support cvGetImageROI option, so I used cvGetSubRect function. Here is the eye detection part of my code :


eye_region = cvGetSubRect(image, cvRect(face.x, int(face.y + (face.height/4)), face.width, int(face.height/2)))
eyes = cvHaarDetectObjects(eye_region, eyeCascade, memo, 1.15, 3, 0, cvSize(25,15))
for e in eyes:
  # drawing rectangle


But when I run this code, it draws rectangles at false places. Really, I need some help, I couldn't figure out the problem, some help pls

steve on May 28, 2010:

Hi

Just wondering if anyone could help with this problem?, keep getting this...

Program received signal SIGSEGV, Segmentation fault.
In icvBGRx2Gray_8u_CnC1R () (C:\OpenCV2.0\bin\libcv200.dll)
Debugger finished with status 0

When Complier hits this line

CvSeq *objects = cvHaarDetectObjects(img, cascade, storage, 1.2, 2,0, cvSize( x, y ) );

Platform:
windows home XP sp2
OpenCV 2.0 "Pre compiled binary"
CodeBlocks 8.02 + MinGW

The strange thing is it works no problem on a machine same setup running windows vista?, being trying to figure this out for about 3-4 days now no luck even googled "icvBGRx2Gray_8u_CnC1R ()" nothing.

Any help would be greatly appreciated. One thing I'm not trying to do is convert color img to gray at any point in the coding, I'm guessing that's something that cvHaarDetectObjects does to detect objects.

Beats Me

steve on May 31, 2010:

RE: Update Posting “steve on May 28, 2010”

One thing I didn't mention in the previous posting is the fact I was Multi-threading tasks etc, further testing revealed that the cvHaarDetectObjects doesn't seem to like Multi-threading environment on Windows XP system not sure why? even with the SP3 upgrade, but again the same program ran fine on Windows Vista.

Work around, simply took the coding out of the thread and put in the main() function and it worked fine on both systems
cvHaarDetectObjects thread safe/Bug? Not sure, but it would seem to me that it's probably some issue with the different OS threading processes which is causing the cvHaarDetectObjects Segmentation fault that I encountered above.

Anyway hope this helps anyone with the same problem, also to bare this in mind in the design stages.

Best Regards
Steve

vipin on Nov 11, 2010:

i am getting rectangle on eyes

but there are so many unwanted rectangles also in the video while running this code

rectangles are at neck chest area etc

how to reduce these unwanted rectangles ??

Nash on Nov 12, 2010:

The haar classifier is not reliable enough for videos.

lazarus on Nov 27, 2010:

hi

Nash?, just want to ask, what programming software tools that's easy to build this kind of program?

What are your recommendations?


Thanks

Nash on Nov 30, 2010:

1. OpenCV
2. gcc
3. any text editor

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000