Home | History | Annotate | Download | only in traincascade
      1 #include "opencv2/core.hpp"
      2 #include "cascadeclassifier.h"
      3 
      4 using namespace std;
      5 using namespace cv;
      6 
      7 int main( int argc, char* argv[] )
      8 {
      9     CvCascadeClassifier classifier;
     10     string cascadeDirName, vecName, bgName;
     11     int numPos    = 2000;
     12     int numNeg    = 1000;
     13     int numStages = 20;
     14     int numThreads = getNumThreads();
     15     int precalcValBufSize = 1024,
     16         precalcIdxBufSize = 1024;
     17     bool baseFormatSave = false;
     18     double acceptanceRatioBreakValue = -1.0;
     19 
     20     CvCascadeParams cascadeParams;
     21     CvCascadeBoostParams stageParams;
     22     Ptr<CvFeatureParams> featureParams[] = { makePtr<CvHaarFeatureParams>(),
     23                                              makePtr<CvLBPFeatureParams>(),
     24                                              makePtr<CvHOGFeatureParams>()
     25                                            };
     26     int fc = sizeof(featureParams)/sizeof(featureParams[0]);
     27     if( argc == 1 )
     28     {
     29         cout << "Usage: " << argv[0] << endl;
     30         cout << "  -data <cascade_dir_name>" << endl;
     31         cout << "  -vec <vec_file_name>" << endl;
     32         cout << "  -bg <background_file_name>" << endl;
     33         cout << "  [-numPos <number_of_positive_samples = " << numPos << ">]" << endl;
     34         cout << "  [-numNeg <number_of_negative_samples = " << numNeg << ">]" << endl;
     35         cout << "  [-numStages <number_of_stages = " << numStages << ">]" << endl;
     36         cout << "  [-precalcValBufSize <precalculated_vals_buffer_size_in_Mb = " << precalcValBufSize << ">]" << endl;
     37         cout << "  [-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb = " << precalcIdxBufSize << ">]" << endl;
     38         cout << "  [-baseFormatSave]" << endl;
     39         cout << "  [-numThreads <max_number_of_threads = " << numThreads << ">]" << endl;
     40         cout << "  [-acceptanceRatioBreakValue <value> = " << acceptanceRatioBreakValue << ">]" << endl;
     41         cascadeParams.printDefaults();
     42         stageParams.printDefaults();
     43         for( int fi = 0; fi < fc; fi++ )
     44             featureParams[fi]->printDefaults();
     45         return 0;
     46     }
     47 
     48     for( int i = 1; i < argc; i++ )
     49     {
     50         bool set = false;
     51         if( !strcmp( argv[i], "-data" ) )
     52         {
     53             cascadeDirName = argv[++i];
     54         }
     55         else if( !strcmp( argv[i], "-vec" ) )
     56         {
     57             vecName = argv[++i];
     58         }
     59         else if( !strcmp( argv[i], "-bg" ) )
     60         {
     61             bgName = argv[++i];
     62         }
     63         else if( !strcmp( argv[i], "-numPos" ) )
     64         {
     65             numPos = atoi( argv[++i] );
     66         }
     67         else if( !strcmp( argv[i], "-numNeg" ) )
     68         {
     69             numNeg = atoi( argv[++i] );
     70         }
     71         else if( !strcmp( argv[i], "-numStages" ) )
     72         {
     73             numStages = atoi( argv[++i] );
     74         }
     75         else if( !strcmp( argv[i], "-precalcValBufSize" ) )
     76         {
     77             precalcValBufSize = atoi( argv[++i] );
     78         }
     79         else if( !strcmp( argv[i], "-precalcIdxBufSize" ) )
     80         {
     81             precalcIdxBufSize = atoi( argv[++i] );
     82         }
     83         else if( !strcmp( argv[i], "-baseFormatSave" ) )
     84         {
     85             baseFormatSave = true;
     86         }
     87         else if( !strcmp( argv[i], "-numThreads" ) )
     88         {
     89           numThreads = atoi(argv[++i]);
     90         }
     91         else if( !strcmp( argv[i], "-acceptanceRatioBreakValue" ) )
     92         {
     93           acceptanceRatioBreakValue = atof(argv[++i]);
     94         }
     95         else if ( cascadeParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
     96         else if ( stageParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
     97         else if ( !set )
     98         {
     99             for( int fi = 0; fi < fc; fi++ )
    100             {
    101                 set = featureParams[fi]->scanAttr(argv[i], argv[i+1]);
    102                 if ( !set )
    103                 {
    104                     i++;
    105                     break;
    106                 }
    107             }
    108         }
    109     }
    110 
    111     setNumThreads( numThreads );
    112     classifier.train( cascadeDirName,
    113                       vecName,
    114                       bgName,
    115                       numPos, numNeg,
    116                       precalcValBufSize, precalcIdxBufSize,
    117                       numStages,
    118                       cascadeParams,
    119                       *featureParams[cascadeParams.featureType],
    120                       stageParams,
    121                       baseFormatSave,
    122                       acceptanceRatioBreakValue );
    123     return 0;
    124 }
    125