2013-03-13 20:22:44 +08:00
|
|
|
#include "opencv2/core.hpp"
|
2010-05-12 01:44:00 +08:00
|
|
|
#include "cascadeclassifier.h"
|
|
|
|
|
|
|
|
using namespace std;
|
2013-11-16 23:56:08 +08:00
|
|
|
using namespace cv;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-10-21 12:49:38 +08:00
|
|
|
/*
|
|
|
|
traincascade.cpp is the source file of the program used for cascade training.
|
2017-10-22 15:07:19 +08:00
|
|
|
User has to provide training input in form of positive and negative training images,
|
2017-10-21 12:49:38 +08:00
|
|
|
and other data related to training in form of command line argument.
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
int main( int argc, char* argv[] )
|
|
|
|
{
|
|
|
|
CvCascadeClassifier classifier;
|
2013-02-25 00:14:01 +08:00
|
|
|
string cascadeDirName, vecName, bgName;
|
2010-05-12 01:44:00 +08:00
|
|
|
int numPos = 2000;
|
|
|
|
int numNeg = 1000;
|
|
|
|
int numStages = 20;
|
2014-07-24 04:47:51 +08:00
|
|
|
int numThreads = getNumThreads();
|
2015-03-17 16:00:41 +08:00
|
|
|
int precalcValBufSize = 1024,
|
|
|
|
precalcIdxBufSize = 1024;
|
2010-05-12 01:44:00 +08:00
|
|
|
bool baseFormatSave = false;
|
2015-03-06 18:52:26 +08:00
|
|
|
double acceptanceRatioBreakValue = -1.0;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvCascadeParams cascadeParams;
|
|
|
|
CvCascadeBoostParams stageParams;
|
2013-08-13 22:33:06 +08:00
|
|
|
Ptr<CvFeatureParams> featureParams[] = { makePtr<CvHaarFeatureParams>(),
|
|
|
|
makePtr<CvLBPFeatureParams>(),
|
|
|
|
makePtr<CvHOGFeatureParams>()
|
2011-10-07 00:46:03 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
int fc = sizeof(featureParams)/sizeof(featureParams[0]);
|
|
|
|
if( argc == 1 )
|
|
|
|
{
|
|
|
|
cout << "Usage: " << argv[0] << endl;
|
|
|
|
cout << " -data <cascade_dir_name>" << endl;
|
|
|
|
cout << " -vec <vec_file_name>" << endl;
|
|
|
|
cout << " -bg <background_file_name>" << endl;
|
|
|
|
cout << " [-numPos <number_of_positive_samples = " << numPos << ">]" << endl;
|
|
|
|
cout << " [-numNeg <number_of_negative_samples = " << numNeg << ">]" << endl;
|
|
|
|
cout << " [-numStages <number_of_stages = " << numStages << ">]" << endl;
|
|
|
|
cout << " [-precalcValBufSize <precalculated_vals_buffer_size_in_Mb = " << precalcValBufSize << ">]" << endl;
|
|
|
|
cout << " [-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb = " << precalcIdxBufSize << ">]" << endl;
|
|
|
|
cout << " [-baseFormatSave]" << endl;
|
2014-07-24 04:47:51 +08:00
|
|
|
cout << " [-numThreads <max_number_of_threads = " << numThreads << ">]" << endl;
|
2015-03-06 18:52:26 +08:00
|
|
|
cout << " [-acceptanceRatioBreakValue <value> = " << acceptanceRatioBreakValue << ">]" << endl;
|
2010-05-12 01:44:00 +08:00
|
|
|
cascadeParams.printDefaults();
|
|
|
|
stageParams.printDefaults();
|
|
|
|
for( int fi = 0; fi < fc; fi++ )
|
|
|
|
featureParams[fi]->printDefaults();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
bool set = false;
|
|
|
|
if( !strcmp( argv[i], "-data" ) )
|
|
|
|
{
|
|
|
|
cascadeDirName = argv[++i];
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-vec" ) )
|
|
|
|
{
|
|
|
|
vecName = argv[++i];
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-bg" ) )
|
|
|
|
{
|
|
|
|
bgName = argv[++i];
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-numPos" ) )
|
|
|
|
{
|
|
|
|
numPos = atoi( argv[++i] );
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-numNeg" ) )
|
|
|
|
{
|
|
|
|
numNeg = atoi( argv[++i] );
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-numStages" ) )
|
|
|
|
{
|
|
|
|
numStages = atoi( argv[++i] );
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-precalcValBufSize" ) )
|
|
|
|
{
|
|
|
|
precalcValBufSize = atoi( argv[++i] );
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-precalcIdxBufSize" ) )
|
|
|
|
{
|
|
|
|
precalcIdxBufSize = atoi( argv[++i] );
|
|
|
|
}
|
|
|
|
else if( !strcmp( argv[i], "-baseFormatSave" ) )
|
|
|
|
{
|
|
|
|
baseFormatSave = true;
|
|
|
|
}
|
2014-07-24 04:47:51 +08:00
|
|
|
else if( !strcmp( argv[i], "-numThreads" ) )
|
|
|
|
{
|
|
|
|
numThreads = atoi(argv[++i]);
|
|
|
|
}
|
2015-03-06 18:52:26 +08:00
|
|
|
else if( !strcmp( argv[i], "-acceptanceRatioBreakValue" ) )
|
|
|
|
{
|
|
|
|
acceptanceRatioBreakValue = atof(argv[++i]);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
else if ( cascadeParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
|
|
|
|
else if ( stageParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
|
|
|
|
else if ( !set )
|
|
|
|
{
|
|
|
|
for( int fi = 0; fi < fc; fi++ )
|
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
set = featureParams[fi]->scanAttr(argv[i], argv[i+1]);
|
2010-05-12 01:44:00 +08:00
|
|
|
if ( !set )
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2014-07-24 04:47:51 +08:00
|
|
|
setNumThreads( numThreads );
|
2010-05-12 01:44:00 +08:00
|
|
|
classifier.train( cascadeDirName,
|
|
|
|
vecName,
|
2012-06-08 01:21:29 +08:00
|
|
|
bgName,
|
|
|
|
numPos, numNeg,
|
2010-05-12 01:44:00 +08:00
|
|
|
precalcValBufSize, precalcIdxBufSize,
|
|
|
|
numStages,
|
|
|
|
cascadeParams,
|
|
|
|
*featureParams[cascadeParams.featureType],
|
|
|
|
stageParams,
|
2015-03-06 18:52:26 +08:00
|
|
|
baseFormatSave,
|
|
|
|
acceptanceRatioBreakValue );
|
2010-05-12 01:44:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|