Ask Your Question
2

Videocapture suddenly blocks on reading a frame from IP camera

asked 2016-04-21 15:22:10 -0600

paty.r15 gravatar image

I'm using OpenCV 3. Grabbing a frame using VideoCapture with an IP Camera is blocking if the camera goes disconnected from the network or there is an issue with a frame. I first check if videoCapture.isOpened(). If it is, I tried these methods but nothing seems to work:

1) grabber >> frame

if(grabber.isOpened()) {
    grabber >> frame; 
    // DO SOMETHING WITH FRAME
}

2) read

if(grabber.isOpened()) {
    if(!grabber.grab()){
      cout << "failed to grab from camera" << endl;
    } else {
      if (grabber.retrieve(frame,0) ){
        // DO SOMETHING WITH FRAME
      } else {
        // SHOW ERROR
      }
    }
}

3) grab/retrieve

if(grabber.isOpened()) {
    if ( !grabber.read(frame) ) {
      cout << "Unable to retrieve frame from video stream." << endl;
    }
    else {
     // DO SOMETHING WITH FRAME
    } 
}

The video stream gets stuck at some point grabbing a frame with all of the previous options, each one blocks but doesn't exit or returns any error.

Do you know if there is a way to handle or solve this? Maybe some validations, try/catch or timer?

edit retag flag offensive close merge delete

Comments

Did you make any progress on this? I've recently experienced the same thing. In my case, sporadically, VideoCapture::read() blocks when it experiences a particular type of error (I don't know exactly the nature of the error, but one of my cameras produces it). In this case the read() call never returns...

logidelic gravatar imagelogidelic ( 2016-05-20 10:01:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-12 17:57:56 -0600

aestaq gravatar image

I have also faced the same problem while capturing frames from IP camera. I searched on different forums and dug into the source files to pinpoint the line, function and the file where the grab call is getting stuck. So the function CvCapture_FFMPEG::grabFrame() (sources/modules/highgui/src/cap_ffmpeg_impl.h) is stuck in while loop (check the file for clarity) when IP camera is disconnected and somehow the timeout interrupt is not working in this case. You can check yourself by adding log statements at different points in the CvCapture_FFMPEG::grabFrame() function.

So now coming to the solution. There are two ways: First, make a separate thread in which you call the grab frame function. Monitor this thread in the main thread. When the grab function blocks the thread then kill this thread on timeout (which you can decide) and respawn again if you want to retry grabbing the frame. The other way around is to compile OpenCV with Gstreamer instead of ffmpeg (install gstreamer libraries and set the flags as WITH_GSTREAMER=ON and WITH_FFMPEG=OFF when compiling the OpenCV). After compiling with Gstreamer test the grab function: it will return false when your IP camera is disconnected (the way you expected the grab function to work).

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-04-21 15:22:10 -0600

Seen: 5,832 times

Last updated: Nov 12 '16