traincascade.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "opencv2/core.hpp"
  2. #include "cascadeclassifier.h"
  3. using namespace std;
  4. using namespace cv;
  5. /*
  6. traincascade.cpp is the source file of the program used for cascade training.
  7. User has to provide training input in form of positive and negative training images,
  8. and other data related to training in form of command line argument.
  9. */
  10. int main( int argc, char* argv[] )
  11. {
  12. CvCascadeClassifier classifier;
  13. string cascadeDirName, vecName, bgName;
  14. int numPos = 2000;
  15. int numNeg = 1000;
  16. int numStages = 20;
  17. int numThreads = getNumThreads();
  18. int precalcValBufSize = 1024,
  19. precalcIdxBufSize = 1024;
  20. bool baseFormatSave = false;
  21. double acceptanceRatioBreakValue = -1.0;
  22. CvCascadeParams cascadeParams;
  23. CvCascadeBoostParams stageParams;
  24. Ptr<CvFeatureParams> featureParams[] = { makePtr<CvHaarFeatureParams>(),
  25. makePtr<CvLBPFeatureParams>(),
  26. makePtr<CvHOGFeatureParams>()
  27. };
  28. int fc = sizeof(featureParams)/sizeof(featureParams[0]);
  29. if( argc == 1 )
  30. {
  31. cout << "Usage: " << argv[0] << endl;
  32. cout << " -data <cascade_dir_name>" << endl;
  33. cout << " -vec <vec_file_name>" << endl;
  34. cout << " -bg <background_file_name>" << endl;
  35. cout << " [-numPos <number_of_positive_samples = " << numPos << ">]" << endl;
  36. cout << " [-numNeg <number_of_negative_samples = " << numNeg << ">]" << endl;
  37. cout << " [-numStages <number_of_stages = " << numStages << ">]" << endl;
  38. cout << " [-precalcValBufSize <precalculated_vals_buffer_size_in_Mb = " << precalcValBufSize << ">]" << endl;
  39. cout << " [-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb = " << precalcIdxBufSize << ">]" << endl;
  40. cout << " [-baseFormatSave]" << endl;
  41. cout << " [-numThreads <max_number_of_threads = " << numThreads << ">]" << endl;
  42. cout << " [-acceptanceRatioBreakValue <value> = " << acceptanceRatioBreakValue << ">]" << endl;
  43. cascadeParams.printDefaults();
  44. stageParams.printDefaults();
  45. for( int fi = 0; fi < fc; fi++ )
  46. featureParams[fi]->printDefaults();
  47. return 0;
  48. }
  49. for( int i = 1; i < argc; i++ )
  50. {
  51. bool set = false;
  52. if( !strcmp( argv[i], "-data" ) )
  53. {
  54. cascadeDirName = argv[++i];
  55. }
  56. else if( !strcmp( argv[i], "-vec" ) )
  57. {
  58. vecName = argv[++i];
  59. }
  60. else if( !strcmp( argv[i], "-bg" ) )
  61. {
  62. bgName = argv[++i];
  63. }
  64. else if( !strcmp( argv[i], "-numPos" ) )
  65. {
  66. numPos = atoi( argv[++i] );
  67. }
  68. else if( !strcmp( argv[i], "-numNeg" ) )
  69. {
  70. numNeg = atoi( argv[++i] );
  71. }
  72. else if( !strcmp( argv[i], "-numStages" ) )
  73. {
  74. numStages = atoi( argv[++i] );
  75. }
  76. else if( !strcmp( argv[i], "-precalcValBufSize" ) )
  77. {
  78. precalcValBufSize = atoi( argv[++i] );
  79. }
  80. else if( !strcmp( argv[i], "-precalcIdxBufSize" ) )
  81. {
  82. precalcIdxBufSize = atoi( argv[++i] );
  83. }
  84. else if( !strcmp( argv[i], "-baseFormatSave" ) )
  85. {
  86. baseFormatSave = true;
  87. }
  88. else if( !strcmp( argv[i], "-numThreads" ) )
  89. {
  90. numThreads = atoi(argv[++i]);
  91. }
  92. else if( !strcmp( argv[i], "-acceptanceRatioBreakValue" ) )
  93. {
  94. acceptanceRatioBreakValue = atof(argv[++i]);
  95. }
  96. else if ( cascadeParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
  97. else if ( stageParams.scanAttr( argv[i], argv[i+1] ) ) { i++; }
  98. else if ( !set )
  99. {
  100. for( int fi = 0; fi < fc; fi++ )
  101. {
  102. set = featureParams[fi]->scanAttr(argv[i], argv[i+1]);
  103. if ( !set )
  104. {
  105. i++;
  106. break;
  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. }