Sunday, June 19, 2011

opencv haartraining

Learning opencv haartraining is challengeable, in this tutorial I describe haartraining best practices with my own experience. If you have basic knowledge at image processing , it will be helpful to understand haartraining concept. if you are new to image processing and opencv read some books. i recommend following books .
If you have questions regarding haartrainig don't hesitate to post them here.     

Download haartraining toolkit, vec files and haar classifiers from below link 

Alternative  download link :-

Step 01: Collect postive images of interest object ( at least 1000 positive images )

                         If you have video of the interest object, then you can divided them into frames. All images should be in bitmap format. If you have .jpg or other format you can use "Right click image converter" or other image convertor to convert images into bitmap. learn more about opencv positive samples from here

Step 02: Crop Images

                         Here i'm going to create haar classifier of Side view of human. I hope you have download above toolkit . it contains with few sample images.
Put the all positive images into the "rawdata" folder and then double click and run the Objectmaker.exe to create the "info.txt" file. It would be something similar to this.

rawdata/IFWN-sequence-035.avi-00089-R200.bmp 1 305 57 69 202
rawdata/IFWN-sequence-035.avi-00096-R200.bmp 1 213 58 122 202
rawdata/IFWN-sequence-035.avi-00099-R200.bmp 1 208 61 107 207
rawdata/IFWN-sequence-036.avi-00117-R200.bmp 1 173 61 117 204

Step 03: Add absolute path for generated info.txt file
               For safety, you have to rename info.txt file becuase if you accidently double click the objectmaker.exe, it will cause to erase or change all data on info.txt file. As you can see the above, *.txt file contain with the relative path, but it's hard to create *.vec file with the relative path. So you should add the full path (absolute path) to your *.txt file. you can use append.bat file. right click and edit that bat file. it will looks like something similar to this

"for /F "delims=" %%j in (positiveOLD.txt) do echo.D:/opencvtest/HAAR/GAIT_REC_DET/DATA/Training/%%j >> POSITIVES.txt"

here positiveOLD.txt is without absolute path and  POSITIVES.txt is with absolute path.
once you got the POSITIVES.txt verify it's really added the correct absolute path or not.

Step 04: Generate *.vec file
By createSampleCommand.bat file you can generate the *.vec files. you should change the parameters according to your requirement

Step 05: Verify the *.vec file
By using SampleVerify.bat file you can verify the generated *.vec pressing any key you can browse all cropped images manually. few vec files are located at "sample vec". folder

Step 06: Start Haartraining
Create new folder and put *.vec file, negatives.txt and haartraining.bat file. Double click the bat file and launch the haartraining process. This would be take 60,90 hours to complete and it depends on your sample can't pause this process once it start, it is better use cloud . i used .

Step 07: Check the performance
you can use performance.bat file to check the classifier performance.

Human side view detection from real time stream/video
download source code (visual studio 2008 with opencv 2.2)

Alternative  download link :-



Related post

[ Opencv installation ]

[ Negative images for haartraining ]

[ Positive images for haartraining ]


These are the useful links for haartraining.

[3]. part 1.
      part 2.


  1. what are the features that we generally consider during training of our on haar classifier??

  2. i think i got some troubles with this training !!
    I'm working on Windows 7 & Visual Studio 2010.... so how to do this training ?

  3. Thanks from Turkey, good work.

  4. This process takes a very long time (4-5 days), use the traincascade application instead of the haartraining according to this link and the opencv docs:

    Hope this saves someone the days wasted using haartraining. This is still a good tutorial follow it to step 6. then use traincascade. (Just edit the haartraining batch file.)

    Thanks for the blog.

  5. Great Job Shehan's thanks for posting this one.It is very useful to train data.Keep doing..!!!

  6. How can i update to Opencv 2.4.9 is that commands are changes .Parameter are changes How can i create trained data using OpenCV2.4.9? Can You please update the tool.?

  7. Thank you very much for all this information. I am having trouble with object marker. It opens the image very very large, and will not allow me to resize. I cannot see the bottom half of the image, and only the left and right because I have two monitors. If I call the windows command on right click, the resize option is gray and not working. It will not respond to resize with the mouse. I can move, minimize, and close it, but nothing more. I cannot find settings in objectmarker that will adjust this size.

  8. can you please explain a little how to create negative.txt

  9. mine is getting stuck at haartraining saying insufficient memory in cAlloc dont know what to do i have tried traincascade but thats also not working please if anyone can help i ll be grateful and please be quick i have only few days left before submission.
    thank u.


Related Posts