Home Machine Learning Face Detection using MATLAB Part I
If this site is helpful to you, please consider donating so that I might one day quit my job and do this full time.
Face Detection using MATLAB Part I PDF Print E-mail
User Rating: / 83
PoorBest 
Monday, 27 September 2010 13:17

 

Want to download the code instead of writing it down from this website? get it from HERE

For those following this tutorial or buying the code from the link above I want to clarify a few things:

1. The youtube video you see was created with the code in these pages. I didn't leave anything out.

2. I did not create some kind of adaptive light filter in this code. Yes, that means that in each different lighting, the thresholds have to be
set up manually to get good recognition.

3. This algorithm doesn't work well with images. It was meant to do real time video face detection. The video doesn't scan for different sized faces.

4. Read all comments on all pages. There has been mistakes found. However, even with mistakes, this algorithm worked fine. Why? The mistakes are 
additions and subtractions in the Haar features. Technically, by adding the integral images vs subtracting them, you are still creating a Haar feature.

5. I can't/wont sell the images with the code. You can download the images HERE.

6. I sell the code to help support my server costs. You don't have to buy it. You can follow the whole tutorial here. I just make it easier for both of us.

7. I try my best to respond to all e-mails, but lately I am receiving a lot of e-mails and I might not be able to respond to you.

IMPORTANT - This algorithm was written in MATLAB! Not the best language to write this algorithm in, but I do not think anybody else
has done it in MATLAB, for a reason!

This is my first try at machine learning. I knew very little about it when I started, but I feel that I'm ok enough to give you a tutorial
on what I did and how i perceived this algorithm should be implemented.

I decided that I would implement my version of the Viola and Jones method for face detection. Using Haar like features and
adaptive boosting... blah blah, we don't care about the technicals right? Just want to understand how this is done. so here
is my video of my implementation. I still need to tweak it some more to make some improvements.

 

Yes that is me! I should have hired some beautiful model instead of having my face here, then maybe this page would get more hits.

Anyways, lets get started.

Training

Step 1

To do face detection first we need a dataset of images of faces and non-faces. You need as many as you can
get ( in the thousands or ten thousands range). Because you will do supervised training, which means you are already telling
the computer if each picture is a face or non-face, you need to get those images.

In my case, I obtained a database from MIT which was composed of 2429 faces and 4547 non faces. The images are 19 X 19 ( later I
realized that I should have looked for something in the range of 25 X 25, it seems to capture features better). They are all black and
white images. Here are some of them:

Once you have your own nice database, you need to understand the Haar features. They are rectangles that map over the faces
of people and tell you if you are a face or non face. I will explain how this is accomplished later. In here I will just focus on what
took me a long time to figure out. Why are there so many Haar features in a 19 x 19 Image? So here are what they look like:

There are so many more variations, but these are the only ones I used. So lets take the first one in the list here. A white rectangle
above a black rectangle. If you see your image as a 19 x 19 matrix ( that is from x1 to x19 and y1 to y19), and you start with this
feature being a 1 x 2 in size and in position x1,y1, then you have your first feature:

You would do all your calculations based on this classifier, then move on to the next size, which would be a 1 x 4 in position x1,
y1, your second feature:

And you get the point, it would keep increasing in size.It would be 1 x 8, 1 x 16 and well, it cannot go farther than that.

Then the classifiers would start with a 2 x 2.

And it would continue 2 x 6, 2 x 8, and so on. Eventually, you will come to an end of this classifier. The code for this would be:

The feature matrix explains the first sizes that each of the 5 classifiers can be.

- Each feature (5 total), this is i.

- They all must start at 1 x 2, this is sizeX and sizeY.

- They cannot go over the size of 19 x 19, this is x and y.

- winLength and winWidth are parameters that each feature will increase in size through the image.

CalcBestThresh() is the function called that will see if the feature being tested is a good one or not.

Go to the next section following the link below:

Part II here

 

Last Updated on Wednesday, 05 December 2012 05:11
 

Comments  

 
+9 # john_k 2011-04-17 14:33
Thanks a lot, this is exactly what I was looking for.
Reply | Reply with quote | Quote
 
 
+1 # Dorisssss 2011-05-09 14:39
Thank you very much for uploading the code. But I dont really understand the file 'NormalizeFace.m'. Can you please explain what does this file do? What are face0000, face000, face00, face0 mean?

Thx a lot.
Reply | Reply with quote | Quote
 
 
+2 # ece301 2011-05-22 21:47
Sorry Doris I been in China for two weeks. For this particular .m file you do not have to care about it. When I first downloaded the images, they had weird names. I simply created these scripts to rename them so they could be easy to identify and used for training.
Reply | Reply with quote | Quote
 
 
0 # hung 2011-05-31 10:03
hello,i'm doing a thesis about face detect and recognition.
I don't understand why haar Features(rectan gle black and white) can detect face or how do it dectect face?
Can you explain for me!
Thanks so much!
my Email: boy_nolove_88@y ahoo.com.vn
Reply | Reply with quote | Quote
 
 
+3 # ece301 2011-06-01 20:35
Ok, basically the black and white rectangle is not really black and white. It is just a way to explain that one rectangle is subtracted from the other.

Think about each haar feature being a classifier. Eventually if you have 100 classifiers that are at least 50% right, by probability, all of them together can be very close to being right. Does that make sense?

On the other hand, this subtraction that is happening in each classifier is actually going to be very close for all faces because they are similar. For example all have a dark spot between the eyes, or the eye part in a human is concave so it looks darker than the forehead in a picture. Therefore because of this being similar for all faces we can create features that only faces have and create thresholds for them. Is this good enough for you?
Reply | Reply with quote | Quote
 
 
0 # vincent 2011-06-14 00:08
hi, thanks for the code.
i have a question, when i run imgfacedetect.m to detect my pic, the result show me a lot of face block, can u tell me how should i do, detect face,
my email: saiwoonmee2000@ gmail.com
Reply | Reply with quote | Quote
 
 
0 # ece301 2011-06-15 19:41
Hey Vincent,

I have answered this before, but for you, I will answer it again.

My face detect software works better on video than on images. The reason being that I did not finish this program completely ( I got a job, finished school, etc). The threshold values need to be modified little by little to get a better detection. These threshold values are located in the if statements of cascadeClass. Depending on each environment, or each image, they always have to be changed.

If you would pursue my algorithm further, you should create another algorithm that finds the best thresholds, something I did not do. Hope this answers your question! Thanks for visiting.
Reply | Reply with quote | Quote
 
 
0 # Quentin 2011-06-19 08:38
Hi,thank you so much for the code

I have one question.
The window start at point(1,1).

In your code,

for x=2:imagesz-sizeX
for y=2:imagesz-sizeY


why you choose start at (2,2)?
Reply | Reply with quote | Quote
 
 
+1 # ece301 2011-06-19 18:37
I figured there be no useful data from 1,1 so I wanted to skip the calculations for it. I could be wrong!
Reply | Reply with quote | Quote
 
 
+1 # Dravid 2011-10-18 13:47
Hi
I just saw your demo about face detection and willing to learn it. I am used to with
C, Java but just started Matlab. I went through your Tutorials too but kinda confused.Can you please tell me which is the main file and how should i run so that i can run your code and see the result in my computer.
Thanks a lot for your wonderful effort
Reply | Reply with quote | Quote
 
 
0 # ece301 2011-10-18 14:00
Hello Dravid,

I would suggest you port this code to C or java, your results will be faster.

Also please read all the comments from all the people here as they have caught some errors in my code which I will not fix since I am focusing on android and leaving MATLAB behind :cry: .

As for the code you want to use TestCascade to run from your webcam.

1. Make sure you change the path for weights to the path where you have the weights file.

2. If recognition is not happening, change the calls to CalcHaarCascade inside TestCascade to have a threshold that allows more recognition. On the opposite spectrum if too much recognition is happening, change it so that to allow less recognition.

3. Go to the Evaluation webpage here:
http://www.ece301.com/ml-doc/48-evaluating.html

which explains exactly everything about TestCascade file.

Hope it helps!
Reply | Reply with quote | Quote
 
 
+1 # Dravid 2011-10-22 10:33
1.can you please explain what is weights and weights file
2.I see there is two TestCascade file one is inside Recognizing files and one outside which one i should run as main file.
3.Once i run TestCascade what else i need to change except path to Classifier.mat

Thank You
Reply | Reply with quote | Quote
 
 
0 # ece301 2011-10-23 10:15
Hey Dravid,

1. The weights file is a file that contains the weight that each image has on the classification process. In adaptive boosting each training image gets a weight associated with it depending on how hard or easy it is to classify. I recommend you do a google search on that topic more if you need a more detail example.
2. You should run the one outside. The one with the green square.
3. Run it first and see if your face is getting recognized. if not, then try changing the thresholds in the file until recognition happens. If you read all 4 tutorial pages, you will know that I do not have some kind of adaptive filter to compensate for different lighting environments so the thresholds must always be adjusted.
Reply | Reply with quote | Quote
 
 
0 # Kishore 2012-11-22 12:17
Hi, From where shouldi get weights.mat file
Reply | Reply with quote | Quote
 
 
0 # sepastien 2011-10-26 02:54
can you tell me how to set adaptive filter?
It is cascade?
Reply | Reply with quote | Quote
 
 
0 # ece301 2011-10-30 09:35
Hello sepastien,

Sorry, that would take me a while to do and I don't have a lot of time these days, but yes I imagine a cascade filter would do.
Reply | Reply with quote | Quote
 
 
0 # Joy 2011-10-30 00:16
sir, can i use a larger image on the range of 250x250, will it give me a better feature detection?
Reply | Reply with quote | Quote
 
 
-1 # ece301 2011-10-30 09:37
You could, but image how long your training will take. Also, I don't think you would need to go as far as that big of an image. I seen others use 25 x 25 images and it works much better for them.
Reply | Reply with quote | Quote
 
 
-1 # Joy 2011-11-23 21:24
sir, how can i run this code to implement real time face detection
Reply | Reply with quote | Quote
 
 
0 # saurabh 2012-01-19 01:28
hello sir where i can find the image database?
plz mail me...
Reply | Reply with quote | Quote
 
 
+1 # ece301 2012-01-19 09:34
Hellow Saurabh,

If you download the code from the link above:

http://www.mathworks.com/matlabcentral/fileexchange/31206

You should have the database images there as well. I accidentally put them there. They do not belong to me, they are from MIT I believe.

Also please read the comment threads as people have found small mistakes in my code. Thanks for visiting.
Reply | Reply with quote | Quote
 
 
0 # bigloomy 2012-02-16 04:19
I have a question,the code isn't run,so I hope you can give me your original code.Thank you very much!!! :roll:
Reply | Reply with quote | Quote
 
 
0 # amir 2012-03-17 14:39
I have paid and downloaded the source code, can you give me the instruction how to use the code ?
Reply | Reply with quote | Quote
 
 
0 # ece301 2012-04-18 15:19
Amir,

I believe I helped you out with our exchange of emails, good luck!
Reply | Reply with quote | Quote
 
 
+1 # Dario 2012-04-18 15:05
hey dude can u send me the code... im doing a project of smile recognition... but this gonna help me a lot
dario8ita@gmail .com
Reply | Reply with quote | Quote
 
 
0 # ece301 2012-04-18 15:15
Hey Dario,

You can download the code from the link above. This code is just to follow the tutorial and it costs $1. Some people have bought the code expecting a no effort full code for their projects that only costs them 1 dollar, so be aware this is just if you do not want to write the code presented on these tutorials by hand.
Reply | Reply with quote | Quote
 
 
0 # Heena 2012-11-14 04:13
send me faces n non-faces images.
Reply | Reply with quote | Quote
 
 
0 # ece301 2012-11-22 12:28
Did it.
Reply | Reply with quote | Quote
 
 
0 # Vesaas 2012-11-24 22:23
Hello sir, can you tell me where I place this code (which function)?
Reply | Reply with quote | Quote
 
 
0 # ece301 2012-11-25 05:20
Huh?
Reply | Reply with quote | Quote
 
 
0 # Vesaas 2012-11-30 18:48
The code you place above. Where do you place it? (which function does it locate?)
Reply | Reply with quote | Quote
 
 
0 # bedro 2012-12-14 00:58
thanks for your reply, I want to ask about weights file, how can I create it?? step by step please?? because I am new to matlab. thanks again
Reply | Reply with quote | Quote
 
 
0 # Koji R. Ikehara 2013-01-04 05:11
Hello! I just want to ask what tool did you use for the training? Thanks in advance!
Reply | Reply with quote | Quote
 
 
0 # Menchie Chou 2013-01-29 05:59
Good day Sir!
Sir when we run the tescascade.m in matlab 2012b we got this error:
Error using open (line 100)
File 'C:\Users\BERTHA ACEVEDO\Desktop \MATLAB project\Face Detect
Software Recent 5.25.2010\Face Detect
Software\vars\C lassifiers.mat' not found.

Error in TestCascade (line 8)
W = open('C:\Users\BERTHA ACEVEDO\Desktop \MATLAB project\Face
Detect Software Recent 5.25.2010\Face Detect
Software\vars\C lassifiers.mat');

Sir what should we do with this error?.. Thanks sir :)
Reply | Reply with quote | Quote
 
 
+1 # ece301 2013-01-29 06:33
Hi, you need to change the path of the classifiers file and weights file to yours.
Reply | Reply with quote | Quote
 
 
0 # Myleen Maceda 2013-01-29 21:25
good day sir. I have checked your NormalizeFace.m and I'm confused with this path

'C:\Users\Juan\D esktop\School\M asters\Spring 2010\DIP\MIT CBCL Image set\train\face\ face0000',int2str(i),'.pgm');

I know I have to change the path name but for the images, do I have to rename the pics manually? I'm sorry. I cant understand this part "\face000". Thanks in advance.
Reply | Reply with quote | Quote
 
 
0 # Ria 2013-03-18 00:39
Dear ece301,

I only recently discovered that the feature extraction requires a really long time. Is there any way i can obtained the classifier file after the feature extraction that took 3 weeks.

thank you
Reply | Reply with quote | Quote
 
 
+1 # ece301 2013-03-18 06:49
Those files are already included along with the code as .m files.
Reply | Reply with quote | Quote
 
 
0 # SAM 2013-04-02 20:20
Hi,

Please can you contact me. I used some ideas from your MATLAB code and I want to reference them appropriately but I do not know your name or any information about you.
Reply | Reply with quote | Quote
 
 
0 # Sky 2013-04-14 20:03
It is very helpful!
Reply | Reply with quote | Quote
 
 
0 # hnoo 2013-05-15 18:42
i get this error

Error in ==> StartTraining at 46
CalcBestThresh( x,y,winWidth,wi nLength,i);

what i can do ?
Reply | Reply with quote | Quote
 
 
0 # ece301 2013-05-15 19:36
I need more info to help you. Are all your paths correct ?
Reply | Reply with quote | Quote
 
 
0 # valerio 2013-06-19 02:44
i0ve buyed the articles but i can't download it... why??????
Reply | Reply with quote | Quote
 
 
0 # sanzo 2013-11-20 23:37
Can you give the links for image databases?
Have been following your tutorial. Now the paper makes much more sense. Thanks a lot.
Cheers.
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

Copyright © 2014 ece301.com. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.