在之前的筆記OpenCV4學習筆記(65)中,使用OpenCV實現了一個簡單的口罩檢測,但是僅僅只是功能上的實現沒有任何的GUI界面,所以今天就結合QT,做一個簡單的小界面出來。關於功能實現部分的內容,還請參閱之前的筆記。
先說下我的QT開發環境,是使用VS下的“QT VS Tool”拓展來進行的,並且使用QT Designer進行界面控件、佈局等的操作。
這裏主要整理下QtWidgetsApplication.cpp文件中的代碼思路。
首先依然要先加載各個模型,以及設置計算後臺和目標設備
string model_path = "D:\\opencv_c++\\opencv_tutorial\\data\\models\\face_detector\\opencv_face_detector_uint8.pb";
string config_path = "D:\\opencv_c++\\opencv_tutorial\\data\\models\\face_detector\\opencv_face_detector.pbtxt";
Net face_detector = readNetFromTensorflow(model_path, config_path);
face_detector.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
face_detector.setPreferableTarget(DNN_TARGET_CPU);
auto detecModel = ml::SVM::load("face_mask_detection.xml");
然後先設置一下顯示窗口爲黑色背景
QSize label_size = ui.label->size();
Mat background = Mat::zeros(Size(label_size.width(),label_size.height()) , CV_8UC3);
QImage back = QImage((const uchar*)background.data, background.cols, background.rows, QImage::Format::Format_BGR888);
ui.label->setPixmap(QPixmap::fromImage(back.scaled(label_size, Qt::AspectRatioMode::KeepAspectRatio)));
然後設置初始按鈕狀態,只有“打開攝像頭”這個按鈕在一開始是可以點擊的
ui.pushButton2->setDisabled(true);
ui.pushButton3->setDisabled(true);
接着定義攝像頭的VideoCapture
類對象,以及進行拍攝的定時器。
static VideoCapture capture;
QTimer* timer = new QTimer;
timer->setInterval(30);
connect(timer, &QTimer::timeout, ui.label, [=]() {
if (!capture.isOpened())
{
capture.open(0);
}
Mat frame;
capture.read(frame);
QImage qframe((const uchar*)frame.data, frame.cols, frame.rows, QImage::Format::Format_BGR888);
ui.label->setPixmap(QPixmap::fromImage(qframe.scaled(label_size, Qt::KeepAspectRatio)));
});
當點擊“打開攝像頭”按鈕的時候,激活其他按鈕,同時開始拍攝
connect(ui.pushButton1, &QPushButton::clicked, timer, [=]() {
ui.pushButton1->setDisabled(true);
ui.pushButton2->setDisabled(false);
ui.pushButton3->setDisabled(false);
timer->start();
});
設置用於口罩檢測的定時器
QTimer* timerMask = new QTimer;
timerMask->setInterval(30);
connect(timerMask, &QTimer::timeout, ui.label, [=]() {
Mat frame;
capture.read(frame);
FaceMaskDetect(frame, detecModel, face_detector);
QImage qframe((const uchar*)frame.data, frame.cols, frame.rows, QImage::Format::Format_BGR888);
ui.label->setPixmap(QPixmap::fromImage(qframe.scaled(label_size, Qt::KeepAspectRatio)));
});
當按下“口罩檢測”按鈕時,開始對出現的人臉進行識別檢測,判斷有沒有戴口罩
connect(ui.pushButton3, &QPushButton::clicked, timerMask, [=]() {
ui.pushButton1->setDisabled(true);
ui.pushButton2->setDisabled(false);
ui.pushButton3->setDisabled(true);
timer->stop();
timerMask->start();
});
當按下“關閉攝像頭”按鈕時,圖像窗口顯示爲黑色背景,同時釋放攝像頭和關閉所有定時器。
connect(ui.pushButton2, &QPushButton::clicked, ui.label, [=]() {
ui.pushButton3->setDisabled(true);
ui.pushButton1->setDisabled(false);
ui.pushButton2->setDisabled(true);
if (timer->isActive())
{
timer->stop();
}
timerMask->stop();
capture.release();
Mat background = Mat::zeros(Size(ui.label->width(), ui.label->height()), CV_8UC3);
QImage back = QImage((const uchar*)background.data, background.cols, background.rows, QImage::Format::Format_BGR888);
ui.label->setPixmap(QPixmap::fromImage(back.scaled(label_size, Qt::AspectRatioMode::KeepAspectRatio)));
});
那麼實現的效果如下所示:
初始狀態
打開攝像頭
口罩檢測,沒戴口罩爲紅色標記,帶了口罩則爲綠色標記。
好的,那今天就是對之前的筆記內容聯合QT實現一個簡單的GUI界面,那本次筆記就到此結束啦。
PS:本人的註釋比較雜,既有自己的心得體會也有網上查閱資料時摘抄下的知識內容,所以如有雷同,純屬我向前輩學習的致敬,如果有前輩覺得我的筆記內容侵犯了您的知識產權,請和我聯繫,我會將涉及到的博文內容刪除,謝謝!