PageRenderTime 65ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/source/3rd_party/opencv/sources/apps/haartraining/performance.cpp

https://gitlab.com/Ruggero/SparkEngine_Desktop
C++ | 378 lines | 283 code | 46 blank | 49 comment | 52 complexity | 0f29d2009b72aa401dfabf83b1f3547f MD5 | raw file
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. /*
  42. * performance.cpp
  43. *
  44. * Measure performance of classifier
  45. */
  46. #include "opencv2/core/core.hpp"
  47. #include "opencv2/core/internal.hpp"
  48. #include "cv.h"
  49. #include "highgui.h"
  50. #include <cstdio>
  51. #include <cmath>
  52. #include <ctime>
  53. #ifdef _WIN32
  54. /* use clock() function insted of time() */
  55. #define time( arg ) (((double) clock()) / CLOCKS_PER_SEC)
  56. #endif /* _WIN32 */
  57. #ifndef PATH_MAX
  58. #define PATH_MAX 512
  59. #endif /* PATH_MAX */
  60. typedef struct HidCascade
  61. {
  62. int size;
  63. int count;
  64. } HidCascade;
  65. typedef struct ObjectPos
  66. {
  67. float x;
  68. float y;
  69. float width;
  70. int found; /* for reference */
  71. int neghbors;
  72. } ObjectPos;
  73. int main( int argc, char* argv[] )
  74. {
  75. int i, j;
  76. char* classifierdir = NULL;
  77. //char* samplesdir = NULL;
  78. int saveDetected = 1;
  79. double scale_factor = 1.2;
  80. float maxSizeDiff = 1.5F;
  81. float maxPosDiff = 0.3F;
  82. /* number of stages. if <=0 all stages are used */
  83. int nos = -1, nos0;
  84. int width = 24;
  85. int height = 24;
  86. int rocsize;
  87. FILE* info;
  88. char* infoname;
  89. char fullname[PATH_MAX];
  90. char detfilename[PATH_MAX];
  91. char* filename;
  92. char detname[] = "det-";
  93. CvHaarClassifierCascade* cascade;
  94. CvMemStorage* storage;
  95. CvSeq* objects;
  96. double totaltime;
  97. infoname = (char*)"";
  98. rocsize = 40;
  99. if( argc == 1 )
  100. {
  101. printf( "Usage: %s\n -data <classifier_directory_name>\n"
  102. " -info <collection_file_name>\n"
  103. " [-maxSizeDiff <max_size_difference = %f>]\n"
  104. " [-maxPosDiff <max_position_difference = %f>]\n"
  105. " [-sf <scale_factor = %f>]\n"
  106. " [-ni]\n"
  107. " [-nos <number_of_stages = %d>]\n"
  108. " [-rs <roc_size = %d>]\n"
  109. " [-w <sample_width = %d>]\n"
  110. " [-h <sample_height = %d>]\n",
  111. argv[0], maxSizeDiff, maxPosDiff, scale_factor, nos, rocsize,
  112. width, height );
  113. return 0;
  114. }
  115. for( i = 1; i < argc; i++ )
  116. {
  117. if( !strcmp( argv[i], "-data" ) )
  118. {
  119. classifierdir = argv[++i];
  120. }
  121. else if( !strcmp( argv[i], "-info" ) )
  122. {
  123. infoname = argv[++i];
  124. }
  125. else if( !strcmp( argv[i], "-maxSizeDiff" ) )
  126. {
  127. maxSizeDiff = (float) atof( argv[++i] );
  128. }
  129. else if( !strcmp( argv[i], "-maxPosDiff" ) )
  130. {
  131. maxPosDiff = (float) atof( argv[++i] );
  132. }
  133. else if( !strcmp( argv[i], "-sf" ) )
  134. {
  135. scale_factor = atof( argv[++i] );
  136. }
  137. else if( !strcmp( argv[i], "-ni" ) )
  138. {
  139. saveDetected = 0;
  140. }
  141. else if( !strcmp( argv[i], "-nos" ) )
  142. {
  143. nos = atoi( argv[++i] );
  144. }
  145. else if( !strcmp( argv[i], "-rs" ) )
  146. {
  147. rocsize = atoi( argv[++i] );
  148. }
  149. else if( !strcmp( argv[i], "-w" ) )
  150. {
  151. width = atoi( argv[++i] );
  152. }
  153. else if( !strcmp( argv[i], "-h" ) )
  154. {
  155. height = atoi( argv[++i] );
  156. }
  157. }
  158. cascade = cvLoadHaarClassifierCascade( classifierdir, cvSize( width, height ) );
  159. if( cascade == NULL )
  160. {
  161. printf( "Unable to load classifier from %s\n", classifierdir );
  162. return 1;
  163. }
  164. int* numclassifiers = new int[cascade->count];
  165. numclassifiers[0] = cascade->stage_classifier[0].count;
  166. for( i = 1; i < cascade->count; i++ )
  167. {
  168. numclassifiers[i] = numclassifiers[i-1] + cascade->stage_classifier[i].count;
  169. }
  170. storage = cvCreateMemStorage();
  171. nos0 = cascade->count;
  172. if( nos <= 0 )
  173. nos = nos0;
  174. strcpy( fullname, infoname );
  175. filename = strrchr( fullname, '\\' );
  176. if( filename == NULL )
  177. {
  178. filename = strrchr( fullname, '/' );
  179. }
  180. if( filename == NULL )
  181. {
  182. filename = fullname;
  183. }
  184. else
  185. {
  186. filename++;
  187. }
  188. info = fopen( infoname, "r" );
  189. totaltime = 0.0;
  190. if( info != NULL )
  191. {
  192. int x, y;
  193. IplImage* img;
  194. int hits, missed, falseAlarms;
  195. int totalHits, totalMissed, totalFalseAlarms;
  196. int found;
  197. float distance;
  198. int refcount;
  199. ObjectPos* ref;
  200. int detcount;
  201. ObjectPos* det;
  202. int error=0;
  203. int* pos;
  204. int* neg;
  205. pos = (int*) cvAlloc( rocsize * sizeof( *pos ) );
  206. neg = (int*) cvAlloc( rocsize * sizeof( *neg ) );
  207. for( i = 0; i < rocsize; i++ ) { pos[i] = neg[i] = 0; }
  208. printf( "+================================+======+======+======+\n" );
  209. printf( "| File Name | Hits |Missed| False|\n" );
  210. printf( "+================================+======+======+======+\n" );
  211. totalHits = totalMissed = totalFalseAlarms = 0;
  212. while( !feof( info ) )
  213. {
  214. if( fscanf( info, "%s %d", filename, &refcount ) != 2 || refcount <= 0 ) break;
  215. img = cvLoadImage( fullname );
  216. if( !img ) continue;
  217. ref = (ObjectPos*) cvAlloc( refcount * sizeof( *ref ) );
  218. for( i = 0; i < refcount; i++ )
  219. {
  220. int w, h;
  221. error = (fscanf( info, "%d %d %d %d", &x, &y, &w, &h ) != 4);
  222. if( error ) break;
  223. ref[i].x = 0.5F * w + x;
  224. ref[i].y = 0.5F * h + y;
  225. ref[i].width = sqrtf( 0.5F * (w * w + h * h) );
  226. ref[i].found = 0;
  227. ref[i].neghbors = 0;
  228. }
  229. if( !error )
  230. {
  231. cvClearMemStorage( storage );
  232. cascade->count = nos;
  233. totaltime -= time( 0 );
  234. objects = cvHaarDetectObjects( img, cascade, storage, scale_factor, 1 );
  235. totaltime += time( 0 );
  236. cascade->count = nos0;
  237. detcount = ( objects ? objects->total : 0);
  238. det = (detcount > 0) ?
  239. ( (ObjectPos*)cvAlloc( detcount * sizeof( *det )) ) : NULL;
  240. hits = missed = falseAlarms = 0;
  241. for( i = 0; i < detcount; i++ )
  242. {
  243. CvAvgComp r = *((CvAvgComp*) cvGetSeqElem( objects, i ));
  244. det[i].x = 0.5F * r.rect.width + r.rect.x;
  245. det[i].y = 0.5F * r.rect.height + r.rect.y;
  246. det[i].width = sqrtf( 0.5F * (r.rect.width * r.rect.width +
  247. r.rect.height * r.rect.height) );
  248. det[i].neghbors = r.neighbors;
  249. if( saveDetected )
  250. {
  251. cvRectangle( img, cvPoint( r.rect.x, r.rect.y ),
  252. cvPoint( r.rect.x + r.rect.width, r.rect.y + r.rect.height ),
  253. CV_RGB( 255, 0, 0 ), 3 );
  254. }
  255. found = 0;
  256. for( j = 0; j < refcount; j++ )
  257. {
  258. distance = sqrtf( (det[i].x - ref[j].x) * (det[i].x - ref[j].x) +
  259. (det[i].y - ref[j].y) * (det[i].y - ref[j].y) );
  260. if( (distance < ref[j].width * maxPosDiff) &&
  261. (det[i].width > ref[j].width / maxSizeDiff) &&
  262. (det[i].width < ref[j].width * maxSizeDiff) )
  263. {
  264. ref[j].found = 1;
  265. ref[j].neghbors = MAX( ref[j].neghbors, det[i].neghbors );
  266. found = 1;
  267. }
  268. }
  269. if( !found )
  270. {
  271. falseAlarms++;
  272. neg[MIN(det[i].neghbors, rocsize - 1)]++;
  273. }
  274. }
  275. for( j = 0; j < refcount; j++ )
  276. {
  277. if( ref[j].found )
  278. {
  279. hits++;
  280. pos[MIN(ref[j].neghbors, rocsize - 1)]++;
  281. }
  282. else
  283. {
  284. missed++;
  285. }
  286. }
  287. totalHits += hits;
  288. totalMissed += missed;
  289. totalFalseAlarms += falseAlarms;
  290. printf( "|%32.32s|%6d|%6d|%6d|\n", filename, hits, missed, falseAlarms );
  291. printf( "+--------------------------------+------+------+------+\n" );
  292. fflush( stdout );
  293. if( saveDetected )
  294. {
  295. strcpy( detfilename, detname );
  296. strcat( detfilename, filename );
  297. strcpy( filename, detfilename );
  298. cvvSaveImage( fullname, img );
  299. }
  300. if( det ) { cvFree( &det ); det = NULL; }
  301. } /* if( !error ) */
  302. cvReleaseImage( &img );
  303. cvFree( &ref );
  304. }
  305. fclose( info );
  306. printf( "|%32.32s|%6d|%6d|%6d|\n", "Total",
  307. totalHits, totalMissed, totalFalseAlarms );
  308. printf( "+================================+======+======+======+\n" );
  309. printf( "Number of stages: %d\n", nos );
  310. printf( "Number of weak classifiers: %d\n", numclassifiers[nos - 1] );
  311. printf( "Total time: %f\n", totaltime );
  312. /* print ROC to stdout */
  313. for( i = rocsize - 1; i > 0; i-- )
  314. {
  315. pos[i-1] += pos[i];
  316. neg[i-1] += neg[i];
  317. }
  318. fprintf( stderr, "%d\n", nos );
  319. for( i = 0; i < rocsize; i++ )
  320. {
  321. fprintf( stderr, "\t%d\t%d\t%f\t%f\n", pos[i], neg[i],
  322. ((float)pos[i]) / (totalHits + totalMissed),
  323. ((float)neg[i]) / (totalHits + totalMissed) );
  324. }
  325. cvFree( &pos );
  326. cvFree( &neg );
  327. }
  328. delete[] numclassifiers;
  329. cvReleaseHaarClassifierCascade( &cascade );
  330. cvReleaseMemStorage( &storage );
  331. return 0;
  332. }