Opencv IP摄像头断开时视频捕获卡滞
我使用的是opencv 2.4.9 mvsc++2012和Qt,连接相机时代码工作正常,但如果相机断开连接,代码会卡在vcap.open(VideoStreamAddress)上 这是我的密码Opencv IP摄像头断开时视频捕获卡滞,opencv,visual-c++,Opencv,Visual C++,我使用的是opencv 2.4.9 mvsc++2012和Qt,连接相机时代码工作正常,但如果相机断开连接,代码会卡在vcap.open(VideoStreamAddress)上 这是我的密码 const string videoStreamAddress="rtsp://admin:admin@192.168.1.11:88/live/h264/VGA"; VideoCapture vcap; Mat image_input; cameraOpen=true; //first open th
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();
}
感谢您在advanced中的帮助 根据您的评论,使用以下模板访问您的相机
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();
视频捕获vcap(“url”);
如果(!vcap.isOpened())
{
std::cout我解决这个问题的方法是使用线程,我在一个单独的线程中检查摄像头连接并等待一段时间(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;
}
为什么每次迭代都要初始化相机?更改两行:
while((vcap.isOpened()){}
和if(vcap.isOpened()==false){}
。我尝试了vcap.isOpened()也不起作用,对于if(vcap.isOpened()==false){}是为了知道while循环退出的原因(由用户执行)(if(vcap.open(videoStreamAddress)==false))或者因为相机处于脱机状态。感谢您的回复,但我面临的问题是,如果(!vcap.isOpened())断开连接,代码将停留在代码的这一点上,并且如果相机断开连接,代码将不会进一步前进,问题是vcap.isOpened()如果相机断开或未连接,请勿返回falseexist@harounbest它怎么会被卡住?函数要么返回true
要么返回false
。如果返回false
,则If
语句为true,程序退出。如果返回true
,则继续执行,同时
循环dread
函数将返回空帧,这将中断循环。在任何情况下,执行都将终止。这就是我面临的问题,当相机连接时,它工作正常,vcap.isOpened()重新运行“true”,但当相机断开连接时,它会卡住,不会返回任何东西可能是相机的驱动程序错误