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

FAQ: OpenCV Haartraining

Posted on : 10-11-2009 | By : rhondasw | In : OpenCV

130

Hi All, before posting your question, please look at this FAQ carefully! Also you can read OpenCV haartraining article.  If you are sure, there is no answer to your question, feel free to post comment.  Also please, put comments about improvement of this post.  This post will be updated, if needed.

Positive images


Why positive images are named so?

Because a positive image contains the target object which you want machine to detect. Unlike them, a negative image doesn’t contain such target objects.

What’s vec file in OpenCV haartraining?

During haartraining positive samples should have the same width and height as you define in command “-w -h size”.  So original positive images are resized  and packed as thumbs to vec file. Vec file has header: number of positive samples, width, height and contain positive thumbs in body.

Is it possible to merge vec files?

Yes, use Google, there are free tools, written by OpenCV’s community.

I have  positive images, how create vec file of positive samples?

There is tool in C:\Program Files\OpenCV\apps\HaarTraining\src createsamples.cpp.  Usage:

createsamples -info positive_description.txt -vec samples.vec -w 20 -h 20

What’s positive description file?

The matter is that, on each positive image, there can be several objects. They have bounding rectangles: x,y, width, height.  So you can write such description info of image:

positive_image_name  num_of_objects x y width height x y width height …

Text file, which contains such info about positive images is called description file. So during vec file generation,  really objects are packed, but not whole image. Essentially vec file is needed to speed up machine learning.

Do I always need description file, even if I have only one object on a image?

Yes, with createsamples you need description file.  If you have only one object, it’s bounding rectangle may be bounding rectangle of whole image. If you want, write your own tool for vec file generation =)

Should lightning conditions and background be various on positive images?

Yes, it’s very important. On each positive image, beside object, there is background. Try to fill this background with random noise, avoid constant background.

How much background should be on positive image?

If you have much background pixels on your positive images in comparison with object’s pixels – it’s bad since the haartraining could remember the background as feature of positive image.

If you don’t have background pixels at all – it’s also bad. There should be small background frame on positive image

Should all original positive images have the same size?

No, original images can have any size.  But it’s important that width, height of this rectangle have the same aspect ratio as -w -h.

What’ s  -w and -h should I put in createsamples? Should it be always square?

You can put any value to -w and -h depend on aspect ratio of the target object which you want to detect.  But objects of smaller size will not be detected! For faces, commonly used values are 24×24, 20×20. But you may use 24×20, 20×24, etc.

Errors during vec file generation: Incorrect size of input array, 0 kb vec file,

-First check you description file: positive_image_name should be absolute path name without spaces like “C:\content\image.jpg” not “C:\con tent\image.jpg” or relative path name.

-Avoid empty lines in description file

-Resolution of original positive image file should be not less, then -w -h parameters you put.

-Check that positive images are available in your file systems and not corrupted.

-There can be unsupported formats. Jpeg, Bmp, PPM are supported!

Example of vec file generation!

Let’s working directory be C:\haartraining. In it there is createsamples.exe. There is folder

C:\haartraining\positives. So create description file positive_desc.txt.

positives\image1.jpg 1 10 10 20 20

positives\image2.jpg 2 30 30 50 50 60 60 70 70

or

C:\haartraining\positives\image1.jpg 1 10 10 20 20

C:\haartraining\positives\image2.jpg 2 30 30 50 50 60 60 70 70

You should avoid empty lines and empty space in image’s path

createsamples -info positive_desc.txt -vec samples.vec -w 20 -h 20


Negative images


What negative images should I take?

You can use any image of OpenCV supported formats, which does not contain target objects (which are present on positive images). But they should be various – it’s important! Good enough database is here

Should negative images have the same size?

No. But the size should not be less than -w -h, which were put during vec file generation.

What’s description file for negative image?

It’s just text file, often called negative.dat, which contains full path to negative images like:

image_name1.jpg

image_name2.jpg

Avoid empty lines in it.

How many negative/positive image should I take?

It depends on your task.  For real cascades there should be about 1000 positive images and 2000 negative images e.g.

Good enough proportion is  positive:negative = 1:2, but it’s not hard rule! I would recommend first to use small number of samples, generate cascade, test it, then enlarge number of samples.

Launch haartraining.exe (OpenCV\apps\HaarTraining\src)


Example of launching

Working directory is C:\haartraining with haartraining.exe tool and samples.vec file.

Let’s negative images are in C:\haartraining\negative, in this case negative.dat should be like this:

negative\neg1.jpg

negative\neg2.jpg

So in C:\haartraining launch this:  haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20  -minhitrate 0.999 -maxfalsealarm 0.5 -npos 1000 -nneg 2000 -w 20 -h 20 -nonsym -mem 1024

  • w  h  is the same, you put during vec file generation
  • npos nneg  – number of positive samples and negative samples
  • mem – RAM memory, that program may use
  • maxfalsealarm – maximum false alarm, that stage may have. If big false alarm – it could be bad detection system
  • minhitrate – minimal hit rate, that should stage have at least
  • nstage – number of stages in cascade

What’ s falsealarm and hitrate of stage?

You should read theory of adaboost about strong classifier. Stage is strong classifier. In short:

  • For example you have 1000 positive samples. You want your system to detect 900 of them. So desired hitrate = 900/1000 = 0.9. Commonly, put minhitrate = 0.999
  • For example you have 1000 negative samples. Because it’s negative, you don’t want your system to detect them. But your system, because it has error, will detect some of them. Let error be about 490 samples, so false alarm = 490/1000 = 0.49. Commonly,put false alarm  = 0.5

Are falsealarm and hitrate depend on each other?

Yes, there is dependency. You could not put minhitrate = 1.0 and maxfalsealarm = 0.0. .

Firstly, the system builds classifier with desired hitrate, then it will calculate it’s falsealarm, if the false alarm is higher than maxfalsealarm, the system will reject such classifier and will build the next one. During haartraining you may see such:

N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+—-+—-+-+———+———+———+———+
| 0 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272|

HR – hitrate

FA – falsealarm

What’s falsealarm and hitrate of whole cascade?

Cascade is linked list (or three) of stages. That’s why:

  • False alarm of cascade = false alarm of  stage 1* false alarm of  stage 2* …
  • Hit rate = hitrate of  stage 1 * hitrate of stage 2* …

How many stages should be used?

  • If you set big number of stages, then you will achieve better false alarm, but it will take more time for generating cascade.
  • If you set big number of stages, then the detection time could be slower
  • If you set big number of stages, then the worse hitrate will be (0.99*0.99*… etc). Commonly 14-25 stages are enough
  • It’s useless to set many stage, if you have small number of positive, negative samples

What’s weighttrimming, eqw, bt, nonsym options?

Really all these parameters are related to Adaboost, read theory. In short:

  • nonsym – If you positive samples are not X or Y symmetric, put -nonsym, -sym is default!
  • eqw – if you have different number of pos and neg images, it’s better to put no eqw
  • weighttrimming – for calculation optimization. It can reduce calculation time a little, but quality may be worse
  • bt – what Adaboost algorithm to use: Real AB, Gentle AB, etc.

What’s  minpos, nsplits, maxtreesplits options?

These parameters are related to clustering. In Adaboost different week classifier may be used: stump-based or tree-based.  If you choose nsplits > 0, tree-based will be used and you should set up minpos and maxtreesplits.

  • nsplits – minimun number of nodes in tree
  • maxtreesplits – maximum number of nodes in tree. If maxtreesplits < nsplits,  tree will not be built
  • minpos – number of positive images, that can be used by one node during training.  All positive images are splitted between nodes. Generally minpos  should be not less than  npos/nsplits.

Errors and stranges during haartraining!

  • Error (valid only for Discrete and Real AdaBoost): misclass – it’s warning, but no error.  Some options are specific to D and R Adaboost.  So your haartraining is ok.
  • Screen is filled with such | 1000 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272| – your training is cycled, restart it. First column should have value < 100
  • cvAlloc fails. Our of memory – you give too much negative images or sample.vec is too big. All these pictures are loaded to RAM.
  • Pay attention you put the same -w and -h, as during vec file generation
  • Pay attention, that number of positive samples and negative samples, you put in -npos -nneg are really available
  • Avoid empty line in negative.dat file
  • Required leaf false alarm rate achieved. Branch training terminated – it’s impossible to build classifier with good false alarm on this negative images. Check your negative images are really negative =),  maxfalsealarm should be in [0.4-0.5]

OpenCV XML haarcascade


During haartraining, there are txt file in haarcascade folder, how can we get XML  from them?

There is OpenCV/samples/c/convert_cascade.c. Use like:

convert_cascade –size=”20×20″ haarcascade haarcascade.xml

How can I test generated XML cascade?

 There is OpenCv/apps/HaarTraining/src /perfomance.cpp. You need have positive images(not used during training) and positive description file.  Use like:

performance -data haarcascade -w 20 -h 20 -info positive_description.txt -ni

performance -data haarcascade.xml -info positive_description.txt -ni

Time and Speed of haar cascade generation


Average time to generate cascade on PC?

It depends on task and your machine.  I generated cascade for face detection, for this used such parameters: -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 4000 -nneg 5000 -w 20 -h 20 -nonsym -mem 1024.  It took 6 days on Pentium 2.7GHZ 2GB RAM.

What is OpenMP?

The OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared memory multiprocessing programming in C, C++ and Fortran on many architectures, including Unix and Microsoft Windows platforms“. If you have MT processor, you can use it.  In code you should add OpenMP defines and put compile options.  For example in VisualStudio2005: Properties->C/C++->Language->OpenMP support

Is it possible to improve speed of haartraining?

Yes, one of possible ways is to use parallel programming. We have realized OpenCV haartraining using MPI for linux cluster. You can read it here

Object detection with OpenCV XML cascades


Is it possible to detect rotated faces?

Yes. It is impossible to generate cascade, which can detect face in all orientations. But you can generate cascade for each orientation separately. For this you need positive content of rotated faces. You can try to generate cascade with OpenCV , add -mode ALL, with it tilted haar feature will be used. But it’s badly implemented, at least in OpenCV 1.1. If you want you can add your own feature to opencv haartraining – it’s not too hard.

Another approach is to write head pose estimator. Then rotate your pictures, so that you have frontal face and detect it with OpenCV default face cascade

Is it possible to recognize gender, attention, race with  Haar features?

We tried, but could not do it with OpenCV haartraining. That’s why for such classification, we used our own gender and attention classificators. Of course you can use Adaboost for this task, which is implemented in haartraining, but we did not get good results.

Is it possible to detect faces in real time?

Yes.  On PC default OpenCV facedetector takes about 200 ms for 640×480 picture, about 5fps – it’s not real time. We have changed facedetector and get about 15 fps – which is real time. You can see results  here and here

Comments (130)

Mr I have only 2 positive samples at hand ,how to set my negative samples? I mean how many should be used?

How can I solve invalid background description file error ?

Please send me a e-mail. duman.tugberk@gmail.com

Hello all,
Thanks for the article.
I am done with haartraining but when I try test the performance with the code: performance -data haarcascade.xml -info positive_description.txt -ni
I always get the error : unable to load classifier from haarcascade.xml
What can cause this ?

I have a problem with haartraining .exe
it does not stop the first value of the result keeps on increasing.
Can you Sir please suggest what can cause that?

Hi,

I have a problem with haartraining. OpenCV performance always doesn’t work with my test samples. For example I put just for experiment 100 positives and 100 negatives samples to check if my classifier works ok. Unfortunately I always get 0 hits, 100 missed, 0 false.

I have run the sample code from C:\Program Files\OpenCV\apps\HaarTraining\src createsamples.cpp. using visual studio 2008 and it display the message below

LINK : fatal error LNK1104: cannot open file ‘VCOMPD.lib’

I have recoded the OpenCV traincascade, and so therefore I read carefully the code of both C and C++ version of it (old and new OpenCV). One information here is innacurate (or completly wrong) as maxtreesplits IS ABSOLUTELY NOT RELATED TO NSPLITS!

You see nsplits is the number of nodes inside the weak classifiers (The Adaboost theory). So nsplits =1 means : all weak classifiers are decision stump (one threshold on one numerical feature). and nsplits = x means you are building your Adaboost Stage out of several decision trees. Of course Adaboost could use some other (in fact any) form of machine learning algorithm as weak classifier, but I guess OpenCV decided that decision trees were the weaker and therefore the faster and more efficient weak classifiers.

On the other hand maxtreesplits is the number of nodes inside the cascade itself! It means that, instead of following only one line and putting either all true at the left and all false at the right, it splits true in different clusters that, depending on their Adaboost score of the previous stage, will follow one path or another or stop here if they are considered as false examples.

I hope this will help somebody as maxtreesplits is actually a powerfull option to use in haartraining and traincascade module.

HI How do I train a own classifier in Mac with Objective-C

Hello,

i build my cascade which contains 15 stages so it’s stored in format “cascade” ( files in txt) to convert it into .XML i use ” convert_cascade but it don’t works ! i try again with haarconv it’s don’t work also !

Some one has an other method to do the conversion ?
thanks and sorry for my english !

Hi all,

I’m building a system in order to recognize gestures with several detectors, each detector is trained to detect a single gesture.The negative images for a one gesture are provided by the positives images of the other gestures.

After 20 stages I get the message “Required leaf false alarm rate achieved. Branch training terminated” but I’m not sure it is because of the training has reached its goal. Rather it seems that the classifier was not able to correctly separate the examples because they are quite similar. As you explain in the article: “it’s impossible to build good classifier with false alarm on this negative images. Check your images are really negative = negative), maxfalsealarm should be in [0.4-0.5]”

What do you think is happening?

Thanks!

I have about 15000 cropped images with the object of interest (positive samples) and 7000 negative images (non object of interest). The cropped images have a resolution of 48×96 and are placed in a folder. The .txt file containing the positive samples looks something like this : picture1.pgm 1 0 0 48 96 meaning that there is 1 positive sample in picture 1 from (0,0) to (48, 96). Likewise I have a .txt file for negative images.

The command for training is the following: c:\libraries\OpenCV2.4.1\opencv\built\bin\Debug>opencv_haartrainingd.exe -data d ata/cascade -vec data/positives.vec -bg c:/users/desktop/obj/negatives.txt -npos 15660 – nneg 7129 -nstage 14 -mem 1000 -mode ALL -w 18 -h 36 -nonsym

But at some point I always get this error :

Parent node: 0

*** 1 cluster ***
OpenCV Error: Assertion failed (elements_read == 1) in unknown function, file C:
\libraries\OpenCV2.4.1\opencv\apps\haartraining\cvhaartraining.cpp, line 1858
How can I overcome this ??? Any help is appreciated. Many many thanks

i meet this problem now . did you solved it? could you please give me some slot?
i compile the code of opencv2.4 with visual studio 2010.thank you ^-^~
my email is chinahuping@hotmail.com~

you will find a subject “Why always OpenCV Error: Assertion failed (elements_read == 1) in unknown function ?” by searching in google.

Hi.
I’m working on haartraining , I read many tutorials , but it not clear to me what to do with the cropped images. createsamles.exe works with original positive images or not?
Thank You.
Marco

Does Anyone Have any tutorial about their experience working with the new opencv_traincascade?
I would like to see the new options.

Thanking you,
Benzun

thanx for help to understand how to use haartaring im using this method to create xml file
but i want detect door object . And i can send u my door xml file. Is detecting door object possible in opencv ????
pls help me

thanx
dushyantha

hi all

I try now some parameters but allways the same error on training…

opencv_traincascade -data data -vec positives.vec -bg negatives.txt -numPos 77 -numNeg 1000 -w 24 -h 24 -mode ALL
PARAMETERS:
cascadeDirName: data
vecFileName: positives.vec
bgFileName: negatives.txt
numPos: 77
numNeg: 1000
numStages: 20
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: ALL

Stages 0-16 are loaded

===== TRAINING 17-stage =====
<BEGIN
POS count : consumed 77 : 77
NEG count : acceptanceRatio 1000 : 4.52278e-07
Required leaf false alarm rate achieved. Branch training terminated.

did anybody have an idea for me? i work with a ubuntu and opencv 2.4.2
thx for your help

regards

this is a very useful article..I also generated a haar cascade for traffic sign recognition.. i used only 400 positive and 500 negative samples.. that did give a fairly accurate result.. what i want to know is can i train the same cascade to detect two or more different traffic signs…? that is to give several traffic signs as objects for positive samples..? will that confuse the classifier which one to detect..?
thank you..

Hi Senjez,
Whats the database you have used for Traffic signal recognition. BTW, did you had a chance to check it with multiple signs

Regards,
Vijay

Hi, I’m trying detect traffic sign too, but when I use a algortim for detec this is slow, your is in real time?

thanks !!

When running on windows 7 for hours, haartraining shows up a window that tells “haarrtraining.exe has stopped working” then I Re-run the haartraining and it continue then stop again at the same point or at another with the same message.
how can I solve this problem knowing that I’m enabling openMP?
Thanks In advance

Hi,
After using haartraining.exe, i see it create cascade file and cascade.xml.
I do not know,whether i can use cascade.xml to my work or i have to use convert_cascade.exe to convert an intermediate haartraining output dir tree data into a other xml file
Please give me advice

I input “opencv_haartraining.exe -data myxml -vec pos.vec -bg neg\neg.txt -nstages 14 -npos 2706 -nneg 7857 -w 20 -h 20 -mem 1024 -mode BASIC”

I would like to submit a problem I encounter while I input -npos 2706 in cmd, and the output shows like this:

“Data dir name:myxml
Vec file name:pos.vec
BG file name: neg\neg.txt,is a vecfile:no
Num pos:2000
Num neg:7857”

my negative samples is 20*20

I wanna know why they are different

hello, im working on face detection using haar training. i am getting the error “premature end of jpeg file”. can someone please help me. im using 1400 neg images, 400 positive images . i get this error in the 11th stage. should i reduce the no. of stages of my training?
i have been stuck here for a long time now. the link provided above to download the negative dataset has also been tested and so is the other database which i collected from internet.
help would be really appreciated/

Why is “Chosen number of splits:0”, “Total number of splitts:0” always? -nsplits 10 maxtreesplits 11

opencv_haartraining.exe fa=0 no end run?

i used haartraining for face training,,but i have a problem, just like what you said, “Screen is filled with such | 1000 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272| – your training is cycled, restart it. First column should have value < 100" ,,,, and i restart it ,,,but it
's all the same,i change the neg samples,,it just the same like before,,i don't know how to do with it!!can you give me some advices?

[…] FAQ: OpenCV Haartraining […]

i want some example like for find the smiled face
in the video File .

Hi all,
I can not create my own Haar Classifier for detecting objects.
I tried all possible combinations of parameters but nothing to do.
Haartraining sometimes crashes, convert_cascade if it works creates a xml identical to that of haartring and more errors…
Please, please help me

Hi there;

I am working on a PoC using Haar Cascade to identify and count objects on a tray. The object in focus is not symmetric at all and it may fall onto the tray in several possible positions which may result in completely different forms for the very same object. For instance, if it falls on its back it will show up a circle with a logo (with a few different colors and texts), but if it falls up-side-down it will look like a bowl in a variety of solid colors… this means… the object may have a different look depending on the side you are looking at, and we may have a bunch of pieces falling together on the tray, each one in a different position and they may not be of the same color, but still the same kind of object. Is it possible to train one only XML to identify such object or is it better to train 2, 3 or 4 specific for each possible position or variety?

Write a comment