Detect when the ipCamera is unplugged opencv

asked 2014-12-05 05:50:17 -0600

Jesus gravatar image

updated 2020-09-22 19:18:17 -0600

I aware that this is a topic, but I tried many solutions and for me it does not works, for that I put my specific case. I am working with an Ipcamera, and I have this code in C++:

cv::VideoCapture cap("http://127.0.0.1:80/videostream.cgi?resolution=320x240&rate=0& user=admin&pwd=admin&.mjpg");
        //while we keep connected to the ip camera, we process the frames we receive
            if (!cap.isOpened())
            {
                cout<<"NO INTERNET"<<endl;

                //cap.release();
                fallo=true;
                v1=0;
                pthread_exit(NULL);

            }

                while ( cap.isOpened())
                {
                    //we get the frame from the ip camera
                    //cap.read(frame);
                    if(fallo==true)
                        fallo=false;

                    cv::Mat3b frame;

                    if(!cap.read(frame))
                    {
                         cout << "UNPLUGGED";
                         cout << "UNPLUGGED";
                         pthread_exit(NULL);
                    }

                    cap >> frame;

                    //if the frame is empty we break
                    if(frame.empty()) break;

                    //we show the video frame in "video" window
                    cv::imshow("Front", frame);

                    if(v1 == 1){
                        v1 = 0;
                        pthread_exit(NULL);
                    }

                    //To close the window
                    if(cv::waitKey(20) >= 0) break;
                }
}

What I want is to know when the camera is unplugged, because I want to release the thread for throw another camera without problems. But is imposible detected it.

I tried with this:

if(!cap.read(frame))

But nothing, I tried to put an cout after while but also nothing happend, and even I tried to put the while inside of a trybut again nothing.

My question is How can I detect that the camera is disconnected?

Many Thanks

edit retag flag offensive close merge delete