Вопрос

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.

Это было полезно?

Решение

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++;
}

Другие советы

You can try to call waitKey(2000) after each capturing.

Note that the function will not wait exactly 2000ms, it will wait at least 2000ms, depending on what else is running on your computer at that time.

To achieve accurate frame rate, you can set the frame rate of capturing by:

cap.set(CV_CAP_PROP_FPS, 0.5);

Me personally, i would recommend using a modulo operator on the current frame like %2 == would check for every second frame.

if(capture.get(CV_CAP_PROP_POS_FRAMES) % 2 == 0)
    //your code to save

Changing 2 to 3 or 5 you can define the offset.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top