【问题标题】:Videocapture stuck when IP camera is disconnected当 IP 摄像机断开连接时,视频捕获卡住
【发布时间】:2018-07-04 20:01:57
【问题描述】:

我正在使用 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();  
            }   

感谢您的帮助!

【问题讨论】:

  • 为什么每次迭代都要初始化相机?更改两行:while ((vcap.isOpened())){} 和 if(vcap.isOpened()==false){}。
  • 我试过 vcap.isOpened() 也没有,对于 if(vcap.isOpened()==false){} 是知道while循环退出的原因(通过用户 (if(vcap.open(videoStreamAddress)==false)) 或因为相机离线。

标签: opencv visual-c++


【解决方案1】:

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

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();

【讨论】:

  • 感谢您的回复,但我面临的问题是代码将停留在代码的这一点 if(!vcap.isOpened()) 如果相机断开连接,则不会进一步前进,问题是如果相机断开连接或不存在,vcap.isopened() 不会返回 false
  • @harounbest 怎么会卡住?该函数返回true 或false。如果它返回false,那么if 语句为真并且程序退出。如果它返回true,则继续执行while 循环,read 函数将返回空帧,这将中断循环。在任何一种情况下,执行将终止。
  • 这是我面临的问题,当相机连接时它工作正常并且 vcap.isOpened() 重新调整为“true”,但是当相机断开连接时它卡住并且不返回任何东西
  • 可能是您相机的驱动程序错误
【解决方案2】:

我解决这个问题的方法是使用线程,我在一个单独的线程中检查相机连接,如果该线程没有完成并且没有返回任何值,则等待一段时间(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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多