Search code examples
c++opencvimage-processingframe-rate

capture frames after every 2 seconds


I am doing a project on face detection from video. I detected faces from the video, but it is capturing every frame, so i am getting so many images with in a second itself (so many frames got captured in a second).

Problem: I want to reduce that, capture frame after every 3 second is enough. I tried to use wait() ,sleep() functions. But they are just stop running the video for sometime,nothing else is happening. Can any one help me to overcome from this.

    #include <cv.h>   
    #include <highgui.h>
    #include <time.h>
    #include <stdio.h>
    using namespace std;
    IplImage *frame;
    int frames;
    void facedetect(IplImage* image);
    void saveImage(IplImage *img,char *ex);
    IplImage* resizeImage(const IplImage *origImg, int newWidth,int newHeight, bool      keepAspectRatio);
    const char* cascade_name="haarcascade_frontalface_default.xml";//specify classifier     cascade.
    int k;
    int main(int argc, char** argv)
    {
      // OpenCV Capture object to grab frames
      //CvCapture *capture = cvCaptureFromCAM(0);
      CvCapture *capture=cvCaptureFromFile("video.flv");
      //int frames=cvSetCaptureProperty(capture,CV_CAP_PROP_FPS, 0.5);
      //double res1=cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
      //cout<<"res"<<res<<endl;
      // start and end times
       time_t start, end;
       // fps calculated using number of frames / seconds
       double fps;
      // frame counter
      int counter = 0;
      // start the clock
     time(&start);
     //while(cvGrabFrame(capture))
     while(1)
     {
    //if(capture.get(CV_CAP_PROP_POS_FRAMES) % 2 == 0)
    frames=cvSetCaptureProperty(capture,CV_CAP_PROP_FPS, 0.5);
    if(frames%2==0)
    {
    frame = cvQueryFrame(capture);
    cout<<"Frame"<<frame<<endl;
    facedetect(frame);
    }

     }

  cvReleaseCapture(&capture);
  return 0;
}

I gave cvWaitKey(2000) after every frame is captured.


Solution

  • This would have been my trial. It saves one image per 30 frames. when you say too many images in one second, I understand that you are referring to saved faces.

    int counter = 0;
    // start the clock
    time(&start);
    //while(cvGrabFrame(capture))
    while(1)
    {
      frame = cvQueryFrame(capture);
      cout<<"Frame"<<frame<<endl;
      if(count%30==0)
      {
       facedetect(frame);
      }
      count++;
    }
    

    if you really meant of skipping the frames, then try this. one frame per second might be the outcome of below code.

    while(1)
    {
     if(count%30==0)
      { 
       frame = cvQueryFrame(capture);
       cout<<"Frame"<<frame<<endl;
       facedetect(frame);
      }
      count++;
    }