mainwindow.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma execution_character_set("utf-8")
  2. #include "mainwindow.h"
  3. #include "ui_mainwindow.h"
  4. #include "grabframethread.h"
  5. #include <QCameraInfo>
  6. #include <QThread>
  7. #include <QTimer>
  8. #include <QCursor>
  9. MainWindow::MainWindow(QWidget *parent) :
  10. QMainWindow(parent),
  11. ui(new Ui::MainWindow)
  12. {
  13. ui->setupUi(this);
  14. ui->label_ShowImage->setBackgroundRole(QPalette::Dark);
  15. on_action_FindCamera_triggered();
  16. initParm();
  17. }
  18. MainWindow::~MainWindow()
  19. {
  20. delete ui;
  21. }
  22. void MainWindow::closeEvent(QCloseEvent *event)
  23. {
  24. if(m_thread->isRunning()){
  25. m_grabFrameThread->closeCamera();
  26. m_thread->quit();
  27. m_thread->wait();
  28. }
  29. }
  30. void MainWindow::on_action_FindCamera_triggered()
  31. {
  32. ui->comboBox_CameraList->clear();
  33. m_cameraList.clear();
  34. m_cameraList = QCameraInfo::availableCameras();
  35. for(int i=0;i<m_cameraList.size();++i){
  36. ui->comboBox_CameraList->addItem(m_cameraList.at(i).description());
  37. }
  38. }
  39. void MainWindow::on_action_OpenCamera_triggered()
  40. {
  41. if(ui->action_OpenCamera->text() == tr("打开摄像头")){
  42. if(ui->comboBox_CameraList->count() > 0){
  43. if(!m_timerShowFrame->isActive())
  44. m_timerShowFrame->start();
  45. ui->action_OpenCamera->setText(tr("关闭摄像头"));
  46. // 打开相机
  47. m_grabFrameThread->openCamera(ui->comboBox_CameraList->currentIndex());
  48. // 设置分辨率
  49. on_pushButton_SetResolution_clicked();
  50. //设置检测参数
  51. on_pushButton_SetParam_clicked();
  52. //设置计算精度
  53. on_checkBox_fp16_clicked();
  54. on_checkBox_int8_clicked();
  55. QApplication::setOverrideCursor(Qt::WaitCursor);
  56. } else {
  57. qDebug() << tr("当前设备没有摄像头");
  58. }
  59. } else {
  60. ui->action_OpenCamera->setText(tr("打开摄像头"));
  61. m_grabFrameThread->closeCamera();
  62. m_timerShowFrame->stop();
  63. ui->label_ShowImage->clear();
  64. // Destroy the engine
  65. m_grabFrameThread->destroyEngine();
  66. }
  67. }
  68. void MainWindow::refreshFrame(const QImage &image)
  69. {
  70. if(Qt::ArrowCursor != QApplication::overrideCursor()->shape())
  71. QApplication::setOverrideCursor(Qt::ArrowCursor);
  72. ui->label_ShowImage->setPixmap(QPixmap::fromImage(image));
  73. // ui->label_ShowImage->adjustSize();
  74. }
  75. void MainWindow::initParm()
  76. {
  77. m_thread = new QThread;
  78. m_grabFrameThread = new GrabFrameThread;
  79. m_timerShowFrame = new QTimer(this);
  80. m_grabFrameThread->moveToThread(m_thread);
  81. connect(m_thread,&QThread::finished,m_thread,&QThread::deleteLater);
  82. connect(m_thread,&QThread::finished,m_grabFrameThread,&GrabFrameThread::deleteLater);
  83. connect(m_thread,&QThread::finished,m_grabFrameThread,&GrabFrameThread::init);
  84. connect(m_timerShowFrame,&QTimer::timeout,m_grabFrameThread,&GrabFrameThread::refreshFrame);
  85. connect(m_grabFrameThread,&GrabFrameThread::signal_refreshFrame,this,&MainWindow::refreshFrame);
  86. m_timerShowFrame->setInterval(33);
  87. m_thread->start();
  88. }
  89. void MainWindow::on_pushButton_SetResolution_clicked()
  90. {
  91. ui->spinBox_FrameWidth->interpretText();
  92. ui->spinBox_FrameHeight->interpretText();
  93. const int frameWidth = ui->spinBox_FrameWidth->value();
  94. const int frameHeight = ui->spinBox_FrameHeight->value();
  95. m_grabFrameThread->setFrameResolution(frameWidth,frameHeight);
  96. }
  97. void MainWindow::on_pushButton_SetParam_clicked()
  98. {
  99. ui->doubleSpinBox_conf->interpretText();
  100. ui->doubleSpinBox_nms->interpretText();
  101. const float conf_threshold = ui->doubleSpinBox_conf->value();
  102. const float nms_threshold = ui->doubleSpinBox_nms->value();
  103. m_grabFrameThread->setParameter(conf_threshold,nms_threshold);
  104. }
  105. void MainWindow::on_pushButton_StartDetec_clicked()
  106. {
  107. m_grabFrameThread->startDetect();
  108. }
  109. void MainWindow::on_pushButton_EndDetec_clicked()
  110. {
  111. m_grabFrameThread->closeDetect();
  112. }
  113. void MainWindow::on_checkBox_fp16_clicked()
  114. {
  115. m_grabFrameThread->setfp16(ui->checkBox_fp16->isChecked());
  116. }
  117. void MainWindow::on_checkBox_int8_clicked()
  118. {
  119. m_grabFrameThread->setint8(ui->checkBox_int8->isChecked());
  120. }