opencv - 当 IP 摄像机断开连接时,视频捕获卡住
问题描述
我正在使用 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秒),如果该线程没有完成并且没有返回任何值,这意味着相机没有已连接。这是代码
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;
}
推荐阅读
- ios - 如何在 Xcode 12(.5) 中设置基本本地化和开发语言?
- python - 访问类的值
- python - 如何将两个方程的最大值输入熊猫数据框列?
- r - r中的未知激活函数
- sql - 合并来自多个表 SQL server 的数据
- linux - 带有 bcompare 的 Git difftool 目录
- python - 如何修复 urllib.error.HTTPError:HTTP 错误 429:请求过多
- typescript - NestJS中与单表继承的多个孩子的关系
- c# - AWS API Gateway/Lambda 使用外部 API 如何检索数据并将其存储到 dynamoDb
- c++ - 为什么带有初始化程序的 C++17 if 语句不能按预期工作?