DianLi/LandslideAssess/LandslideAssess.cpp
2023-03-13 16:16:25 +08:00

379 lines
11 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "LandslideAssess.h"
#include <QSettings>
LandslideAssess::LandslideAssess()
{
//ui.setupUi(this);
}
QString LandslideAssess::PannelName()
{
return QString::fromLocal8Bit("華窐耀輸");
}
QString LandslideAssess::CategoryName()
{
return QString::fromLocal8Bit("華窐耀輸");
}
QString LandslideAssess::EnglishName()
{
return QString::fromLocal8Bit("LandslideAssess");
}
QString LandslideAssess::ChineseName()
{
return QString::fromLocal8Bit("賦彆丿嘛");
}
QString LandslideAssess::Information()
{
return QString::fromLocal8Bit("賦彆丿嘛");
}
QString LandslideAssess::IconPath()
{
return QString(":/LandslideAssess/resources/assessment.svg");
}
QWidget* LandslideAssess::CenterWidget()
{
QString gdal_path = qApp->applicationDirPath().toLocal8Bit() + "/share/gdal";
QString pro_lib_path = qApp->applicationDirPath().toLocal8Bit() + "/share/proj";
qputenv("GDAL_DATA", gdal_path.toLocal8Bit());
qputenv("PROJ_LIB", pro_lib_path.toLocal8Bit());
bool showWin = false;
if (myWidget == nullptr)
{
myWidget = new QDialog();
showWin = true;
qDebug() << "new QDialog()";
}
else
{
qDebug() << "already have myWidget";
//慾魂敦諳甜枑汔祫階脯
myWidget->activateWindow();
myWidget->raise();
return myWidget;
}
ui.setupUi(myWidget);
myWidget->setWindowTitle(QString::fromLocal8Bit("賦彆丿嘛"));
myWidget->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
myWidget->setWindowIcon(QIcon(":/LandslideAssess/resources/assessment.svg"));
myWidget->setAttribute(Qt::WA_QuitOnClose, false);
myWidget->setAttribute(Qt::WA_DeleteOnClose);
connect(myWidget, &QDialog::destroyed, this, [=] {
qDebug() << "----Landslide assess window close----";
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (pluginsDir.cd("srsplugins\\SldModel"))
{
QString strConfigPath = pluginsDir.absoluteFilePath("sld_config.ini");
QFile f(strConfigPath);
if (f.exists())
{
WriteConfigPaths(strConfigPath);
f.close();
}
}
if (mWorkThread != nullptr)
{
mWorkThread->requestInterruption();//ワА盄最笢剿
mWorkThread->quit();
mWorkThread->wait();//覃蚚wait綴珂覃蚚finished陓瘍勤茼腔羞滲杅ㄛ硒俴俇傖綴婬厘狟軗
mWorker->on_cancel();
mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLaterㄛ祥剒猁delete
mWorker = nullptr;//mWorkThread, &QThread::finished, mWorker, &QObject::deleteLater
}
//qDebug() << "--destroyed";
myWidget->close();
myWidget = nullptr;
});
connect(ui.pbtInDataset, &QPushButton::clicked, this, &LandslideAssess::choosePredictPath);
connect(ui.pbtInLabel, &QPushButton::clicked, this, &LandslideAssess::chooseLabelPath);
connect(ui.pbtIndex, &QPushButton::clicked, this, &LandslideAssess::chooseAssessFile);
connect(ui.pbtInModel, &QPushButton::clicked, this, &LandslideAssess::chooseModelFile);
connect(ui.pbtResult, &QPushButton::clicked, this, &LandslideAssess::chooseResultPath);
connect(ui.pushButton_ok, &QPushButton::clicked, this, &LandslideAssess::readAndStart);
connect(ui.pushButton_cancel, &QPushButton::clicked, this, &LandslideAssess::pbCancel);
ui.progressBar->setTextVisible(true);
ui.progressBar->setRange(0, 100);
ui.pbtInModel->setFocus();
QFile qssFile(":/LandslideAssess/LandslideAssess.qss");
qssFile.open(QFile::ReadOnly); //眕硐黍源宒湖羲
if (qssFile.isOpen())
{
QString qss = QLatin1String(qssFile.readAll());
myWidget->setStyleSheet(qss);
qssFile.close();
}
else
qDebug() << "no qssFile";
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (pluginsDir.cd("srsplugins\\SldModel"))
{
QString strConfigPath = pluginsDir.absoluteFilePath("sld_config.ini");
QFile f(strConfigPath);
if (f.exists())
{
ReadConfigHistoryPaths(strConfigPath);
f.close();
}
}
if (showWin)
myWidget->show();
return myWidget;
}
void LandslideAssess::startWorkThread()
{
if (mWorker != nullptr)
{
return;
}
mWorkThread = new QThread();
mWorker = new WorkObject();
mWorker->moveToThread(mWorkThread);
connect(mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater);
connect(mWorkThread, &QThread::finished, mWorker, &QObject::deleteLater);
connect(mWorker, &WorkObject::progress, myWidget, [=](int value) {
ui.progressBar->setValue(value);
if (value == 100)
{
QString txtDir = ui.lineResult->text() + "/assessment.txt";
QFile file(txtDir);
QString outLines = "";
if (file.open(QIODevice::ReadOnly))
{
QTextStream stream(&file);
// recall 欸隙薹
// precision 儕<>
// mIoU ⅸ歙蝠甜掀
// mAP ⅸ歙儕僅歙硉
// f1_score
while (!stream.atEnd())
{
QString line = stream.readLine();
//strs.push_back(line);
if (line.contains("===>mIoU"))
{
//===>mIoU:68.45; mAP: 74.92
line.remove("===>");
line.replace(QStringLiteral("mAP"), QStringLiteral("ⅸ歙儕僅歙硉mAP"));
line.replace(QStringLiteral("mIoU"), QStringLiteral("ⅸ歙蝠甜掀mIoU"));
outLines = outLines + line + "\n";
}
else if (line.contains("===>recall"))
{
//===>recall:66.62; precision: 51.2
line.remove("===>");
line.replace(QStringLiteral("recall"), QStringLiteral("欸隙薹recall"));
line.replace(QStringLiteral("precision"), QStringLiteral("<EFBFBD>薹precision"));
outLines = outLines + line + "\n";
}
else if (line.contains("===>f1_score"))
{
//===>f1_score:57.9
line.remove("===>");
line.replace(QStringLiteral("f1_score"), QStringLiteral("F1煦杅f1_score"));
outLines = outLines + line;
}
}
file.close();
}
QMessageBox mess(QMessageBox::Information,
QString::fromLocal8Bit("丿嘛賦旰"),
QString::fromLocal8Bit("丿歎賦彆:\n") + txtDir + "\n"
+ outLines);
mess.setWindowFlags(Qt::Drawer);
mess.setStandardButtons(QMessageBox::Yes);
mess.button(QMessageBox::StandardButton::Yes)->setText(QString::fromLocal8Bit("<EFBFBD>"));
int result = mess.exec();
pbCancel();
}
});
connect(this, &LandslideAssess::startPreAssessment, mWorker, &WorkObject::runAssessWork);
mWorkThread->start();
}
void LandslideAssess::ReadConfigHistoryPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
//湖羲梓枙峈ㄩ[DemTrain] 腔郪ㄛ黍龰DemTrain怀堤賦彆TrainResult趼僇
configIni.beginGroup("SldTrain");
QString strDemTrainResult = configIni.value("TrainResult").toString();
QDir srcDir = QDir(strDemTrainResult);
//載陔丿歎恅璃蹈桶
if (srcDir.absoluteFilePath("val_list.txt") != "")
ui.lineIndex->setText(srcDir.absoluteFilePath("val_list.txt"));
//載陔怀⻌Images
srcDir = QDir(strDemTrainResult);
if (srcDir.cd("Images"))
ui.lineInDataset->setText(srcDir.absolutePath());
//載陔怀⻌Labels
srcDir = QDir(strDemTrainResult);
if (srcDir.cd("Labels"))
ui.lineInLabel->setText(srcDir.absolutePath());
configIni.endGroup();//壽敕郪
//湖羲梓枙峈ㄩ[DemAssess] 腔郪ㄛ黍龰AssessResult趼僇
configIni.beginGroup("SldAssess");
//載陔丿歎賦彆怀堤
ui.lineResult->setText(configIni.value("AssessResult").toString());
configIni.endGroup();//壽敕郪
}
void LandslideAssess::WriteConfigPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
configIni.setIniCodec("utf-8");
//湖羲梓枙峈ㄩ[DemAssess] 腔郪
configIni.beginGroup("SldAssess");
//載陔怀⻌耀倰繚噤
QString temp = ui.lineResult->text();
if (temp != "")
configIni.setValue("AssessResult", temp);
configIni.endGroup();//壽敕郪
}
void LandslideAssess::readAndStart()
{
QString dirModel = ui.lineInModel->text();
QString dirIndex = ui.lineIndex->text();
QString dirLabel = ui.lineInLabel->text();
QString dirPredict = ui.lineInDataset->text();
QString dirResult = ui.lineResult->text();
ui.progressBar->setValue(0);
if (dirLabel == "" || dirPredict == "" || dirIndex == "" || dirModel == "" || dirResult == "")
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"), QString::fromLocal8Bit("ワ潰脤怀⻌繚噤"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
if (mWorkThread == nullptr)
{
qDebug() << "--startThread";
startWorkThread();
}
emit startPreAssessment(dirModel, dirPredict, dirLabel, dirIndex, dirResult);
}
void LandslideAssess::pbCancel()
{
qDebug() << "--pbtCancel";
delete myWidget;//覃お&QDialog::destroyed
}
void LandslideAssess::choosePredictPath()
{
QString dirInDataset = QFileDialog::getExistingDirectory(ui.pbtInDataset, QString::fromLocal8Bit("恁寁怀⻌丿歎恅璃繚噤"), "");
if (dirInDataset != "")
ui.lineInDataset->setText(dirInDataset);
}
void LandslideAssess::chooseLabelPath()
{
QString dirDom = QFileDialog::getExistingDirectory(ui.pbtInLabel, QString::fromLocal8Bit("恁寁怀⻌梓キ恅璃繚噤"), "");
if (dirDom != "")
ui.lineInLabel->setText(dirDom);
}
void LandslideAssess::chooseAssessFile()
{
QString dirDom = QFileDialog::getOpenFileName(ui.pbtIndex, QString::fromLocal8Bit("恁寁怀⻌丿嘛恅璃坰竘"), "", "*.txt");
if (dirDom != "")
ui.lineIndex->setText(dirDom);
}
void LandslideAssess::chooseModelFile()
{
QString fileModel = QFileDialog::getOpenFileName(ui.pbtInModel, QString::fromLocal8Bit("恁寁怀⻌耀倰恅璃"), "", "*.pth");
if (fileModel != "")
ui.lineInModel->setText(fileModel);
}
void LandslideAssess::chooseResultPath()
{
QString dirResult = QFileDialog::getExistingDirectory(ui.pbtResult, QString::fromLocal8Bit("恁寁丿嘛賦彆怀堤繚噤"), "");
if (dirResult != "")
ui.lineResult->setText(dirResult);
}
void WorkObject::runAssessWork(QString model, QString PRE, QString label, QString index, QString result)
{
qDebug() << "start work:" << "\npredict: " << PRE << "\nLabel:" << label;
QFileInfo file(index);
file.absolutePath();
assess_txt_path = file.absolutePath() + "/assessment.txt";
QDir pluginsDir = QDir(qApp->applicationDirPath());
qDebug() << "----" << pluginsDir.currentPath();
if (!pluginsDir.cd("models\\envs"))
{
qDebug() << "no folder models\\envs";
return;
}
QString exeDirName = pluginsDir.absoluteFilePath("miou_landslide.exe");
QString model_dir = " --model_dir " + model;
QString img_data_dir = " --img_data_dir " + PRE + "/";
QString label_dir = " --label_dir " + label + "/";
QString val_list = " --val_list " + index;
QString out_result = " --result_file " + result + "/";
QString ss = exeDirName + model_dir + img_data_dir + label_dir + val_list + out_result;
qDebug() << ss;
QProcess* pProces = new QProcess(this);
connect(pProces, SIGNAL(readyReadStandardOutput()), this, SLOT(writeAssessTxt()));
pProces->start(ss);
emit progress(2);
}
void WorkObject::writeAssessTxt()
{
mProcess = (QProcess*)sender();
QString output = QString::fromLocal8Bit(mProcess->readAllStandardOutput());
output.chop(2);
if (output.toFloat() != 0)
{
qDebug() << "process:" << output.toFloat();
emit progress(output.toFloat());
}
else
qDebug() << "unsolved exe out:" << output;
}
void WorkObject::on_cancel()
{
if (mProcess == nullptr)
qDebug() << "--mProces null";
else
{
QString KillStr = "taskkill /f /im miou_landslide.exe";
mProcess->startDetached(KillStr);
qDebug() << "--kill Proces";
}
}