OpenCV VideoCapture IP摄像机重新连接 [英] OpenCV VideoCapture IP camera reconnection
问题描述
我正在通过HTTP从相机读取图像.这是代码:
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
}
它工作正常,但是运行了一段时间后,如果我断开了相机的连接,则代码将永久挂在cap.read(src);
中.
It works perfectly, but after running for a while if I physically disconnect the camera then the code hangs forever in cap.read(src);
.
如果5秒钟后没有新图像,我需要某种方法从read
返回.
I need some way to return from read
if, let's say, after 5 seconds there is no new image.
还有另一个问题.如果程序在read
中等待时相机重新连接,则相机仍会挂起.如果重新连接相机后VideoCapture
能够再次抓取帧,那就太好了.
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.
推荐答案
如果没有抓取帧(相机已断开连接,或者视频文件中没有其他帧),则方法返回false,函数返回NULL指针.
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.
尝试
if(cap.read(src)== false) 休息;
if(cap.read(src) == false ) break;
这篇关于OpenCV VideoCapture IP摄像机重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!