Saurav
Saurav

Reputation: 76

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

I am trying to capture a video from webcam (Logitech C170). So far i am able to capture the video and save it in .avi container.

My c++ code goes like this

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

My next target is

     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

I am using opencv-2.4.3, code is in c++.

Please let me know if any other information is required from my side.

How can i achieve this? Please give your valuable suggestions?

Thanks in advance.

Upvotes: 0

Views: 2313

Answers (2)

Saurav
Saurav

Reputation: 76

So, here is the solution.

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

Output is:

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

I will appreciate any other solution. so please do post, if you have any.

Thanks

Upvotes: 1

Aphire
Aphire

Reputation: 1652

I would suggest using a clock to measure time.

Keep checking the clock while your loop is running, once 3 minutes has been reached, swap over into a sleep mode function, and time that, once 2 minutes has been reached, swap back again.

Below is some code to time a function partially cannibalized from this SO answer.

I'm sure you can edit this to keep your recording time and allow you to swap between recording and sleeping.

Hope this helps.

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

Upvotes: 1

Related Questions