When should I use VideoCapture.release()?

asked 2018-01-21 18:28:22 -0500

calocabe96 gravatar image

This is a part of my code in C++, I'm using it to count live traffic and the idea is to keep running the program at least 20 hours, 7 days a week. I mean this is an example of how I'm catching the frames but I need to do it all the time (like a While(true) ) and I'm having some problems.

#include "opencv2/opencv.hpp"
#include <time.h>

using namespace cv;
using namespace std;

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

    // Start default camera
    VideoCapture video("ip of my ipCam");

    // With webcam get(CV_CAP_PROP_FPS) does not work.
    // Let's see for ourselves.

    double fps = video.get(CAP_PROP_FPS);
    // If you do not care about backward compatibility
    // You can use the following instead for OpenCV 3
    // double fps = video.get(CAP_PROP_FPS);
    cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;


    // Number of frames to capture
    int num_frames = 120;

    // Start and end times
    time_t start, end;

    // Variable for storing video frames
    Mat frame;

    cout << "Capturing " << num_frames << " frames" << endl ;

    // Start time
    time(&start);

    // Grab a few frames
    for(int i = 0; i < num_frames; i++)
    {
        video >> frame;
    }

    // End Time
    time(&end);

    // Time elapsed
    double seconds = difftime (end, start);
    cout << "Time taken : " << seconds << " seconds" << endl;

    // Calculate frames per second
    fps  = num_frames / seconds;
    cout << "Estimated frames per second : " << fps << endl;

    // Release video
    video.release(); //----------------THIS------------------
    return 0;
}

Off course I take the frames and procces them to count the traffic... but I mean I'm running my proggram and after 5 minutes it crashes, so I was wondering if shoul I VideoCapture.release() every X minuto to prevent problems like

OpenCV Error: Insufficient memory (Failed to allocate 2764800 bytes) in OutOfMemoryError, file /home/pi/opencv/opencv-3.3.0/modules/core/src/alloc.cpp, line 55
OpenCV Error: Assertion failed (u != 0) in create, file /home/pi/opencv/opencv-3.3.0/modules/core/src/matrix.cpp, line 436
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/pi/opencv/opencv-3.3.0/modules/core/src/matrix.cpp:436: error: (-215) u != 0 in function create

In the image processing I use an array to store the frame so I can process them later, but I'm emptying the array at the same time, so I don't know why I get this errors.

edit retag flag offensive close merge delete

Comments

sturkmen gravatar imagesturkmen ( 2018-01-21 20:10:33 -0500 )edit
1

" I take the frames and procces them to count the traffic.." -- unfortunately, without seeing that part, we cannot find out.

also: napkin maths: 640*480*3*120 = 110592000 . quite unlikely, that the VideoCapture is to blame here.

berak gravatar imageberak ( 2018-01-22 04:26:28 -0500 )edit

"When should I use VideoCapture.release()?" this is what I'm asking about, like in any situation... when should I use it, no matter if I'm proccesing images or not. And other thing @berak640*480*3*120..??? I still not getting it. If you can answer in spanish, much better :)

calocabe96 gravatar imagecalocabe96 ( 2018-01-22 10:00:56 -0500 )edit

you don't need to call release() there, because it will be automatically invoked from the return in the next line.

the VideoCapture does indeed allocate a couple of buffers, but that's done in the initialization once, and the memory does not change.

all i'm saying is: you're worrying in the wrong place, maybe. rather look at your processing

berak gravatar imageberak ( 2018-01-22 10:56:03 -0500 )edit

ohhh, okay @berak thanks, you too @sturkmen

calocabe96 gravatar imagecalocabe96 ( 2018-01-22 11:15:03 -0500 )edit