Esempio n. 1
0
int main(int argc, char* argv[])
{
	VideoCapture cap(0);
	Mat image;
	string filename = argv[1];
	while(1){
		cap >> image;
		Mat detectFaceImage = detectFaceInImage(image, filename);
		imshow("detect face", detectFaceImage);
		if(waitKey(1) == 27){
			break;
		}
	}
	return 0;
}
Esempio n. 2
0
int main( int argc, char** argv ) 
{
    char flandmark_window[] = "flandmark_example1";
    double t;
    int ms;
    
    if (argc < 2)
    {
      fprintf(stderr, "Usage: flandmark_1 <path_to_input_image> [<path_to_output_image>]\n");
      exit(1);
    }
    
    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 wasn't 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
    
    // input image
    IplImage *frame = cvLoadImage(argv[1]);
    if (frame == NULL)
    {
      fprintf(stderr, "Cannot open image %s. Exiting...\n", argv[1]);
      exit(1);
    }
    // convert image to grayscale
    IplImage *frame_bw = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);
    cvConvertImage(frame, frame_bw);
    
    int *bbox = (int*)malloc(4*sizeof(int));
    double *landmarks = (double*)malloc(2*model->data.options.M*sizeof(double));
    detectFaceInImage(frame, frame_bw, faceCascade, model, bbox, landmarks);
    
    cvShowImage(flandmark_window, frame);
    cvWaitKey(0);
    
    if (argc == 3)
    {
      printf("Saving image to file %s...\n", argv[2]);
      cvSaveImage(argv[2], frame);
    }
    
    // cleanup
    free(bbox);
    free(landmarks);
    cvDestroyWindow(flandmark_window);
    cvReleaseImage(&frame);
    cvReleaseImage(&frame_bw);
    cvReleaseHaarClassifierCascade(&faceCascade);
    flandmark_free(model);
}
Esempio n. 3
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);
}
Esempio n. 4
0
int main(int argc, char** argv)
{
    /**********
    bfs::create_directory(input_dir / "prova");
    std::ofstream file(input_dir / "prova/testo.txt");
    file << "ciao!";
    file.close();
    if (!bfs::exists(input_dir / "prova/testo.txt"))
        std::cout << "Something went wrong." << std::endl;
    else std::cout << "apposto" << std::endl;
    **************/
    static const char *faceCascadeFilename = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml";
    CvHaarClassifierCascade* faceCascade = (CvHaarClassifierCascade*)cvLoad(faceCascadeFilename, 0, 0, 0 );
     if( !faceCascade ) 
     {
       printf("Could not load Haar cascade Face detection classifier in '%s'.", faceCascadeFilename);
       exit(1);
     }   

    if (!bfs::exists(input_dir))
    { 
        std::cout << "Directory does not exist!" << std::endl;
        return -1; // check existence of input_dir
    }

    bfs::directory_iterator end_itr;
    //int pictures=0;
    std::vector<int> compression_params; //params for imwrite function
    compression_params.push_back(CV_IMWRITE_PXM_BINARY);
    compression_params.push_back(1);
    std::string filename;
    IplImage* img;
    //CvMat* mat;
    
    for (bfs::directory_iterator itr(input_dir); itr!=end_itr; itr++)
    {
        try
        { 
            filename = itr->path().string();
            img = cvLoadImage( filename.c_str(),1);
        }
        catch(int e)
        {
            std::cout << "An exception occured: exception n: " << e << std::endl;
        }
       CvRect rect = detectFaceInImage(img, faceCascade);
       std::cout << "3 cvrect ok" << std::endl;
       if (rect.x > 0 && rect.y > 0 && rect.height > 0 && rect.width > 0)
            img = crop(img,rect);
       
       //mat = cvCreateMat(img->height, img->width,CV_32FC3);
       //std::cout << "5 createMat ok" << std::endl;
       //cvConvert(img,mat);
       //std::cout << "6 convert ok" << std::endl;
       //cvNamedWindow( "check2", 1 );
       //cvShowImage( "check2", mat );
       //cv::waitKey(30);
       //std::string tmp;
       // std::cin>>tmp;
       if(cvSaveImage(filename.c_str(), img))
      // if (cv::imwrite(filename,(cv::InputArray)mat,compression_params))
           std::cout<<"image " << filename << " written" << std::endl;
       else
            std::cout<<"can't write image"<<std::endl;
      // std::cout << filename << std::endl;
        
    }
    

    
    return 0;
}