Guest User

Untitled

a guest
Jun 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #include "pro_includes.h"
  2.  
  3.  
  4.  
  5.  
  6.  
  7. static IplImage* detect_and_draw(IplImage*);
  8.  
  9. static void frame_collector(){
  10. }
  11.  
  12. static void capture_video(int select_flag,char* IN_FILE)
  13. {
  14. CvCapture* capture;
  15. double fps, cur_pos;
  16. double TOTAL_FRAMES;
  17. double frame_pos_inc = 0;
  18. IplImage* faces_detected;
  19. IplImage **FRAME_BUFF = NULL;
  20. char FILE_PREFIX[] = "../data/faces_db";
  21. int next_frame = 0;
  22.  
  23.  
  24. cvNamedWindow("capturing", 0 );
  25. cvResizeWindow("capturing",600,600);
  26. if(select_flag == 0){
  27. capture = cvCaptureFromCAM( CV_CAP_ANY );
  28. }
  29. else{
  30. capture = cvCaptureFromFile( IN_FILE );
  31. }
  32. if(!capture){
  33. printf("Error occured");
  34. getchar();
  35. exit(0);
  36. }
  37. fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
  38.  
  39. TOTAL_FRAMES = cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
  40. FRAME_BUFF = malloc( (TOTAL_FRAMES * sizeof(IplImage) ) );
  41.  
  42. if( FRAME_BUFF == NULL ){
  43. printf("out of memory :ERROR ");
  44. exit(1);
  45. }
  46.  
  47.  
  48.  
  49. //frame_pos_inc = cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
  50. while((cvGetCaptureProperty(capture,CV_CAP_PROP_POS_AVI_RATIO)) < 1){
  51. if( !cvSetCaptureProperty( capture, CV_CAP_PROP_POS_FRAMES, frame_pos_inc += 4) )
  52. break;
  53.  
  54.  
  55. IplImage* frame = cvQueryFrame(capture);
  56. if( detect_and_draw(frame) != NULL){
  57.  
  58. faces_detected = detect_and_draw(frame);
  59.  
  60. FRAME_BUFF[next_frame] = faces_detected;
  61. next_frame++;
  62. //if(!frame) break;
  63. //cvShowImage("recording",frame);
  64. //if(cvWaitKey(4)== 27) break;
  65.  
  66. //printf("\nface on frame :");
  67. }
  68. }
  69.  
  70. while(next_frame-- >=0 ){
  71.  
  72. cvShowImage("capturing",FRAME_BUFF[next_frame]);
  73. if(cvWaitKey(4)== 27) break;
  74.  
  75.  
  76. }
  77. free(FRAME_BUFF);
  78. cvReleaseCapture(&capture);
  79. cvDestroyWindow("capturing");
  80. }
  81.  
  82. static IplImage* detect_and_draw( IplImage* img )
  83. {
  84.  
  85.  
  86. const char* cascade_name ="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml";
  87.  
  88. // Create memory for calculations
  89. static CvMemStorage* storage = 0;
  90.  
  91. // Create a new Haar classifier
  92. static CvHaarClassifierCascade* cascade = 0;
  93.  
  94. int scale = 1;
  95.  
  96. // Create a new image based on the input image
  97. IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
  98.  
  99. // Create two points to represent the face locations
  100. CvPoint pt1, pt2;
  101. int i;
  102.  
  103. // Load the HaarClassifierCascade
  104. cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
  105.  
  106. // Check whether the cascade has loaded successfully. Else report and error and quit
  107. if( !cascade )
  108. {
  109. fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
  110. return;
  111. }
  112.  
  113. // Allocate the memory storage
  114. storage = cvCreateMemStorage(0);
  115.  
  116. // Create a new named window with title: result
  117. // cvNamedWindow( "result", 1 );
  118.  
  119. // Clear the memory storage which was used before
  120. cvClearMemStorage( storage );
  121.  
  122. // Find whether the cascade is loaded, to find the faces. If yes, then:
  123. if( cascade )
  124. {
  125.  
  126. // There can be more than one face in an image. So create a growable sequence of faces.
  127. // Detect the objects and store them in the sequence
  128. CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,1.1, 2, CV_HAAR_DO_CANNY_PRUNING,cvSize(40, 40),cvSize(40,40) );
  129.  
  130. // Loop the number of faces found.
  131. for( i = 0; i < (faces ? faces->total : 0); i++ )
  132. {
  133. // Create a new rectangle for drawing the face
  134. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  135.  
  136. // Find the dimensions of the face,and scale it if necessary
  137. pt1.x = r->x*scale;
  138. pt2.x = (r->x+r->width)*scale;
  139. pt1.y = r->y*scale;
  140. pt2.y = (r->y+r->height)*scale;
  141.  
  142. // Draw the rectangle in the input image
  143. cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
  144. }
  145.  
  146. //cvWaitKey(1);
  147. // Show the image in the window named "result"
  148. // while(1){
  149. // cvShowImage( "result", img );
  150. // char c = cvWaitKey(500);
  151. // if( c == 27 ) break;
  152. // }*/
  153. // Release the temp image create
  154.  
  155.  
  156.  
  157. cvReleaseImage( &temp );
  158.  
  159. if ( faces->total > 0 ){
  160. return img;
  161. }
  162.  
  163.  
  164. }
  165.  
  166.  
  167. return NULL;
  168. }
  169.  
  170.  
  171.  
  172.  
  173. int main(int argc,char** argv)
  174. {
  175.  
  176. capture_video(1,argv[1]);
  177.  
  178. return 0;
  179. }
Add Comment
Please, Sign In to add comment