IP摄像机断开连接时,视频捕获卡住了

时间:2018-07-04 09:00:52

标签: opencv visual-c++

我正在使用opencv 2.4.9 mvsc ++ 2012和Qt,在连接摄像机时该代码可以正常工作,但是如果摄像机断开连接,则该代码将停留在vcap.open(videostreamadress) 这是我的代码

const string videoStreamAddress="rtsp://admin:admin@192.168.1.11:88/live/h264/VGA";
VideoCapture  vcap;
Mat image_input;
cameraOpen=true;
//first open the graphic widget to display the camera stream
ui.graphicsView->setEnabled(TRUE);
    while ((vcap.open(videoStreamAddress)==true)&&(cameraOpen==true))
         {
            if(vcap.read(image_input)==false) 
                {
                //QmessageBox 
                QMessageBox msgBox;
                msgBox.setText("probleme de connexion a la caméra");
                msgBox.exec();
                //close_camera_feed();
                break;
                }           
                vcap.set(CV_CAP_PROP_FPS, 1);
                //vcap.read(image_input);
                qimage_input = QImage((const unsigned char*)(image_input.data), 
                                        image_input.cols,image_input.rows, 
                                        QImage::Format_RGB888).rgbSwapped();
                image = QPixmap::fromImage(qimage_input);
                scene = new QGraphicsScene(this);
                scene->addPixmap(image);
                scene->setSceneRect(image.rect());
                ui.graphicsView->setScene(scene);
                //to 
                qApp->processEvents();
                 //thread t1(task1, "Hello");
                detect_license_plate(image_input);
            }

        if(vcap.open(videoStreamAddress)==false)
            {
                QMessageBox msgBox;
                msgBox.setText("La Caméra est déconnecté, vérifier l'uinstallation");
                msgBox.exec();  
            }   

感谢您对高级的帮助!

2 个答案:

答案 0 :(得分:0)

根据您的评论,使用以下模板访问您的相机。

cv::VideoCapture vcap("url");
if(!vcap.isOpened())
{
    std::cout<<"Camera could not be opened"<<std::endl;
    return -1;
}

cv::Mat frame;
bool ret;
while(true) {

    ret = vcap.read(frame);
    if(!ret)
    {
        std::cout<<"Empty frame returned"<<std::endl;
        break;
    }

    /* Process frame here*/


    /* Set loop terminating condition here*/

}

vcap.release();

答案 1 :(得分:0)

我解决此问题的方法是使用线程,我在一个单独的线程中检查摄像头连接,如果该线程未完成并且不返回任何值,则等待一段时间(2秒),这意味着相机未连接。代码为

cameraOpen=true; 
//first open the graphic widget to display the camera stream
ui.graphicsView->setEnabled(TRUE);
cameraIsConnected = false ;

while ((check_camera_thread()==true)&&(cameraOpen==true))   
    //while ((vcap.isOpened()==true)&&(cameraOpen==true))
         {
            if(vcap.read(image_input)==false) 
                {
                //QmessageBox 
                QMessageBox msgBox;
                msgBox.setText("probleme de connexion a la caméra");
                msgBox.exec();
                //close_camera_feed();
                break;
                }           
                vcap.set(CV_CAP_PROP_FPS, 1);
                //vcap.read(image_input);
                qimage_input = QImage((const unsigned char*)(image_input.data), 
                                        image_input.cols,image_input.rows, 
                                        QImage::Format_RGB888).rgbSwapped();
                image = QPixmap::fromImage(qimage_input);
                scene = new QGraphicsScene(this);
                scene->addPixmap(image);
                scene->setSceneRect(image.rect());
                ui.graphicsView->setScene(scene);
                //to 
                qApp->processEvents();
                 //thread t1(task1, "Hello");
                detect_license_plate(image_input);
            }

        if(vcap.isOpened()==false)
            {
                QMessageBox msgBox;
                msgBox.setText("La Caméra est déconnecté, vérifier l'uinstallation");
                msgBox.exec();  
            }                       
vcap.release();
close_camera_feed();
 } 
bool check_camera_thread()
 {
   std::thread t0( check_camera);   
   Sleep(2000);
   t0.detach();
   return cameraIsConnected;
  }
bool check_camera()
{
  vcap.open(videoStreamAddress);
  cameraIsConnected= true ;
  return cameraIsConnected;
}