PageRenderTime 371ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/HaarTraining/src/facedetect.c

https://gitlab.com/zharfi/tutorial-haartraining
C | 215 lines | 187 code | 24 blank | 4 comment | 33 complexity | 7b329abd707001249d1fd39463dd5827 MD5 | raw file
  1. #include "cv.h"
  2. #include "highgui.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <math.h>
  8. #include <float.h>
  9. #include <limits.h>
  10. #include <time.h>
  11. #include <ctype.h>
  12. #ifdef _EiC
  13. #define WIN32
  14. #endif
  15. static CvMemStorage* storage = 0;
  16. static CvHaarClassifierCascade* cascade = 0;
  17. void detect_and_draw( IplImage* img, double scale_factor, int min_neighbors, int flags, CvSize min_size );
  18. const char* cascade_name =
  19. "haarcascade_frontalface_alt.xml";
  20. /* "haarcascade_profileface.xml";*/
  21. int main( int argc, char** argv )
  22. {
  23. CvCapture* capture = 0;
  24. IplImage *frame, *frame_copy = 0;
  25. int optlen = strlen("--cascade=");
  26. const char* input_name = "0";
  27. int i;
  28. double scale_factor = 1.1;
  29. int min_neighbors = 1;
  30. int flags = 0;/*CV_HAAR_DO_CANNY_PRUNING*/
  31. CvSize min_size = cvSize(0,0);
  32. if( argc == 1 )
  33. {
  34. fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
  35. fprintf( stderr,
  36. "Usage: facedetect "
  37. "--cascade=\"<cascade_xml_path>\" or -c <cascade_xml_path>\n"
  38. " [ -sf < scale_factor = %f > ]\n"
  39. " [ -mn < min_neighbors = %d > ]\n"
  40. " [ -fl < flags = %d > ]\n"
  41. " [ -ms < min_size = %d %d > ]\n"
  42. " [ filename | camera_index = %s ]\n",
  43. scale_factor, min_neighbors, flags, min_size.width, min_size.height, input_name );
  44. fprintf( stderr, "See also: cvHaarDetectObjects() about option parameters.\n" );
  45. return -1;
  46. }
  47. for( i = 1; i < argc; i++ )
  48. {
  49. if( !strncmp( argv[i], "--cascade=", optlen ) )
  50. {
  51. cascade_name = argv[++i] + optlen;
  52. }
  53. else if( !strcmp( argv[i], "-c" ) )
  54. {
  55. cascade_name = argv[++i];
  56. }
  57. else if( !strcmp( argv[i], "-sf" ) )
  58. {
  59. scale_factor = (float) atof( argv[++i] );
  60. }
  61. else if( !strcmp( argv[i], "-mn" ) )
  62. {
  63. min_neighbors = atoi( argv[++i] );
  64. }
  65. else if( !strcmp( argv[i], "-fl" ) )
  66. {
  67. flags = CV_HAAR_DO_CANNY_PRUNING;
  68. }
  69. else if( !strcmp( argv[i], "-ms" ) )
  70. {
  71. min_size = cvSize( atoi( argv[++i] ), atoi( argv[++i] ) );
  72. }
  73. else
  74. {
  75. input_name = argv[i];
  76. }
  77. }
  78. cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
  79. storage = cvCreateMemStorage(0);
  80. if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
  81. capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
  82. else
  83. capture = cvCaptureFromAVI( input_name );
  84. cvNamedWindow( "result", 1 );
  85. if( capture )
  86. {
  87. for(;;)
  88. {
  89. if( !cvGrabFrame( capture ))
  90. break;
  91. frame = cvRetrieveFrame( capture );
  92. if( !frame )
  93. break;
  94. if( !frame_copy )
  95. frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
  96. IPL_DEPTH_8U, frame->nChannels );
  97. if( frame->origin == IPL_ORIGIN_TL )
  98. cvCopy( frame, frame_copy, 0 );
  99. else
  100. cvFlip( frame, frame_copy, 0 );
  101. detect_and_draw( frame_copy, scale_factor, min_neighbors, flags, min_size );
  102. if( cvWaitKey( 10 ) >= 0 )
  103. break;
  104. }
  105. cvReleaseImage( &frame_copy );
  106. cvReleaseCapture( &capture );
  107. }
  108. else
  109. {
  110. const char* filename = input_name ? input_name : (char*)"lena.jpg";
  111. IplImage* image = cvLoadImage( filename, 1 );
  112. if( image )
  113. {
  114. detect_and_draw( image, scale_factor, min_neighbors, flags, min_size );
  115. cvWaitKey(0);
  116. cvReleaseImage( &image );
  117. }
  118. else
  119. {
  120. /* assume it is a text file containing the
  121. list of the image filenames to be processed - one per line */
  122. FILE* f = fopen( filename, "rt" );
  123. if( f )
  124. {
  125. char buf[1000+1];
  126. while( fgets( buf, 1000, f ) )
  127. {
  128. int len = (int)strlen(buf);
  129. while( len > 0 && isspace(buf[len-1]) )
  130. len--;
  131. buf[len] = '\0';
  132. image = cvLoadImage( buf, 1 );
  133. if( image )
  134. {
  135. detect_and_draw( image, scale_factor, min_neighbors, flags, min_size );
  136. cvWaitKey(0);
  137. cvReleaseImage( &image );
  138. }
  139. }
  140. fclose(f);
  141. }
  142. }
  143. }
  144. cvDestroyWindow("result");
  145. return 0;
  146. }
  147. void detect_and_draw( IplImage* img, double scale_factor, int min_neighbors, int flags, CvSize min_size )
  148. {
  149. static CvScalar colors[] =
  150. {
  151. {{0,0,255}},
  152. {{0,128,255}},
  153. {{0,255,255}},
  154. {{0,255,0}},
  155. {{255,128,0}},
  156. {{255,255,0}},
  157. {{255,0,0}},
  158. {{255,0,255}}
  159. };
  160. double scale = 1.3;
  161. IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
  162. IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
  163. cvRound (img->height/scale)), 8, 1 );
  164. int i;
  165. cvCvtColor( img, gray, CV_BGR2GRAY );
  166. cvResize( gray, small_img, CV_INTER_LINEAR );
  167. cvEqualizeHist( small_img, small_img );
  168. cvClearMemStorage( storage );
  169. if( cascade )
  170. {
  171. double t = (double)cvGetTickCount();
  172. CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
  173. scale_factor, min_neighbors, flags, min_size );
  174. //1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(30, 30) );
  175. t = (double)cvGetTickCount() - t;
  176. printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
  177. for( i = 0; i < (faces ? faces->total : 0); i++ )
  178. {
  179. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  180. CvPoint center;
  181. int radius;
  182. center.x = cvRound((r->x + r->width*0.5)*scale);
  183. center.y = cvRound((r->y + r->height*0.5)*scale);
  184. radius = cvRound((r->width + r->height)*0.25*scale);
  185. cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
  186. }
  187. }
  188. cvShowImage( "result", img );
  189. cvReleaseImage( &gray );
  190. cvReleaseImage( &small_img );
  191. }