DianLi/DEM_GeneVec/DEM_GeneVec.cpp

426 lines
12 KiB
C++
Raw Normal View History

2023-03-13 14:13:07 +08:00
#include "DEM_GeneVec.h"
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include <QSettings>
TrainMoudle::TrainMoudle()
{
//ui.setupUi(this);
}
TrainMoudle::~TrainMoudle()
{
}
QString TrainMoudle::PannelName()
{
return QString::fromLocal8Bit("");
}
QString TrainMoudle::CategoryName()
{
return QString::fromLocal8Bit("DEMģ<EFBFBD><EFBFBD>");
}
QString TrainMoudle::EnglishName()
{
return QString::fromLocal8Bit("DEM_Module");
}
QString TrainMoudle::ChineseName()
{
return QString::fromLocal8Bit("ģ<EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD>");
}
QString TrainMoudle::Information()
{
return QString::fromLocal8Bit("ģ<EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD>");
}
QString TrainMoudle::IconPath()
{
return ":/DEM_GeneVec/resources/dem_vec.svg";
}
QWidget* TrainMoudle::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("ģ<EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD>"));
myWidget->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
myWidget->setWindowIcon(QIcon(":/DEM_GeneVec/resources/dem_vec.svg"));
myWidget->setAttribute(Qt::WA_QuitOnClose, false);
myWidget->setAttribute(Qt::WA_DeleteOnClose);
connect(myWidget, &QDialog::destroyed, this, [=] {
qDebug() << "----DEM train window close----";
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (pluginsDir.cd("srsplugins\\DemModel"))
{
QString strConfigPath = pluginsDir.absoluteFilePath("dem_config.ini");
QFile f(strConfigPath);
if (f.exists())
{
WriteConfigPaths(strConfigPath);
f.close();
}
}
if (mThread != nullptr)
{
mThread->requestInterruption();//<2F><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>ж<EFBFBD>
mThread->quit();
mThread->wait();//<2F><><EFBFBD><EFBFBD>wait<69><74><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>finished<65>źŶ<C5BA>Ӧ<EFBFBD>IJۺ<C4B2><DBBA><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mWorkObject->on_cancel();
mThread = nullptr;//m_objThread, &QThread::finished, m_objThread, &QObject::deleteLater<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫdelete
mWorkObject = nullptr;//m_objThread, &QThread::finished, m_obj, &QObject::deleteLater
}
myWidget->close();
myWidget = nullptr;
});
connect(ui.pbtInModel, &QPushButton::pressed, this, &TrainMoudle::choseInModel);
connect(ui.pbtInDom, &QPushButton::pressed, this, &TrainMoudle::choseInDom);
connect(ui.pbtInDsm, &QPushButton::pressed, this, &TrainMoudle::choseInDsm);
connect(ui.pbtInLabel, &QPushButton::pressed, this, &TrainMoudle::choseInLabel);
connect(ui.pbtOutModel, &QPushButton::pressed, this, &TrainMoudle::choseOutModel);
connect(ui.pushButton_ok, &QPushButton::pressed, this, &TrainMoudle::readAndStart);
connect(ui.pushButton_cancel, &QPushButton::pressed, this, &TrainMoudle::pbCancel);
connect(ui.checkBoxMid, &QCheckBox::clicked, this, [=](bool checked) {
executeMid = checked;
if (!executeMid && !executeTrain)
ui.pushButton_ok->setEnabled(false);
else
ui.pushButton_ok->setEnabled(true);
});
connect(ui.checkBoxTrain, &QCheckBox::clicked, this, [=](bool checked) {
executeTrain = checked;
if (!executeMid && !executeTrain)
ui.pushButton_ok->setEnabled(false);
else
ui.pushButton_ok->setEnabled(true);
});
ui.progressBar->setTextVisible(true);
ui.progressBar->setRange(0, 100);
ui.lineEpoch1->setValidator(new QIntValidator(0, 999, this));
ui.lineEpoch2->setValidator(new QIntValidator(0, 999, this));
ui.lineEpoch1->setText("100");
ui.lineEpoch2->setText("100");
ui.pbtInModel->setFocus();
QFile qssFile(":/DEM_GeneVec/DEM_GeneVec.qss");
qssFile.open(QFile::ReadOnly); //<2F><>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
if (qssFile.isOpen())
{
QString qss = QLatin1String(qssFile.readAll());
myWidget->setStyleSheet(qss);
qssFile.close();
}
else
qDebug() << "<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>";
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (pluginsDir.cd("srsplugins\\DemModel"))
{
QString strConfigPath = pluginsDir.absoluteFilePath("dem_config.ini");
QFile f(strConfigPath);
if (f.exists())
{
ReadConfigHistoryPaths(strConfigPath);
f.close();
}
}
if (showWin)
myWidget->show();
return myWidget;
}
void TrainMoudle::startWorkThread()
{
if (mWorkObject != nullptr)
{
return;
}
mThread = new QThread();
mWorkObject = new WorkThreadObject();
mWorkObject->moveToThread(mThread);
connect(mThread, &QThread::finished, mThread, &QObject::deleteLater);
connect(mThread, &QThread::finished, mWorkObject, &QObject::deleteLater);
connect(mWorkObject, &WorkThreadObject::process, myWidget, [=](double val) {ui.progressBar->SetDoubleFormatValue(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), val); });
connect(mWorkObject, &WorkThreadObject::addDataToMap, this, &TrainMoudle::addMap);
//connect(this, &TrainMoudle::killChildThread, mWorkObject, &WorkThreadObject::on_cancel);
connect(this, &TrainMoudle::start, mWorkObject, &WorkThreadObject::runTrainWork);
mThread->start();
}
//pbtDataset
//pbtValName
//pbtOutModel
void TrainMoudle::choseInModel()
{
QString inModel = QFileDialog::getOpenFileName(ui.pbtInModel, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "", "*.pth");
if (inModel != "")
ui.lineInModel->setText(inModel);
}
void TrainMoudle::choseInDom()
{
QString dataset = QFileDialog::getExistingDirectory(ui.pbtInDom, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DOM<EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (dataset != "")
ui.lineInDom->setText(dataset);
}
void TrainMoudle::choseInDsm()
{
QString valName = QFileDialog::getExistingDirectory(ui.pbtInDsm, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Slope<EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (valName != "")
ui.lineInDsm->setText(valName);
}
void TrainMoudle::choseInLabel()
{
QString valName = QFileDialog::getExistingDirectory(ui.pbtInLabel, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Label<EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (valName != "")
ui.lineInLabel->setText(valName);
}
void TrainMoudle::choseOutModel()
{
QString outModel = QFileDialog::getExistingDirectory(ui.pbtOutModel, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (outModel != "")
ui.lineOutModel->setText(outModel);
}
void TrainMoudle::ReadConfigHistoryPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[DemTrain] <20><><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>port<72>ֶε<D6B6>ֵ
configIni.beginGroup("DemTrain");
ui.lineInModel->setText(configIni.value("RetrainModel").toString());
ui.lineInDom->setText(configIni.value("SrcDom").toString());
ui.lineInDsm->setText(configIni.value("SrcDsm").toString());
ui.lineInLabel->setText(configIni.value("SrcLabel").toString());
ui.lineOutModel->setText(configIni.value("TrainResult").toString());
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
void TrainMoudle::WriteConfigPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
configIni.setIniCodec("utf-8");
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[DemTrain] <20><><EFBFBD><EFBFBD>
configIni.beginGroup("DemTrain");
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>·<EFBFBD><C2B7>
QString temp = ui.lineInModel->text();
if (temp != "")
configIni.setValue("RetrainModel", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DOM·<4D><C2B7>
temp = ui.lineInDom->text();
if (temp != "")
configIni.setValue("SrcDom", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DSM·<4D><C2B7>
temp = ui.lineInDsm->text();
if (temp != "")
configIni.setValue("SrcDsm", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Label·<6C><C2B7>
temp = ui.lineInLabel->text();
if (temp != "")
configIni.setValue("SrcLabel", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
temp = ui.lineOutModel->text();
if (temp != "")
configIni.setValue("TrainResult", temp);
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
void TrainMoudle::addMap()
{
QMessageBox mess(QMessageBox::NoIcon,
QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>\n") + ui.lineOutModel->text(),
QMessageBox::Ok, NULL);
mess.setWindowFlags(Qt::Drawer);
mess.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("ȷ<EFBFBD><EFBFBD>"));
int result = mess.exec();
//switch (result)
//{
//case QMessageBox::Ok:
// //string_list.append(ui.lineEdit_outras->text());
// //string_list.append(ui.lineEdit_outshp->text());
// //qDebug() << "----Yes:" << string_list;
// //openResultData(string_list);
// //emit addDataToCanvas(string_list);
// break;
//default:
// break;
//}
pbCancel();
}
void TrainMoudle::readAndStart()
{
QString inModel = ui.lineInModel->text();
QString inDOM = ui.lineInDom->text();
QString inDSM = ui.lineInDsm->text();
QString inLabel = ui.lineInLabel->text();
QString outModel = ui.lineOutModel->text();
ui.progressBar->SetDoubleFormatValue(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), 0);
if (inModel == "" || inDOM == "" || inDSM == "" || inLabel == "" || outModel == "")
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir inDOMdir(inDOM);
if (!inDOMdir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("DOM<EFBFBD>ļ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir inDSMdir(inDSM);
if (!inDSMdir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("Slope<EFBFBD>ļ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir inLabeldir(inLabel);
if (!inLabeldir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("Label<EFBFBD>ļ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir outModeldir(outModel);
if (!outModeldir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
if (executeMid && executeTrain)
qDebug() << "executeMid & executeTrain";
if (executeMid && (!executeTrain))
qDebug() << "executeMid, not executeTrain";
if ((!executeMid) && executeTrain)
{
//<2F>ж<EFBFBD>train<69>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵ<EFBFBD><D1B5><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
QDir dirImage(outModel + "/Images");
QDir dirLabel(outModel + "/Labels");
if (!dirImage.exists() || !dirLabel.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("δ<EFBFBD>ҵ<EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>\nȷ<EFBFBD><EFBFBD>Images<EFBFBD><EFBFBD>Labels<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: \n") + outModel);
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
//<2F>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD>ƥ<EFBFBD><C6A5>
QStringList imgList = getAllFiles(outModel + "/Images", "tif");
QStringList labelList = getAllFiles(outModel + "/Labels", "tif");
if (imgList.size() == 0 || labelList.size() == 0)
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
if (imgList.size() != labelList.size())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Images<EFBFBD><EFBFBD>Labels<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
}
if (ui.lineEpoch1->text() == "" || ui.lineEpoch2->text() == "")
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QString epoch1 = ui.lineEpoch1->text();
QString epoch2 = ui.lineEpoch2->text();
if (mThread == nullptr)
{
qDebug() << "--startThread";
startWorkThread();
}
emit start(inModel, inDOM, inDSM, inLabel, outModel, executeMid, executeTrain, epoch1, epoch2);
}
QStringList TrainMoudle::getAllFiles(QString path, QString fileType)
{
QDir dir(path);
if (!dir.exists())
return QStringList();
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
int file_count = list.count();
if (file_count <= 0)
return QStringList();
QStringList files;
for (int i = 0; i < file_count; i++)
{
QFileInfo file_info = list.at(i);
QString suffix = file_info.suffix();
if (QString::compare(suffix, QString(fileType), Qt::CaseInsensitive) == 0)
{
QString absolute_file_path = file_info.absoluteFilePath();
files.append(absolute_file_path);
}
}
return files;
}
void TrainMoudle::pbCancel()
{
delete myWidget;//<2F><><EFBFBD><EFBFBD>&QDialog::destroyed
}