Detecting unplugged camera with OpenCV2.4

asked 2012-09-19 11:44:15 -0600

giroy gravatar image

I just upgraded to OpenCV2.4 and I'm trying to detect when a camera is unplugged. According to the documentation on the cv::VideoCapture::read command, "if no frames have 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." However, in my application, this is not the case. When I unplug the camera, it keeps reading the last frame from when the camera was plugged in and never returns false or NULL. Is this a bug in OpenCV or am I doing something wrong? My code is below.

VideoCapture cap(0);
if(cap.isOpened()){
    Mat frame;
    while(cap.read(frame)){
        // do stuff
        Sleep(30);
    }
}
cap.release();
edit retag flag offensive close merge delete

Comments

I find the same behavior using Windows XP and Visual Studio 2008 and connecting to an AXIS camera over the network. When I unplug the camera cap.read(frame) never returns. It seems like a tight loop.

Steve Roe gravatar imageSteve Roe ( 2012-12-10 18:39:51 -0600 )edit