DianLi/DEM_GeneVec/ThreadObject.cpp
2023-03-13 14:13:07 +08:00

97 lines
2.5 KiB
C++

#include <QProcess>
#include <QDir>
#include <QApplication>
#include <QDebug>
#include <QTimer>
#include <windows.h>
#include "DEM_GeneVec.h"
#include "ThreadObject.h"
using namespace std;
WorkThreadObject::WorkThreadObject(QObject* parent) :QObject(parent)
{
qRegisterMetaType<std::function<void(double)> >("std::function<void(double)>");
}
WorkThreadObject::~WorkThreadObject()
{
}
void WorkThreadObject::runTrainWork(QString inModel, QString dom, QString dsm, QString label, QString outModel, bool mid, bool train, QString epoch1, QString epoch2)
{
QString str = QString("%1->%2,thread id:%3").arg(__FILE__).arg(__FUNCTION__).arg((int)QThread::currentThreadId());
qDebug() << str;
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (!pluginsDir.cd("models\\envs"))
{
qDebug() << "no folder models\\envs";
return;
}
QString exeDirName = pluginsDir.absoluteFilePath("train_4c_vegetaion.exe");
QString inDom = " --dom_path "+ dom + "/";
QString inDsm = " --dsm_path " + dsm + "/";
QString inLabel = " --label_path " + label + "/";
QString trainedModel = " --retrained_model " + inModel;
QString save_model = " --save_model " + outModel + "/";
QString strMid, strTrain;
if (mid)
strMid = " --exe_mid True ";
else
strMid = " --exe_mid False ";
if (train)
strTrain = " --exe_train True";
else
strTrain = " --exe_train False";
QString strEpoch1, strEpoch2;
strEpoch1 = " --epoch1 " + epoch1;
strEpoch2 = " --epoch2 " + epoch2;
//QString CUDA = " --CUDA False";
QString ss = exeDirName + inDom + inDsm + inLabel + trainedModel + save_model+ strMid + strTrain + strEpoch1 + strEpoch2;// + CUDA
qDebug() << ss;
QProcess* pProces = new QProcess(this);
connect(pProces, SIGNAL(readyReadStandardOutput()), this, SLOT(on_read()));
pProces->start(ss);
}
void WorkThreadObject::on_read()
{
mProces = (QProcess*)sender();
QString output = QString::fromLocal8Bit(mProces->readAllStandardOutput());
//qDebug() << "out:" << output;
if (output.toDouble() > 0)
{
qDebug() << "exe out:" << output.toDouble();
emit process(output.toDouble());
if (output.toDouble() == 100.0)
{
emit addDataToMap();
}
}
else
qDebug() << "Unresolved exe out:" << output;
}
void WorkThreadObject::on_cancel()
{
QString str = QString("--1.%1->%2,thread id:%3").arg(__FILE__).arg(__FUNCTION__).arg((int)QThread::currentThreadId());
qDebug() << str;
if (mProces == nullptr)
{
qDebug() << "--mProces null";
}
else
{
QString KillStr = "taskkill /f /im train_4c_vegetaion.exe";
mProces->startDetached(KillStr);
qDebug() << "--kill Proces";
}
}