Rhonda Software

Highest quality full cycle software development.

Expert in areas of Computer Vision, Multimedia, Messaging, Networking and others. Focused on embedded software development. Competent in building cross-platform solutions and distributed SW systems.

Offer standalone custom solutions as well as integration of existing products. Opened for outsourcing services.

Visit us at: http://www.rhondasoftware.com

OpenCV Haartraining: Detect objects using Haar-like features

Posted on : 02-06-2009 | By : rhondasw | In : OpenCV

217

OpenCV’s standart cascades allow to detect faces and eyes.  I wanted to create cascade in similar way to detect another objects:  pringles or plate for example.  I found some material in Net how to use OpenCV training tools, also I investigated training tool’s source  code myself  to found out, what training parameters can be tuned.

Prepare images.

For training, I needed thousands of images, containing my object with different lightning conditions and perspectives. After trying to find required number of pictures with Google ,  I understood, that it’s really difficult task =).  So I decided to take video with my object,  then I wrote simple program to crop object from video, frame by frame.  In such way,  I generated about 3000 positive samples (cropped images  with my object).  Resolution varied from 50×50 to 100×100.  The advantage of this method – you  get many samples with different reflections, illuminations and backgrounds.  It’s very important, that all these images “features” are various!

Negative samples I got  from Internet public databases…, there were about 5000 images of different resolution, which did not contain my objects.   I worked with BMP format, due to compression artefacts in some compression levels of JPEG.

Sample/Test data creation.

So I split all my images into 4 sets:

  • Positive images, containing my object – about 3000 images, for training.
  • Negative images – about 5000 images, for training.
  • Positive images – about 100 for testing.
  • Negative images – about 100 for testing.

Of course, I could use the same images for testing and training, but it’s not good.  Positive images should be be packed to OpenCV vec-file.  it can be done with createsamples program from OpenCV.  When packed, all images are resized to 20×20 – it’s good size for training speed.  Also during object detection, object with smaller size will not be found.   For negative samples,  I only created negative.dat(list of negative file names) with win dir \b command.

Training.

OpenCV implements Adaboost algorithm in haartraining. exe.  This tool  generates xml cascade file, which is used in haardetection tool for object detection.  There are some main characteristics of cascade:  number of stages, type of classifiers on each stage. These parameters can be configured when you launch haartraining. I configured it as per my training data (positives and negatives).

Training is finished, when required false alarm of cascade is achieved.  So cascade can contain less stages than nstage – it’s ok. It’s rather worse, when we achieve required number of stage, but required false alarm is not. It’s signal, that something is wrong either with content or with haartraining parameters.  Also maxfalsealarm is false alarm of stage, false alarm of cascade is multiplication of stage’s alarms.

I used P4 2.7GHZ with 2 GB and it took about !6 days! of permanent working to finally get cascade.  If you have Hyper Threading, you can recompile haartraining.exe with OpenMP support, it will speed up process a little bit =).

Testing.

Finally I got haarcascade.xml file. OpenCV perfomance.exe tool can be used for testing cascade.  I tested it on my testing content  – sorry for pun =).

Comments (217)

Hi… I am currently trying out face detection through parallelization… working on vs2008..
can u help me in how to write code of face detection to be multi threaded..

Hi John,
We don’t think that parallel code of viola-jones will be faster. Viola-Jones algorithm has random memory access. With two threads on the same image, it could work slowly due to memory access. We have the synchronous fast face detection see http://www.computer-vision-software.com/blog/2009/06/fastfurious-face-detection-with-opencv/. We have even more faster own face detection. If you are interesting it, you could discuss it with our marketing team (see about page for details).

Aleksey

Thanks for ur reply….
Can u suggest me the face detection using OpenMP….
and also how to recognize the detected faces… Wat database should i use for it ? ? ?

Hi

I got a problem with the performance test. Training worked so far (I guess). I used 200 positives and 800 negatives for a first run and after 11 nodes it stopped. When I try to test my cascade with performance.exe I get nothing. Same happens if I try to use one of the OpenCV implemented haarcascades:
C:\OpenCV\bin>performance.exe -data haarcascade_frontalface_alt.xml -info test_p
os_val.txt
+================================+======+======+======+
| File Name | Hits |Missed| False|
+================================+======+======+======+
| Total| 0| 0| 0|
+================================+======+======+======+
Number of stages: 22
Number of weak classifiers: 2135
Total time: 0.000000
22
0 0 -1.#IND00 -1.#IND00
0 0 -1.#IND00 -1.#IND00
0 0 -1.#IND00 -1.#IND00

Any suggestion?

thx

my test_pos_val.txt looks like this:

test_pos_val/img1.pgm
test_pos_val/img2.pgm
test_pos_val/img3.pgm
test_pos_val/img4.pgm
test_pos_val/img5.pgm

Try “test_pos_val/img1.pgm 1 140 100 45 45″ format of line in your test_pos_val.txt (please, read other comments in the topic for more details).

thx, I didn’t find any hints regarding the performance.exe.

Hi,

You say :
“Training is finished, when required false alarm of cascade is achieved. [...] . Also maxfalsealarm is false alarm of stage, false alarm of cascade is multiplication of stage’s alarms.”

But we cannot set false alarm of cascade, so how to know when the training will be finished exactly ?
Because, it won’t never reach nstages, and reach HitRate and FalseAlarm during a stage doesn’t mean it is OK…just mean step to the next stage…

Thanks,

There is a line in code (cvhaartraining.cpp):

required_leaf_fa_rate = pow( (double) maxfalsealarm, (double) nstages ) / nleaves;

so, when you define maxfalsealarm for one level and number of stages, application calculates required false alarm of whole cascade.

Also do you know if working with splits and tree is usefull ?
Is it in anyway faster for future detection ?
Thanks for your answer

I believe, influence of number of splits on quality of cascade is not well defined matter. Try different numbers and compare results for your particular task. I saw references to nsplits equaled 1, 2 and 4. There is no significant imact on detection speed as, in any case, the most of tested windows are filtered at initial levels. Good long cascade will save more time during detection than bad short.

Hello
I am having problems while executing haartraining.

I create the training set and everything seems ok (checked with flag -show activated), but training for example 20 stages, from stage nº 5 I get this message “Premature end of JPG file”, and a few stages later it ends with the message “Required leaf false alarm rate achieved. Branch training terminated”

When I test the xml file obtained its not able to detect anything, and I dont know what the problem is

Looking forward for any help :D

Thanks

I got a question regarding different CPUs for haartraining.

I noticed that the results differ a lot from the same date with different CPUs…how is that possible?
Tested it on 2 single cores, 1 dual core and 1 quadcore with the same parameters and data. The number of features calculated each stage are very different. Stage 5 f.e. goes from 7 on quadcore to 65 on singlecore and the gap gets bigger in higher stages.

Is there an explenation for this?

[...] of training and test images. (For example, see these Haartraining articles by Naotoshi Seo, Rhonda Ltd. and Florian Adolf). I find this surprising and disappointing, since we humans can learn to [...]

hi
I need to implement haar-like features in my application
can you please tell me how can I choose the exact features for my objects??
is there a relation or rule??

Write a comment

Spam Protection by WP-SpamFree