You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.1 KiB
C++

#include "ModelTest.h"
#include <QGridLayout>
#include <QDir>
#include <QFileInfo>
#include <QDirIterator>
#include <QFileDialog>
#pragma execution_character_set("utf-8")
ModelTest::ModelTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
{
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 = "H:\\<EFBFBD>½<EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>\\Source\\";
//QString strPath = "H:\\<5C>½<EFBFBD><C2BD>ļ<EFBFBD><C4BC><EFBFBD>\\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);
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>();
QImage pixResult = vMap.value("ResImage").value<QImage>();
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));
}