Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Detect when the ipCamera is unplugged opencv

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

Detect when the ipCamera is unplugged opencv

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