#include "ModelTest.h" #include #include #include #include #include #include "saveimgthread.h" #include "QDateTime" #pragma execution_character_set("utf-8") ModelTest::ModelTest(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); strRootPath = QApplication::applicationDirPath(); { QGridLayout *pLayout = new QGridLayout(ui.widget_src); m_srcView = new lpImgViewer(ui.widget_src); pLayout->addWidget(m_srcView); ui.widget_src->setLayout(pLayout); } { QGridLayout *pLayout = new QGridLayout(ui.widget_rlt); m_rltView = new lpImgViewer(ui.widget_rlt); pLayout->addWidget(m_rltView); ui.widget_rlt->setLayout(pLayout); } QString strPath = "E:\\wheelValve\\2019-05-22\\"; //QString strPath = "H:\\新建文件夹\\7\\"; loadImage(strPath); QString strRootPath = QApplication::applicationDirPath(); m_pModelMgr = new ModelManager(strRootPath); m_pModelMgrUI = new QModelMgrDlg(m_pModelMgr); m_pModelMgr->init(); connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onButtonClicked())); connect(ui.m_pbSelect, SIGNAL(clicked()), this, SLOT(onButtonClicked())); connect(ui.m_pbNext, SIGNAL(clicked()), this, SLOT(onButtonClicked())); m_pAlg = new QAlgDetect(); } ModelTest::~ModelTest() { if (m_pModelMgr) { delete m_pModelMgr; m_pModelMgr = nullptr; } if (m_pModelMgrUI) { delete m_pModelMgrUI; m_pModelMgrUI = nullptr; } if (m_rltView) { delete m_rltView; m_rltView = nullptr; } if (m_srcView) { delete m_srcView; m_srcView = nullptr; } if (m_pAlg) { delete m_pAlg; m_pAlg = nullptr; } } Q_SLOT void ModelTest::onButtonClicked() { QString strObj = sender()->objectName(); if (strObj == "pushButton") { m_pModelMgrUI->show(); } else if (strObj == "m_pbNext") { if (m_filelst.size() <= 0) return; if (m_gIndex >= m_filelst.size()) m_gIndex = 0; QString strPath = m_filelst.at(m_gIndex); ui.label_filePath->setText(strPath); m_gIndex++; QImage img; bool b = img.load(strPath); if (b == true) { m_srcView->setImg(img); } AlgResultCallBack func = std::bind(&ModelTest::AlgResultCallFunc, this, std::placeholders::_1); QVariantMap param; void* address = (void*)m_pModelMgr->getAllModelMapPtr(); long long varadr = (long long)address; param.insert("modelMap", varadr); param.insert("equal",false); m_pAlg->detect(img, param, func); } else if ("m_pbSelect" == strObj) { QString dirPath = QFileDialog::getExistingDirectory(this, "", ""); if (dirPath.isEmpty()) return; loadImage(dirPath); } } void ModelTest::loadImage(QString strPath) { m_filelst.clear(); m_gIndex = 0; QStringList fileFilter; fileFilter << "*.BMP" << "*.JPEG" << "*.JPG" << "*.PNG"; QDirIterator qdi(strPath, fileFilter, QDir::NoSymLinks | QDir::Files, QDirIterator::Subdirectories); while (qdi.hasNext()) { m_filelst.append(qdi.next()); } } void ModelTest::AlgResultCallFunc(QVariantMap vMap) { QImage srcImg = vMap.value("SrcImage").value(); QImage pixResult = vMap.value("ResImage").value(); QString strName = vMap.value("modeName").toString(); double dMinDis = vMap.value("minDis").toDouble(); double dScore = vMap.value("score").toDouble(); double dRunTime = vMap.value("runTime").toDouble(); m_rltView->setImg(pixResult); if (strName != "NG") { IWheelModel *pModel = m_pModelMgr->getModel(strName); if (pModel) { QString filepath = QApplication::applicationDirPath() + pModel->getPicPath(); QPixmap pix(filepath); ui.label_pic->setPixmap(pix.scaled(128 - 15, 128 - 15)); ui.label_Result->setText(strName); ui.label_Result->setStyleSheet("font: 75 24pt \"Consolas\";background-color: rgb(0, 255, 0);"); ui.label_pic->setStyleSheet(QString("QLabel{border: 5px solid rgb(0,250,0,250);background-color: rgb(200, 200, 200);}")); } } else { QPixmap pix(":/Resources/timg (1).jpg"); ui.label_pic->setPixmap(pix.scaled(QSize(128, 128))); ui.label_pic->setStyleSheet(QString("QLabel{border: 5px solid rgb(250,0,0,250);background-color: rgb(200, 200, 200);}")); ui.label_Result->setText("NG"); ui.label_Result->setStyleSheet("font: 75 24pt \"Consolas\";background-color: rgb(255, 0, 0);"); } ui.label_time->setText(QString("%1").arg(dRunTime)); ui.label_score->setText(QString("%1").arg(dMinDis)); saveResult(strName, pixResult); } void ModelTest::saveImage(const QPixmap& m_pixmap, QString m_path, QString filename) {//启动多线程保存图像 SaveImgThread *workTread = new SaveImgThread(this); workTread->setPixmap(m_pixmap, m_path, filename); connect(workTread, &SaveImgThread::finished, workTread, &QObject::deleteLater); workTread->start(); } void ModelTest::saveResult(QString strName, QImage img) { QDir testDir; QString baseSavePath = strRootPath + "\\Data\\"; bool IsTestDir = testDir.exists(baseSavePath); if (!IsTestDir) testDir.mkpath(baseSavePath); QString dataTime = QDateTime::currentDateTime().toString("yyyy-MM-dd"); QString fileSavePath = baseSavePath + "\\" + dataTime; QString filename = strName; if (strName != "NG") { QString goodsourceDir = fileSavePath + "\\Good"; filename += QDateTime::currentDateTime().toString("hhmmsszzz"); goodsourceDir += "\\"; goodsourceDir += strName; QString resultpath = goodsourceDir + "\\" + filename + ".png"; saveImage(QPixmap::fromImage(img), goodsourceDir + "\\", filename + ".png"); } else { QString errorSourceDir = fileSavePath + "\\Error"; filename += QDateTime::currentDateTime().toString("hhmmsszzz"); QString resultpath = errorSourceDir + "\\" + filename + ".png"; saveImage(QPixmap::fromImage(img), errorSourceDir + "\\", filename + ".png"); } }