opencv: VideoCapture "frame grabbing" is blocking using an IP Camera
Transferred from http://code.opencv.org/issues/2786
|| Walter Lucetti on 2013-02-08 15:14
|| Priority: Normal
|| Affected: branch 'master' (2.4.9)
|| Category: highgui-camera
|| Tracker: Bug
|| Difficulty: None
|| PR:
|| Platform: None / None
VideoCapture “frame grabbing” is blocking using an IP Camera
Grabbing a frame using VideoCapture with an IP Camera is blocking if the camera goes disconnected.
I tried the methods:
1) grabber >> frame;
2) grabber.read( frame );
3) grabber.grab(); <- blocks here
grabber.retrieve( frame );
each one blocks if the camera is disconnected from the network.
This is very bad because it does not let you understand if there are problems with the connections.
History
Alexandro Picolini on 2014-04-22 13:25
Hi Vadim,
This freezing occurs in the read method which calls av_read_frame function. So that this function does not freeze in the event of disconnection must be registered to url_set_interrupt_cb callback.
Exist forecast for this fix?
Tks,
Alexandro Picolini on 2014-04-27 20:59
Hello Vadim.
Following patch to fix this problem.
You can put this in the official build?
Thank you.
- File cap_ffmpeg_impl.hpp_patch added
Andy Bean on 2014-06-03 19:40
I can confirm this issue in 2.4.9 release running on windows 7. This is really a showstopper when using opencv with network cameras.
Corentin Hamel on 2014-08-06 03:21
Hi Alexandro,
I'm having trouble to see how this patch is gonna work and actually I can't get it work. The _capture.read()_ is still blocking if there is a connection loss.
Even with the patch, the program is getting stuck in the _ while (!valid)_ loop of _CvCapture_FFMPEG::grabFrame()_ line 693 as there is no new frame, the only way to get out of this loop is when _count_errs > max_number_of_attempts_ line 738 but this is taking a while so the _capture.read()_ is blocked.
I thought when the callback function _cvCapture_FFMPEG_Timeout::checkInterrupt_ called with opaque argument and will return 1 (when timeout is reached), the blocking operation will be aborted. But I'm not sure what the blocking operation at this point (I thought it will exist the while loop).
My knowledge on the openCV code are limited so maybe I'm not understanding something.
Thanks
Corentin Hamel on 2014-08-06 03:33
- File cap_ffmpeg_impl.hpp added
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 21 (2 by maintainers)
I am still experiencing this issue in 3.1.0
My problem was that I was using the code from 3.1.0. Checking out master and recompiling opencv resolves the problem.
I see that the default duration of 30 seconds is set in `/modules/videoio/src/cap_ffmpeg_impl.hpp
Is it possible to control the timeout duration in my python code without changing those definitions in that file and recompiling opencv?
For those interested by this problem, I found that OpenCV v4.5.4 now includes a way to provide a read timeout but only if you are using FFmpeg. It works using
params
of theVideoCapture::open
class method. (see below for references)In C++, it looks like this:
Notice that the provided timeout is in milliseconds. You can also control the timeout when opening your capture device with the parameter
cv::CAP_PROP_OPEN_TIMEOUT_MSEC
.See documentation for reference: https://docs.opencv.org/4.5.4/d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704dacdc395b7bfcccb0cd56cdd7e6dc8e0d1 https://docs.opencv.org/4.5.4/d8/dfe/classcv_1_1VideoCapture.html#ac9e6045e168938b82bfc5ed0164e0481
This cause from the cap_ffmpeg_impl.hpp. (From OpenCV Source: modules/videoio/src/) There is no any callback function setting to determine if grabFrame is timeout. (cause by av_read_frame) In ffmpeg, you need to set like this: AVFormatContext::interrupt_callback.opaque = (void*)timeout ; AVFormatContext::interrupt_callback.callback = &SomeTimeoutHandler::checkInterrupt; if you want to handle the timeout callback.
So I directly use the ffmpeg not the videocapture to handle the timeout and more video io functions in detail. Hopefully, maybe someone can add the fix for the callback setter one day.