Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pro_includes.h"
- static IplImage* detect_and_draw(IplImage*);
- static void frame_collector(){
- }
- static void capture_video(int select_flag,char* IN_FILE)
- {
- CvCapture* capture;
- double fps, cur_pos;
- double TOTAL_FRAMES;
- double frame_pos_inc = 0;
- IplImage* faces_detected;
- IplImage **FRAME_BUFF = NULL;
- char FILE_PREFIX[] = "../data/faces_db";
- int next_frame = 0;
- cvNamedWindow("capturing", 0 );
- cvResizeWindow("capturing",600,600);
- if(select_flag == 0){
- capture = cvCaptureFromCAM( CV_CAP_ANY );
- }
- else{
- capture = cvCaptureFromFile( IN_FILE );
- }
- if(!capture){
- printf("Error occured");
- getchar();
- exit(0);
- }
- fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
- TOTAL_FRAMES = cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
- FRAME_BUFF = malloc( (TOTAL_FRAMES * sizeof(IplImage) ) );
- if( FRAME_BUFF == NULL ){
- printf("out of memory :ERROR ");
- exit(1);
- }
- //frame_pos_inc = cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
- while((cvGetCaptureProperty(capture,CV_CAP_PROP_POS_AVI_RATIO)) < 1){
- if( !cvSetCaptureProperty( capture, CV_CAP_PROP_POS_FRAMES, frame_pos_inc += 4) )
- break;
- IplImage* frame = cvQueryFrame(capture);
- if( detect_and_draw(frame) != NULL){
- faces_detected = detect_and_draw(frame);
- FRAME_BUFF[next_frame] = faces_detected;
- next_frame++;
- //if(!frame) break;
- //cvShowImage("recording",frame);
- //if(cvWaitKey(4)== 27) break;
- //printf("\nface on frame :");
- }
- }
- while(next_frame-- >=0 ){
- cvShowImage("capturing",FRAME_BUFF[next_frame]);
- if(cvWaitKey(4)== 27) break;
- }
- free(FRAME_BUFF);
- cvReleaseCapture(&capture);
- cvDestroyWindow("capturing");
- }
- static IplImage* detect_and_draw( IplImage* img )
- {
- const char* cascade_name ="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml";
- // Create memory for calculations
- static CvMemStorage* storage = 0;
- // Create a new Haar classifier
- static CvHaarClassifierCascade* cascade = 0;
- int scale = 1;
- // Create a new image based on the input image
- IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
- // Create two points to represent the face locations
- CvPoint pt1, pt2;
- int i;
- // Load the HaarClassifierCascade
- cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
- // Check whether the cascade has loaded successfully. Else report and error and quit
- if( !cascade )
- {
- fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
- return;
- }
- // Allocate the memory storage
- storage = cvCreateMemStorage(0);
- // Create a new named window with title: result
- // cvNamedWindow( "result", 1 );
- // Clear the memory storage which was used before
- cvClearMemStorage( storage );
- // Find whether the cascade is loaded, to find the faces. If yes, then:
- if( cascade )
- {
- // There can be more than one face in an image. So create a growable sequence of faces.
- // Detect the objects and store them in the sequence
- CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,1.1, 2, CV_HAAR_DO_CANNY_PRUNING,cvSize(40, 40),cvSize(40,40) );
- // Loop the number of faces found.
- for( i = 0; i < (faces ? faces->total : 0); i++ )
- {
- // Create a new rectangle for drawing the face
- CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
- // Find the dimensions of the face,and scale it if necessary
- pt1.x = r->x*scale;
- pt2.x = (r->x+r->width)*scale;
- pt1.y = r->y*scale;
- pt2.y = (r->y+r->height)*scale;
- // Draw the rectangle in the input image
- cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
- }
- //cvWaitKey(1);
- // Show the image in the window named "result"
- // while(1){
- // cvShowImage( "result", img );
- // char c = cvWaitKey(500);
- // if( c == 27 ) break;
- // }*/
- // Release the temp image create
- cvReleaseImage( &temp );
- if ( faces->total > 0 ){
- return img;
- }
- }
- return NULL;
- }
- int main(int argc,char** argv)
- {
- capture_video(1,argv[1]);
- return 0;
- }
Add Comment
Please, Sign In to add comment