PageRenderTime 31ms CodeModel.GetById 31ms RepoModel.GetById 3ms app.codeStats 0ms

/facedetect.c

https://github.com/SteveClement/privateface
C | 280 lines | 152 code | 61 blank | 67 comment | 27 complexity | e7da176d7ccb8206a5c1261b5e324c98 MD5 | raw file
  1. // OpenCV Sample Application: facedetect.c
  2. // Include header files
  3. #include "cv.h"
  4. #include "highgui.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <math.h>
  10. #include <float.h>
  11. #include <limits.h>
  12. #include <time.h>
  13. #include <ctype.h>
  14. // Create memory for calculations
  15. static CvMemStorage* storage = 0;
  16. // Create a new Haar classifier
  17. static CvHaarClassifierCascade* cascade = 0;
  18. // Function prototype for detecting and drawing an object from an image
  19. void detect_and_draw( IplImage* image );
  20. // Create a string that contains the cascade name
  21. const char* cascade_name =
  22. "haarcascade_frontalface_alt.xml";
  23. /* "haarcascade_profileface.xml";*/
  24. // Main function, defines the entry point for the program.
  25. int main( int argc, char** argv )
  26. {
  27. // Structure for getting video from camera or avi
  28. CvCapture* capture = 0;
  29. // Images to capture the frame from video or camera or from file
  30. IplImage *frame, *frame_copy = 0;
  31. // Used for calculations
  32. int optlen = strlen("--cascade=");
  33. // Input file name for avi or image file.
  34. const char* input_name;
  35. // Check for the correct usage of the command line
  36. if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
  37. {
  38. cascade_name = argv[1] + optlen;
  39. input_name = argc > 2 ? argv[2] : 0;
  40. }
  41. else
  42. {
  43. fprintf( stderr,
  44. "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
  45. return -1;
  46. /*input_name = argc > 1 ? argv[1] : 0;*/
  47. }
  48. // Load the HaarClassifierCascade
  49. cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
  50. // Check whether the cascade has loaded successfully. Else report and error and quit
  51. if( !cascade )
  52. {
  53. fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
  54. return -1;
  55. }
  56. // Allocate the memory storage
  57. storage = cvCreateMemStorage(0);
  58. // Find whether to detect the object from file or from camera.
  59. if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
  60. capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
  61. else
  62. capture = cvCaptureFromAVI( input_name );
  63. // Create a new named window with title: result
  64. cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );
  65. cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );
  66. cvNamedWindow( "result", 3 );
  67. // Define Resolution
  68. // Find if the capture is loaded successfully or not.
  69. // If loaded succesfully, then:
  70. if( capture )
  71. {
  72. // Capture from the camera.
  73. for(;;)
  74. {
  75. // Capture the frame and load it in IplImage
  76. if( !cvGrabFrame( capture ))
  77. break;
  78. frame = cvRetrieveFrame( capture );
  79. // If the frame does not exist, quit the loop
  80. if( !frame )
  81. break;
  82. // Allocate framecopy as the same size of the frame
  83. if( !frame_copy )
  84. frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
  85. IPL_DEPTH_8U, frame->nChannels );
  86. // Check the origin of image. If top left, copy the image frame to frame_copy.
  87. if( frame->origin == IPL_ORIGIN_TL )
  88. cvCopy( frame, frame_copy, 0 );
  89. // Else flip and copy the image
  90. else
  91. cvFlip( frame, frame_copy, 0 );
  92. // Call the function to detect and draw the face
  93. detect_and_draw( frame_copy );
  94. // Wait for a while before proceeding to the next frame
  95. if( cvWaitKey( 10 ) >= 0 )
  96. break;
  97. }
  98. // Release the images, and capture memory
  99. cvReleaseImage( &frame_copy );
  100. cvReleaseCapture( &capture );
  101. }
  102. // If the capture is not loaded succesfully, then:
  103. else
  104. {
  105. // Assume the image to be lena.jpg, or the input_name specified
  106. const char* filename = input_name ? input_name : (char*)"lena.jpg";
  107. // Load the image from that filename
  108. IplImage* image = cvLoadImage( filename, 1 );
  109. // If Image is loaded succesfully, then:
  110. if( image )
  111. {
  112. // Detect and draw the face
  113. detect_and_draw( image );
  114. // Wait for user input
  115. cvWaitKey(0);
  116. // Release the image memory
  117. cvReleaseImage( &image );
  118. }
  119. else
  120. {
  121. /* assume it is a text file containing the
  122. list of the image filenames to be processed - one per line */
  123. FILE* f = fopen( filename, "rt" );
  124. if( f )
  125. {
  126. char buf[1000+1];
  127. // Get the line from the file
  128. while( fgets( buf, 1000, f ) )
  129. {
  130. // Remove the spaces if any, and clean up the name
  131. int len = (int)strlen(buf);
  132. while( len > 0 && isspace(buf[len-1]) )
  133. len--;
  134. buf[len] = '\0';
  135. // Load the image from the filename present in the buffer
  136. image = cvLoadImage( buf, 1 );
  137. // If the image was loaded succesfully, then:
  138. if( image )
  139. {
  140. // Detect and draw the face from the image
  141. detect_and_draw( image );
  142. // Wait for the user input, and release the memory
  143. cvWaitKey(0);
  144. cvReleaseImage( &image );
  145. }
  146. }
  147. // Close the file
  148. fclose(f);
  149. }
  150. }
  151. }
  152. // Destroy the window previously created with filename: "result"
  153. cvDestroyWindow("result");
  154. // return 0 to indicate successfull execution of the program
  155. return 0;
  156. }
  157. void cvOverlayImage(IplImage* src, IplImage* overlay, CvPoint location, CvScalar S, CvScalar D)
  158. {
  159. int x,y,i;
  160. for(x=0;x < overlay->width -10;x++)
  161. {
  162. if(x+location.x>=src->width) continue;
  163. for(y=0;y < overlay->height -10;y++)
  164. {
  165. if(y+location.y>=src->height) continue;
  166. CvScalar source = cvGet2D(src, y+location.y, x+location.x);
  167. CvScalar over = cvGet2D(overlay, y, x);
  168. CvScalar merged;
  169. for(i=0;i<4;i++)
  170. merged.val[i] = (S.val[i]*source.val[i]+D.val[i]*over.val[i]);
  171. cvSet2D(src, y+location.y, x+location.x, merged);
  172. }
  173. }
  174. }
  175. // Function to detect and draw any faces that is present in an image
  176. void detect_and_draw( IplImage* img )
  177. {
  178. IplImage *vee = 0;
  179. int scale = 1;
  180. // Create a new image based on the input image
  181. IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
  182. // Load Vee Mask
  183. vee = cvLoadImage( "v_mask.png", CV_LOAD_IMAGE_COLOR );
  184. // Create two points to represent the face locations
  185. CvPoint pt1, pt2;
  186. int i;
  187. // Clear the memory storage which was used before
  188. cvClearMemStorage( storage );
  189. // Find whether the cascade is loaded, to find the faces. If yes, then:
  190. if( cascade )
  191. {
  192. // There can be more than one face in an image. So create a growable sequence of faces.
  193. // Detect the objects and store them in the sequence
  194. CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
  195. 1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
  196. cvSize(40, 40) );
  197. // Loop the number of faces found.
  198. for( i = 0; i < (faces ? faces->total : 0); i++ )
  199. {
  200. // Create a new rectangle for drawing the face
  201. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  202. // Find the dimensions of the face,and scale it if necessary
  203. pt1.x = r->x*scale;
  204. pt2.x = (r->x+r->width)*scale;
  205. pt1.y = r->y*scale*1.1;
  206. pt2.y = (r->y+r->height*0.6)*scale;
  207. // Draw the rectangle in the input image
  208. cvRectangle( img, pt1, pt2, CV_RGB(0,0,0), -3, 8, 0 );
  209. // cvOverlayImage(img, vee, cvPoint(pt1.x, pt1.y), cvScalar(1,1,1,1), cvScalar(1,1,1,1));
  210. }
  211. }
  212. // Show the image in the window named "result"
  213. //cvSetWindowProperty("result", CV_WINDOW_FULLSCREEN, 1);
  214. // cvOverlayImage(img, vee, cvPoint(10, 10), cvScalar(0.5,0.5,0.5,0.5), cvScalar(0.5,0.5,0.5,0.5));
  215. cvShowImage( "result", img );
  216. //cvShowImage( "result", vee );
  217. // Release the temp image created.
  218. cvReleaseImage( &temp );
  219. }