Example #1
0
static void cb_StartStopRecord( Fl_Widget*, void* )
{
    if( !is_recorded )
    {
        if( video_image )
        {
            writer = cvCreateAVIWriter( "c:\\test.avi", -1, 15, cvGetSize( video_image ));
            if( writer )
            {
                record_button->box( FL_DOWN_BOX );
                is_recorded = 1;
            }
        }
    }
    else
    {
        record_button->box( FL_UP_BOX );
        cvReleaseVideoWriter( &writer );
        is_recorded = 0;
    }
}
Example #2
0
int main( int argc, char** argv )
{
    char flandmark_window[] = "flandmark_example2";
    double t;
    int ms;

    const char *infname = 0;
    const char *outfname = 0;
    bool video = false, savevideo = false;

    CvVideoWriter *writer = 0;
    int vidfps, frameW, frameH, fourcc, nframes = 0;
    //int fourcc = CV_FOURCC('D', 'I', 'V', 'X');

    CvCapture* camera = 0;	// The camera device.
    IplImage *frame = 0;

    if (argc == 1)
    {
        exit(1);
    }

    if (argc > 1)
    {
        infname = argv[1];
        printf("infname = %s\n", infname);
		video = (strlen(infname) > 1) ? true : false;

		if (video)
        {
            frame = getCameraFrame(camera, infname);
            frameH = (int)cvGetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT);
            frameW = (int)cvGetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH);
            fourcc = (int)cvGetCaptureProperty(camera, CV_CAP_PROP_FOURCC);
            nframes = (int)cvGetCaptureProperty(camera, CV_CAP_PROP_FRAME_COUNT);
            vidfps = (int)cvGetCaptureProperty(camera, CV_CAP_PROP_FPS);
        } else {
			int width=320, height=240, camid;
			camid = ::atoi(argv[1]);
			if (argc > 4)
			{
				width = ::atoi(argv[3]);
				height = ::atoi(argv[4]);
			}
            frame = getCameraFrame(camera, 0, camid, width, height);
            vidfps = 10;
            frameW = 640;
            frameH = 480;
            fourcc = CV_FOURCC('D', 'I', 'V', 'X');}
    }
    if (argc > 2)
    {
        outfname = argv[2];
        savevideo = true;
        writer = cvCreateAVIWriter(outfname, fourcc, vidfps, cvSize(frameW, frameH));
        //writer = cvCreateVideoWriter(outfname, fourcc, vidfps, cvSize(frameW, frameH));
    }

    cvNamedWindow(flandmark_window, 0);

    // Haar Cascade file, used for Face Detection.
    char faceCascadeFilename [] = "haarcascade_frontalface_alt.xml";
    // Load the HaarCascade classifier for face detection.
    CvHaarClassifierCascade* faceCascade;
    faceCascade = (CvHaarClassifierCascade*)cvLoad(faceCascadeFilename, 0, 0, 0);
    if( !faceCascade )
    {
        printf("Couldnt load Face detector '%s'\n", faceCascadeFilename);
        exit(1);
    }

    // ------------- begin flandmark load model
    t = (double)cvGetTickCount();
    FLANDMARK_Model * model = flandmark_init("flandmark_model.dat");
    if (model == 0)
    {
        printf("Structure model was not created. Corrupted file flandmark_model.dat?\n");
        exit(1);
    }
    t = (double)cvGetTickCount() - t;
    ms = cvRound( t / ((double)cvGetTickFrequency() * 1000.0) );
    printf("Structure model loaded in %d ms.\n", ms);
    // ------------- end flandmark load model

    int *bbox = (int*)malloc(4*sizeof(int));
    double *landmarks = (double*)malloc(2*model->data.options.M*sizeof(double));
    IplImage *frame_bw = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);

    char fps[50];
    CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 1, CV_AA);

    int frameid = 0;
    bool flag = true;

    if (video)
    {
        while (flag)
        {
            if (++frameid >= nframes-2)
            {
                flag = false;
                break;
            }

            t = (double)cvGetTickCount();
            frame = getCameraFrame(camera, infname);

            if (!frame)
            {
                flag = false;
                break;
            }

            cvConvertImage(frame, frame_bw);
            detectFaceInImage(frame, frame_bw, faceCascade, model, bbox, landmarks);

            t = (double)cvGetTickCount() - t;
            sprintf(fps, "%.2f fps", 1000.0/( t/((double)cvGetTickFrequency() * 1000.0) ) );
            cvPutText(frame, fps, cvPoint(10, 40), &font, cvScalar(255, 0, 0, 0));
            cvShowImage(flandmark_window, frame );

            cvWaitKey(10);

            if (savevideo)
            {
                cvWriteFrame(writer, frame);
            }
        }
    } else {
        while ( cvWaitKey(20) != 27 )
        {
            t = (double)cvGetTickCount();
            // Quit on "Escape" key.
            frame = video ? getCameraFrame(camera, infname) : getCameraFrame(camera);

            cvConvertImage(frame, frame_bw);
            detectFaceInImage(frame, frame_bw, faceCascade, model, bbox, landmarks);

            t = (double)cvGetTickCount() - t;
            sprintf(fps, "%.2f fps", (1000.0*1000.0*(double)cvGetTickFrequency())/t );
            cvPutText(frame, fps, cvPoint(10, 40), &font, cvScalar(255, 0, 0, 0));
            cvShowImage(flandmark_window, frame);

            if (savevideo)
            {
                cvWriteFrame(writer, frame);
            }
        }
    }

    // Free the camera.
    free(landmarks);
    free(bbox);
    cvReleaseCapture(&camera);
    cvReleaseHaarClassifierCascade(&faceCascade);
    cvDestroyWindow(flandmark_window);
    flandmark_free(model);
}