簡體   English   中英

如何使用OpenCV在2分鍾的間隔內從網絡攝像頭捕獲視頻

[英]How to capture the video from webcam in the interval of 2 minutes using OpenCV

我正在嘗試從網絡攝像頭(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 ++中。

如果我需要其他任何信息,請告訴我。

我怎樣才能做到這一點? 請給您寶貴的建議?

提前致謝。

我建議使用時鍾來測量時間。

循環運行時,請檢查時鍾,一旦達到3分鍾,請切換到睡眠模式功能,直到達到2分鍾的時間,再重新調回。

下面是一些代碼,以時間的函數部分地拆散這個蘇答案。

我確定您可以對其進行編輯以保留錄制時間,並允許您在錄制和睡眠之間進行切換。

希望這可以幫助。

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

所以,這是解決方案。

#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

我將不勝感激任何其他解決方案。 因此,如果有的話,請發表。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM