如果检测为真,则写入/保存视频流

时间:2013-08-22 10:20:51

标签: c++ opencv video face-detection

我想在检测到真实后写一个视频流。

我将此链接用作Videowrite example

我的代码实现如下:

int main(int argc, const char** argv) {
    bool detection = false;
    VideoCapture cap(-1);

    if (!cap.isOpened())
    {
        printf("ERROR: Cannot open the video file");
    }
    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
    Size frameSize (static_cast<int>(dWidth), static_cast<int>(dHeight));

    VideoWriter record ("/home/hacker/MyVideo.avi", CV_FOURCC('P','I','M','1'),
                        30, frameSize, true);

    if (!record.isOpened())
    {
        printf("Error: Failed to write the video");
        return -1;
    }

    while (true)
    {
        Mat frame;

        if (!frame.empty())
        {
            detectAndDisplay(frame);
        }
        else
        {
            printf(" --(!) No captured frame -- Break!"); break;
        }

        if (detection == true)
        {
            record.write(frame);
        }

        char c = cvWaitKey(33);
        if (c == 27) { break; }
    }
    return 0;
}

在我的主目录中,我可以看到Myvideo.avi,但它是空的。

我在命令行上遇到以下错误:

VIDIOC_QUERMENU: Invalid argument
VIDIOC_QUERMENU: Invalid argument 
Frame size: 640x480  Output #0, avi, to '/home/hacker/MyVideo.avi":
Stream #0.0: Video: mpeg1video (hq), yvu420p, 640x480, q=2-31, 19660
kb/s, 9ok tbn, 23,98 tbc
--(!) No captured frame -- Break! Process returned 0 (0x0) execution time: 0,75 s

2 个答案:

答案 0 :(得分:0)

你应该发布videowriter(record.Release();)。它关闭文件。

答案 1 :(得分:0)

我试着像这样解决它:

但我有两个问题:

如果detecAndDisplay(frame)== true,我想保存MyVideo.avi。但无论如何他都保存了它(有一个空的视频记录)。如果保存视频录制速度更快。

int main( int argc, const char** argv ) {

 Mat frame;



  VideoCapture capture(-1); // open the default camera
  if( !capture.isOpened() )
  {
       printf("Camera failed to open!\n");
       return -1;
   }

   capture >> frame; // get first frame for size

    for(;;)
      {
          // get a new frame from camera
          capture >> frame;
         //-- 3. Apply the classifier to the frame
         if( !frame.empty() )
         {
             detectAndDisplay( frame );
         }

         if(detectAndDisplay(frame)==true)
         {
             // record video
             VideoWriter record("MyVideo.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);

             if( !record.isOpened() )
             {
                 printf("VideoWriter failed to open!\n");
                 return -1;
            }

          // add frame to recorded
          record << frame;
         }


          if(waitKey(30) >= 0) break;
        }

  return 0;
 }




/** @function detectAndDisplay */
/** this function detect face draw a rectangle around and detect eye & mouth and draw a circle around */
bool detectAndDisplay( Mat frame ) {
...
...
    return true;

}