Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV VideoCapture IP camera reconnection

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.

like image 819
dablak Avatar asked Nov 13 '22 01:11

dablak


1 Answers

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;

like image 83
Birol Kuyumcu Avatar answered Nov 15 '22 05:11

Birol Kuyumcu