4.2

4.2 | 13 ratings Rate this file 396 downloads (last 30 days) File Size: 1.24 MB File ID: #19912

Open CV Viola-Jones Face Detection in Matlab

by Sreekar Krishna

 

13 May 2008 (Updated 21 May 2008)

No BSD License  

Viola-Jones Fast Face Detection Mex Implementation

Download Now | Watch this File

File Information
Description

This zip file contains source code and windows executables for carrying out face detection on a gray scale image. The code implements Viola-Jones adaboosted algorithm for face detection by providing a mex implementation of OpenCV's face detector. Instructions for use and for compiling can be found in the Readme file.

Usage:

FaceDetect (<Haar Cascase XML file>, <Gray scale Image>)

The function returns Nx4 matrix. In case no faces were detected, N=1 and all four entries are -1. Otherwise, N=number of faces in the image and the vector contains the x, y, width and height information of the face.

Example,
A = imread ('lena.jpg', 'JPG');
Img = double (rgb2gray(A));
Face = FaceDetect('haarcascade_frontalface_alt2.xml',Img);

Acknowledgements
This submission has inspired the following:
Face Detection & Recognition System
MATLAB release MATLAB 7.5 (R2007b)
Other requirements For compiling Mex Matlab 7.0.0 R14 or Matlab 7.5.0 R2007b Microsoft visual studio 2003 or 2005
Zip File Content  
Other Files
haarcascade_frontalface_alt2.xml,
bin/cv100.dll,
bin/cxcore100.dll,
bin/FaceDetect.dll,
bin/FaceDetect.mexw32,
Include/cv.h,
Include/cv.hpp,
Include/cvcompat.h,
Include/cvtypes.h,
Include/cvver.h,
Include/cxcore.h,
Include/cxcore.hpp,
Include/cxerror.h,
Include/cxtypes.h,
lib/cv.lib,
lib/cvhaartraining.lib,
lib/cxcore.lib,
src/FaceDetect.cpp,
lena.jpg,
TestFaceDetection.m,
Readme.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (27)
15 May 2008 Sreekar Krishna

Please note that the last but one line of the .cpp file in the 'src' directory - "delete (cascade)" is causing segmentation fault at runtime in the Matlab environment. Please comment or remove this line and recompile as per the "readme.txt" file instructions for a stable program. I will fix this and upload a new version as soon as I figure out how to modify the submission through the administrator.

26 May 2008 john poler  
04 Jun 2008 Mohammad Ali Aktaa  
06 Jun 2008 Mohammad Ali Aktaa

Could you please tell me how to make it track the face.
I tried to make it track the face, but after about two minutes it stoped and show me a message (Insufficient memory(out of memory))
I could nit solve it.

% TRACKING

close all;
clear;
clc;

    clear all;
    imaqreset;
vid = videoinput('winvideo',1,'RGB24_320x240');

w=1;
while (w==1)
    preview(vid);
    frame = getsnapshot(vid);
    image(frame)
    i = frame;
    
Img = double (rgb2gray(i));
Face = FaceDetect('haarcascade_frontalface_alt2.xml',Img);
if(Face~=-1)
Rectangle = [Face(1) Face(2); Face(1)+Face(3) Face(2); Face(1)+Face(3) Face(2)+Face(4); Face(1) Face(2)+Face(4); Face(1) Face(2)];
truesize;
hold on;
plot (Rectangle(:,1), Rectangle(:,2), 'g');
hold off;
end

end

09 Jun 2008 fady michel

to Mohammad Ali Aktaa
the reason of getting the "out of memory" message is that the video object in expanding in the memory with each aquired frame so one solution to your problem is to clear the object every once and a while (NOT DELET IT) i suggest using the "Clear" command

clear vid

12 Jun 2008 Thomas Mensink

Hi!
Do you have also a version for a Mac?
That should be great!

18 Jul 2008 berry giatra

hi ,my name is berry..do you help my?
i not understand source code face detection with source matlab 7..please send my e-mail..
thanks a lot of

15 Sep 2008 Petter Strandmark

Thanks!

03 Oct 2008 Petter Strandmark

There is a memory leak. This code leaks memory:

I = double(rgb2gray(imread('peppers.png')));
for i = 1:1000
FaceDetect('haarcascade_frontalface_alt2.xml',I);
end

I can see multiple objects in the .cpp file that don't seem to be release. But I haven't worked with OpenDV that much. For example, "cascade" and "storage" seem like pointers left on their own.

03 Oct 2008 Petter Strandmark

I have been able to fix (or at least reduce very significantly) the memory leak). I added the following lines at the end:

cvReleaseMemStorage(&storage);
cvRelease(reinterpret_cast<void**>(&cascade));
mxFree(input_buf);

And another line to remove the structuring element:

cvReleaseStructuringElement(&se);

It seems to work now. I might write my own library with these fixes and a possibility to cache the XML data.

07 Oct 2008 Mohammad Ali Aktaa

Thanks,
I'll try it.

30 Oct 2008 Der Baertige

Hi! We built a active face tracker in Simulink using this code. We changed the last lines of the cpp-Part to:

cvReleaseImage( &gray );
cvReleaseMemStorage(&storage);
cvReleaseHaarClassifierCascade(&cascade);

It runs on 6.2 fps with 288x384 Pixel Videoresolution. We tested it for over 20 minutes without crashing.

10 Nov 2008 Sreekar Krishna

Thanks everyone for the inputs about releasing memory. I am not using this program currently, but would be glad to incorporate these changes and post it back if I can get confirmation that certain fix is working.

11 Dec 2008 sara

Hi Sreekar
 can you explain implementation steps. I have not been able to the run this program I would be very gratefully if you could help me.
thank you

18 Dec 2008 Alexander Huth

Hi all - I got this to compile on linux using the following command:

mex FaceDetect.cpp -I../Include -L <path to opencv libraries>/lib*so* -outdir ../bin

31 Dec 2008 ddt99999 Tong

Hi all,

I try to setup the matlab file, the following errors come out:

mex FaceDetect.cpp -I../Include/ ../lib/*.lib -outdir ../bin/
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
 
C:\Users\ADMINI~1\AppData\Local\Temp\mex_xCuIeX\FaceDetect.obj ../lib/cv.lib ../lib/cvhaartraining.lib ../lib/cxcore.lib
   Creating library C:\Users\ADMINI~1\AppData\Local\Temp\mex_xCuIeX\templib.x and object C:\Users\ADMINI~1\AppData\Local\Temp\mex_xCuIeX\templib.exp
FaceDetect.obj : error LNK2019: unresolved external symbol cvGetSeqElem referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvHaarDetectObjects referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvClearMemStorage referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvCreateMemStorage referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvEqualizeHist referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvReleaseImage referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvErode referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvCreateStructuringElementEx referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvCreateImage referenced in function mexFunction
FaceDetect.obj : error LNK2019: unresolved external symbol cvLoad referenced in function mexFunction
..\bin\FaceDetect.mexw64 : fatal error LNK1120: 10 unresolved externals
 
  E:\MATLAB\R2008A\BIN\MEX.PL: Error: Link of '..\bin\FaceDetect.mexw64' failed.
 
??? Error using ==> mex at 207
Unable to complete successfully.

I have no clue about what's going on. Do I miss some files? It seems unlikely.

Thanks

07 Jan 2009 Ravi Varma

Hi All,
I need a help,regarding haartraining xml file
how to understand the xml file?.

05 Mar 2009 Hanaa

Hi all!

I'm doing my final year project on 'Image Processing Methods for facial recognition using Matlab'. I've got Visual Studio 2005 background but never used MatLab before. I've just started on with research, video tutorials and reading.

Can anyone please help me with a guide, where can I start this from? Does this face detection code helps?

Your help would be very much appreciated! :))

21 Mar 2009 sowjanya sripada

hi,we r pursuing a project on face detection using this code.bt we r unable to understand the xml file.pls help us out

13 Aug 2009 Peter Aldrian  
14 Aug 2009 Thomas

Dear all,

This library is only provides binary libraries for Windows. For any other architecture you will certainly need your own libraries.

On linux, I can compile with
(in directory src)

mex FaceDetect.cpp -I../Include/ -LPATHTOOPENCVINSTALLATION/lib/ -lcvaux -lcv -lcxcore -lcvhaartraining -lhighgui -outdir ../bin/

then, I need to start matlab with LD_LIBRARY_PATH set:

export LD_LIBRARY_PATH=PATHTOOPENCVINSTALLATION/lib
matlab

and then it works just fine.

The same should work on mac, and probably there is a similar construct for 64bit windows.

cheers,
thomas

17 Sep 2009 Chris Garson

Dear all,
This looks like it could be very useful, but I am unable to get the code to run. I've compiled using Visual C++ 2008 in MATLAB r2009B and moved the .mexw32 file and both included .dlls to a directory on my path. When I try to call the mex file, I get a message saying "the specified module could not be found".

I noticed that there's a note in the readme stating something about "compiler-specific dll's", but this is unclear. Could this be the source of my problem? If so, where can I find the necessary dll's? Thank you for your time.

~Chris

08 Nov 2009 Raymond Cheng

Thanks for your sharing.

14 Dec 2009 Marcin Eichner

easy to plug-in, it's working straight out of the box.
(checked with opencv 2.0)

09 Jan 2010 Ashar Waseem

I tested this code and it runs fine. but one problem that I have faced while using it is that when I try to localize multiple faces in an image, it does not do it. However, it works great when an image has only one face. Any method to correct it??

12 Jan 2010 Ferran Poveda

I've tested your code on Mac Os X, it works fine :)

1. Compile and install OpenCV (http://bit.ly/1j2pjL)

2. Config your mex compiler if you haven't done it already (mex -config)

3. Compile Sreekar's code with Matlab as easily as:
mex FaceDetect.cpp -I/usr/local/include/opencv -L/usr/local/lib -lcxcore -lcv -lcvaux -lhighgui -lml

Hope it helps to the Mac beginners ;)

And, thanks Sreekar for your code!!!

13 Jan 2010 Ralu

>Dear all,
>This looks like it could be very useful, but I am unable to get the >code to run. I've compiled using Visual C++ 2008 in MATLAB >r2009B and moved the .mexw32 file and both included .dlls to a >directory on my path. When I try to call the mex file, I get a >message saying "the specified module could not be found".

>I noticed that there's a note in the readme stating something >about "compiler-specific dll's", but this is unclear. Could this be >the source of my problem? If so, where can I find the necessary >dll's? Thank you for your time.

>~Chris

Hi!
I have the same problem as Chris, did you post somewhere any answer for this?

Thank you!

Please login to add a comment or rating.
Updates
21 May 2008

The original code was causing segmentation fault in matlab. Removed the last but one line in the source file 'FaceDetect.cpp' - delete(cascade).
Have tested this code up to 1500 continuous face detections without any segmentation faults.

Tag Activity for this File
Tag Applied By Date/Time
application Sreekar Krishna 22 Oct 2008 10:01:05
face detection Sreekar Krishna 22 Oct 2008 10:01:05
matlab implementation Sreekar Krishna 22 Oct 2008 10:01:05
viola jones Sreekar Krishna 22 Oct 2008 10:01:05
fast face detect Sreekar Krishna 22 Oct 2008 10:01:05
hi santhosh kumar 04 Dec 2008 01:58:34
application Xu Ping 14 Apr 2009 07:58:47
face detection Manimekalai 25 May 2009 07:01:20
viola jones Manimekalai 01 Jun 2009 23:54:00
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com