Thursday, January 12, 2012

Using train cascades

The detector I trained back in November doesn't work for me anymore, so I'm attempting to train another one using the train cascades executable provided in the OpenCV framework.

I gathered 70k negatives , 30k positives and I run exactly the same code and boom crash burn. All I got was the message "Train dataset for temp stage can not be filled. Branch training terminated."

After 2 hours of screwing around all possible combinations and reading the same problems on the net I decided to look at the code (damn messy C++ code). After that I decided to reduce the number of negatives to the same number of positives and voila it worked.

I've come to believe that OpenCV was originally written by a couple of uber programmers and then left to be wrecked by drunken monkeys.


  1. hi!

    great work! :)
    do you have the XML of your haar?
    do you share it? i was hoping that i could use it for my project.
    pls.reply..thanks a lot!


  2. even I am facing the same error as you are....and even after specifying the number of positive and negative images to 1000 in the command promt, I am facing the same problem...Can you help me out???

    I am trying to train classifiers for different hand gestures

  3. Hi!
    Great work using the haar-cascade training to obtain the hands. I have a question how you train the classifier. Let me explain, I had created the .vec file with positives images, and when I use the opencv_haartraining the I have some error, the same that you report but even if I use the same number of images in the txt file (negative).
    The error is the following "Unable to obtain positive samples in function cvCreateTreeCascadeClassifier".
    Can you help to train the classifier?

  4. I am having the same error. I tried your trick, making the same numbers of positive and negative images, but no luck. Same error.

    I dig into the code. The error originates from imagestorage.cpp; when it tries to load the negative images, it fails to do so. I checked the path and the total number of negative images, both are correct. I just cannot figure out why it does not load negatives. Anybody else overcomes this error? plz help.

    1. Hi there Xiaolei,

      Did you manage to figure out what was wrong? I am having the same problem. I have tried different values for -numPos and -numNeg and it seems to affect how far the training gets, but it never seems finish.


    2. Hi again,

      Just to let you know I think I solved the problem. The textfile file with my negative images (called negative.txt) had an extra line at the bottom saying negative.txt

      This is because I created it with ls > negative.txt in the directory with negative images. It seems the haar trainer randomly selects negative images from the list, so every-time it tried to load an image called negative.txt it would crash with that error.

      So either just remove the extra line in the file each time or create it like this rather:
      find ./ -name "*.jpg" -printf "%f\n" > negative.txt

      Hope that helps someone (if in fact this was the problem), I wasted a fair bit of time debugging this.

    3. Hey Ross , I get a lot of people on this post so , I will quote your answer and maybe you can put it yourself in stackoverflow because a lot of people have lost weeks over this.

    4. I found opencv_traincascade gives a lot of problems if the path to the negative samples info is not to its liking.

      a) Run the code from the same directory that contains the text file listing the negative images (e.g. neg_info.txt).
      b) do not include the path to the neg_info.txt file


  5. This comment has been removed by the author.

  6. Hello, I am new to opencv and cascade classifiers: what kind of size ( -w -h options) should I use in opencv_createsamples ? With the default option of 24x24 the samples are very fuzzy ! When I generates good size samples like 96x96 then opencv_traincascades crashes with a bad_alloc error :( Can anyone tell me what I should try ?

    Thank you for any help.

  7. It appears that my problem is a memory problem: program exceeds to 2GB and triggers an error (line 52 from core/alloc.cpp)

    Any idea to get away from that ?

  8. i was facing the same error message... then finally got to know that in my negative samples were greater than no. of entries i had for them in the .txt file...

  9. Man, same numbers, tried everything else. thanks!! :D

  10. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    PHP Training in Chennai