【问题标题】:How to capture the video from webcam in the interval of 2 minutes using OpenCV如何使用 OpenCV 以 2 分钟的间隔从网络摄像头捕获视频
【发布时间】:2015-04-10 08:50:33
【问题描述】:

我正在尝试从网络摄像头 (Logitech C170) 捕获视频。到目前为止,我能够捕获视频并将其保存在 .avi 容器中。

我的 c++ 代码是这样的

using namespace std;
using namespace cv;

int main(int, char**)
{
    VideoCapture capture(0);
    .
    .
    .
    .
    VideoWriter video("record.avi", CV_FOURCC('M', 'J', 'P', 'G'), frame_rate, Size(frame_width,frame_height), true);
    .
    .
    .
    .

    Mat frame;
    .
    .
    .
    .
    for(;;)
    {
       capture >> frame;
       video.write(frame);

    }

    return 0;
}

我的下一个目标是

     Record         Sleep          Record
|<------------>|<------------>|<------------>| Goes for infinite loop...
    3 Minutes      2 Minutes      3 Minutes
        |              |              |
        |              |              |
        v              v              v
     save to   Cam goes to sleep    save to
  /records/1.avi                /records/2.avi

我使用的是opencv-2.4.3,代码是c++。

如果我需要任何其他信息,请告诉我。

我怎样才能做到这一点?请提出宝贵的建议?

提前致谢。

【问题讨论】:

    标签: c++ opencv video video-streaming


    【解决方案1】:

    我建议使用时钟来测量时间。

    在循环运行时继续检查时钟,一旦达到 3 分钟,切换到睡眠模式功能,并且一旦达到 2 分钟,再次切换回来。

    下面是一些代码,用于对从this SO 答案部分蚕食的函数计时。

    我相信您可以编辑它以保持您的录制时间并允许您在录制和睡眠之间切换。

    希望这会有所帮助。

    #include <iostream>
    #include <cstdio>
    #include <ctime>
    
    int main()
    {
        std::clock_t start;
        double duration;
    
        start = std::clock();
    
        // Run your frame grabbing code here //
    
        duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
    }
    

    【讨论】:

    • 我也在考虑使用这种方法。但后来我的思绪转向了另一个方向。我想我现在就试一试。如果我成功了,我会发布。
    • 嗨@Aphire 我现在有一个解决这个问题的方法。我也会尝试你的方法。完成后我会告诉你的。请检查此解决方案。
    【解决方案2】:

    所以,这就是解决方案。

    #include<iostream>
    #include<unistd.h>
    #include<sstream>
    #include<string>
    #include<ctime>
    #include<opencv/cv.h>
    #include<opencv2/opencv.hpp>
    #include<opencv2/core/core.hpp>
    #include<opencv2/highgui/highgui.hpp>
    
    using namespace std;
    using namespace cv;
    
    string currentDateTime() {
        time_t     now = time(0);
        struct tm  tstruct;
        char       buf[80];
        tstruct = *localtime(&now);
        strftime(buf, sizeof(buf), "%X", &tstruct);
        return buf;
    }
    
    int main(int, char**)
    {
        VideoCapture capture(0);
        .
        .
        .
        .
        int frame_rate = 10;
        .
        .
        .
        .
        for(int i = 0; i<4 ; i++)    // for infinite loop remove iteration
        {   
            time_t timer_start, timer_stop;
            Mat frame;
            stringstream recordstr;
            recordstr << "records/" << i << ".avi";
            string recordfile = recordstr.str();
    
            VideoWriter video(recordfile, CV_FOURCC('M', 'J', 'P', 'G'), frame_rate, Size(frame_width,frame_height), true);
    
            cout<<"video "<<i<<".avi started capturing at "<< currentDateTime() <<endl;
            time (&timer_start);
            for(int j = 0; j < numFrames; j++){
                capture >> frame;
                if(frame.empty()){
                    cout << "Failed to capture an image" << endl;
                    return -1;
                }
                video.write(frame);
            }
            time (&timer_stop);
            double timeDifference = difftime (timer_stop, timer_start);
            cout<<"video "<<i<<".avi stopped capturing at "<< currentDateTime() <<" & took "<<timeDifference<<" seconds"<<endl;
            sleep(60);
        }
        return 0;
    }
    

    输出是:

    video 0.avi started capturing at 12:09:18
    video 0.avi stopped capturing at 12:09:55 & took 37 seconds
    video 1.avi started capturing at 12:10:55
    video 1.avi stopped capturing at 12:11:32 & took 37 seconds
    video 2.avi started capturing at 12:12:32
    video 2.avi stopped capturing at 12:13:09 & took 37 seconds
    

    我将不胜感激任何其他解决方案。所以请发帖,如果你有的话。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 2017-04-27
      • 2015-08-18
      • 2014-06-05
      • 1970-01-01
      • 2015-07-17
      相关资源
      最近更新 更多