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

Write/save videostream if detection true

本文关键字:保存 视频 结果 检测 true 如果      更新时间:2023-10-16

我想在检测结果为true后编写一个视频流。

我使用此链接作为Videowrite示例

我的代码实现看起来是这样的:

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

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

我试着这样解决它:

但我有两个问题:

如果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;
}

这可能是视频文件为空的原因。

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