Tuesday, July 30, 2013

Traning Haar cascaded classifier with OpenCv

 Hello Friends,

I have never thought that, I will write a blog someday. But last 2 weeks I struggled a lot to find-out how to use Opencv's utility to train a cascaded classifier. I could not find a single tutorial talking about the newest applications of OpenCV to train cascade classifier i.e opencv_traincascade. All the blogs and tutorials I found only talks about the older version which is opencv_haartraining.

Currently there are two applications in OpenCV to train cascade classifier: opencv_haartraining and opencv_traincascade. opencv_haartraining is now an obsolete application, and also you can find many tutorials talking about it so I will only talk about  opencv_traincascade.

A.  First thing first, Install Opencv. Once you have installed OpenCV look under  your opencv/apps folder you can see two folders "haartraining" and "traincascade". We will use "traincascade" folder for training. And We will use the "createsamples.cpp" from the "haartraining" folder to create the positive samples. I will explain it step by step.


B. First we will make a createsamples.exe by building the .cpp files using the Visual Studio 2010. To do so, follow the below steps-
     a. open Visual Studio 2010 -> new project ->project name(createsample)->empty Project->finish
     b. In the right hand in Solution Explorer->Header Files, right click on Header Files and add exiting files -  "_cvcommon.h" , "_cvhaartraining.h" , "cvclassifier.h", "cvhaartraining.h".
     c. Right click on  Solution Explorer->Source Files, And add existing files - "createsamples.cpp", "cvboost.cpp" , "cvcommon.cpp", cvhaarclassifier.cpp", "cvhaartraining.cpp", "cvsamples.cpp".
     d. Add all the include folders, library folder and additional dependencies in the Project property and build the solution.
      f. You can find the createsamples.exe in you project debug folder.

C.Similarly we will make traincascade.exe. For traincasacde.exe we will add "boost.h" , "cascadeclassifier.h", "haarfeature.h", imagestorage.h" , Ibpfeatures.h", "traincascade_features.h" in the "Heade Files" and in the "Source Files" we will add "boost.cpp", "cascadeclassifier.cpp", "features.cpp", "haarfeatures.cpp", "imagesstorage.cpp", "lbpfeatures.cpp", "traincascade.cpp".

D. Now that we have we have createsample.exe and traincascade.exewe can start the main process.
    a.Create a folder in a fresh path say, C:\haar
    b.Create 2 folders inside C:\haar i. Positive, ii. Negative
    c.Positive Images: These images contain the object to be detected. Inside the positive folder keep all the positive images. Then create a ‘positive.txt’ file which will have the location of the image, number of positive samples present in that images and coordinate of all those samples in the image. Below is the example of ‘positive.txt’ , which contain location of the image positive1, number of positive sample in each image, x & y coordinate of the sample and height and width of the image.

Positive\poisitive1.jpg 1 0 0 92 116
Positive\poisitive2.jpg 1 0 0 92 116
...

    d.After having the positive images and the information text file we will create a “vec” file. During haartraining positive samples should have different height and width , 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.
To create the vec file we will use the command prompt(cmd) and type the follow-

    C:\haar\createsamples.exe –info Positive\positive.txt –vec positive.vec –num 100 –w 24 –h 24

Where 100 is the number of positive samples that are used for training and 24 will be the height and width of the positive samples. This ‘vec file’ will be created at “c:\haar” we can see it by typing the following in the cmd

    C:\haar\createsamples.exe –vec positives.ec –w 24 –h 24

    e.Negative Images: Negative images could be anything that does not contain positive samples. Keep all the negative images in “C:\haar\Negative” and create a ‘negative.txt’ file containing full paths of all negative images. something like this-

Negative\negative1.jpg
Negative\negative1.jpg

...E. Now that we have all the things we need to train a cascade classifier we can call the traincascade.exe from the command prompt. To start training you need to type the following command-

    C:\haar\traincascade.exe -data result/ -info positive.vec -bg negative.txt -numPos 2000 -numNeg 3000 -numStages 20 -w 24 -h 24

Where '-data' specifies the directory where the the result will be saves as a .xml file. The final result of the training will be saved in "cascade.xml" file , you can remove all other files inside the result folder. '-info' specifies the information file of the positive images i. e the .vec file. '-bg' specifies the background file that is the text file having information about the negative images. '-numPos' means the number of positive images and '-numNeg' menas the number of negative images. '-numStages' means the number of stages should be used for the training process. Once the training process starts you can see some thing like this-
Be patient , the whole process might take 4-5 days to complete.



24 comments:

  1. thank you, nice tutorial but I have problem with
    C:\haar\cascadeclassifier.exe -data result/ -info positive.vec -bg negative.txt -numPos 2000 -numNeg 3000 -numStages 20 -w 24 -h 24

    how you get cascadeclassifier.exe ,did you mean traincascade.exe ?
    if it's C:\haar\traincascade.exe
    then I get failed,
    the failure like this:
    Debug assertion failed vc/include/xsting
    any help please.

    ReplyDelete
    Replies
    1. this is the exact message I get:

      ---------------------------
      Microsoft Visual C++ Runtime Library
      ---------------------------
      Debug Assertion Failed!

      Program: ...traincascaded.exe
      File: C:\program files(x86)\microsoft visual studio 11.0\vc\include\xstring
      Line: 1143

      Expression: invalid null pointer

      For information on how your program can cause an assertion
      failure, see the Visual C++ documentation on asserts.

      Delete
    2. Hi, Did you include all the folders?? library folder and additional dependencies in the Project property and build the solution.

      Delete
  2. Hi Mostafiz ,
    To start training, you have mentioned
    C:\haar\cascadeclassifier.exe -data result/ -info positive.vec -bg negative.txt -numPos 2000 -numNeg 3000 -numStages 20 -w 24 -h 24
    is that cascadeclassifier.exe or traincascade.exe ..
    I have build the traincascade and when i am running the command it is getting terminated.

    Can you please help me with the command i need to hit to start training.
    thanks

    ReplyDelete
    Replies
    1. Hi Raviteja,

      1.is that cascadeclassifier.exe or traincascade.exe ?

      - It should be trancascade.exe, I have updated the tutorial accordingly.

      2. To run the trancasdse.exe you need to use the following command-

      C:\haar\traincascade.exe -data result/ -info positive.vec -bg negative.txt -numPos 2000 -numNeg 3000 -numStages 20 -w 24 -h 24

      Make sure all your datas i.e .vec file, negative images are inside 'C:\haar' folder. follow the tutorial step by step, you should not have any problem. best wishes.

      Delete
  3. Hi Mostafiz,
    Thanks for your reply, when i go with
    C:\haar>traincascade.exe -data result/ -vec positive.vec -bg negative.txt -numPos 100 -numNeg 300 -numStages 10 -w 24 -h 24
    I am getting this
    Image reader can not be created from -vec positive.vec and -bg negative.txt.
    and when i replace -vec with -info the traincascade.exe is getting aborted.
    I have all my .vec file and negative images are inside my C:\haar folder itself.
    Can you help where i am missing .

    ReplyDelete
  4. Hi Ravijeta,

    I think some thing is wrong in the create sample step. .vec file is not created properly. please try to use small number of images for test.

    ReplyDelete
  5. HI,
    I got that resolved and now i getting this error.
    C:\haar>traincascade.exe -data result/ -vec positive.vec -bg negative.txt -numPos 100 -numNeg 300 -numStages 10 -w 24 -h 24
    PARAMETERS:
    cascadeDirName: result/
    vecFileName: positive.vec
    bgFileName: negative.txt
    numPos: 100
    numNeg: 300
    numStages: 10
    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: BASIC

    ===== TRAINING 0-stage =====
    <BEGIN
    POS count : consumed 100 : 100
    Train dataset for temp stage can not be filled. Branch training terminated.
    Cascade classifier can't be trained. Check the used training parameters.

    ReplyDelete
  6. Hi Mostafiz,
    C:\haar>traincascade.exe -data result -vec data\positive.vec -bg Negative\negative.txt -numPos 100 -numNeg 300 -numStage 10 -featureType LBP -precalcvalBufSize 1024 -precalcIdBufSize 1024
    PARAMETERS:
    cascadeDirName: result
    vecFileName: data\positive.vec
    bgFileName: Negative\negative.txt
    numPos: 100
    numNeg: 300
    numStages: 20
    precalcValBufSize[Mb] : 256
    precalcIdxBufSize[Mb] : 256
    stageType: BOOST
    featureType: LBP
    sampleWidth: 24
    sampleHeight: 24
    boostType: GAB
    minHitRate: 0.995
    maxFalseAlarmRate: 0.5
    weightTrimRate: 0.95
    maxDepth: 1
    maxWeakCount: 100

    ===== TRAINING 0-stage =====

    OpenCV Error: Unspecified error (Invalid fs.state) in unknown function, file ..\..\..\src\opencv\modules\core\src\persistence.cpp, line 5271


    can please help me out what is the issue with the error i am getting

    ReplyDelete
    Replies
    1. Hi raviteja,

      Sorry for late reply. I was busy with my projects. I have never experinced such error. So I don't know how to solve it. If you already have the solution, please share here.. so that people who are having similar errors will be helpful.

      Thanks,

      Delete
    2. I still have this same problem. I CAN'T FIND ANY SOLUTION =(

      Delete
  7. Hey, I can't make that createsample.exe file....please help me...I have added all the include folders, library folder and additional dependencies in the Project property

    ReplyDelete
  8. It is showing me several errors in _cvcomman.h file and cvboost.cpp file!!!

    ReplyDelete
  9. Adit. Did you configure linker library paths and additional include directory paths?

    ReplyDelete
    Replies
    1. Hi Sushant,

      I've taken following steps:

      In Project> Properties> Configuration Properties>

      Debugging> Environment : D:\openCV\opencv\build\x86\vc10\bin

      VC++ directories>
      Include Directories> D:\openCV\opencv\build\include\opencv2;D:\openCV\opencv\build\include\opencv;$(IncludePath)

      Library> D:\openCV\opencv\build\x86\vc10\lib;$(LibraryPath)

      Under "C++" in Configuration Properties:-
      In General> Additional Include Directories: D:\openCV\opencv\build\include\opencv2;D:\openCV\opencv\build\include\opencv;D:\openCV\opencv\build\include;$(OPENCV_DIR)\..\..\include;%(AdditionalIncludeDirectories)

      Under "Linkers" in Configuration Properties:-
      In General> Additional Library Directories: D:\openCV\opencv\build\include\opencv2;D:\openCV\opencv\build\include\opencv;D:\openCV\opencv\build\include;%(AdditionalLibraryDirectories)

      In Input> Additional Dependencies: opencv_calib3d248d.lib;opencv_contrib248d.lib;opencv_core248d.lib;opencv_features2d248d.lib;opencv_flann248d.lib;opencv_gpu248d.lib;opencv_highgui248d.lib;opencv_imgproc248d.lib;opencv_legacy248d.lib;opencv_ml248d.lib;opencv_nonfree248d.lib;opencv_objdetect248d.lib;opencv_ocl248d.lib;opencv_photo248d.lib;opencv_stitching248d.lib;opencv_superres248d.lib;opencv_ts248d.lib;opencv_video248d.lib;opencv_videostab248d.lib

      I've done same for the release, by removing "d", like "opencv_calib3d248.lib"

      Delete
    2. I'm using visual studio 2010.

      Delete
  10. Hi Adit,

    Is this your first project with visual studio 2010 and opencv? I think you need set the PC environment variable, to the opencv bin path.

    ReplyDelete
    Replies
    1. Yes, it is my first project with VS 2010 and opencv. Could you please show me how to set the PC environment variable, to the opencv bin path?

      Delete
  11. Hi there, I so knew this.. guys I am sorry to say, but you should first test with some small project, if your opencv is working or not before going to do some complex project.

    please check this link http://stackoverflow.com/questions/10901905/installing-opencv-2-4-3-in-visual-c-2010-express

    make some projects like "hello world" or something easier then try to do face detection later.

    Best wishes,

    ReplyDelete
  12. When I gave command 'opencv_traincascade -data facedet -vec vecfile.vec -bg negative.txt -numPos 2650 -numNeg 581 -numStages 20 -w 20 -h 20' it shows error like this.
    PARAMETERS:
    cascadeDirName: facedet
    vecFileName: vecfile.vec
    bgFileName: negative.txt
    numPos: 2650
    numNeg: 581
    numStages: 20
    precalcValBufSize[Mb] : 256
    precalcIdxBufSize[Mb] : 256
    stageType: BOOST
    featureType: HAAR
    sampleWidth: 20
    sampleHeight: 20
    boostType: GAB
    minHitRate: 0.995
    maxFalseAlarmRate: 0.5
    weightTrimRate: 0.95
    maxDepth: 1
    maxWeakCount: 100
    mode: BASIC

    ===== TRAINING 0-stage =====

    Parameters can not be written, because file facedet/params.xml can not be opened.

    what is this errror.I don't understand?

    ReplyDelete
  13. The coordinates of the samples are the bottom-left x,y?

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete