OpenCV VideoCapture IP camera reconnection

2019-06-22 08:23发布

问题:

I'm reading images from a camera through HTTP. This is the code:

Mat src;
VideoCapture cap();
cap.open("http://192.168.1.10:8008"); // IP camera

while(1) {
    cap.read(src);
    // Other code
}

It works perfectly, but after running for a while if I physically disconnect the camera then the code hangs forever in cap.read(src);.

I need some way to return from read if, let's say, after 5 seconds there is no new image.

There is another problem. If the camera is reconnected while the program is waiting in read then it keeps hanged anyway. It would be good if VideoCapture is able to grab frames again once the camera is reconnected.

回答1:

http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-read

If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.

try this

if(cap.read(src) == false ) break;