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.
whellvalue/tpvs17/tpMain/QModelMgrDlg.cpp

1020 lines
34 KiB
C++

5 years ago
#include "QModelMgrDlg.h"
#include "ProgressView.h"
#include <QFileInfo>
#include <QDir>
#include "qpulpewidget.h"
#include "HubBase.h"
#include "IWheelModel.h"
#include "TempImage.h"
#include "qaddmodel.h"
#include "qmodnamedlg.h"
#include <vector>
#include "QSettings.h"
#include "ICompareModel.h"
#include <qevent.h>
#define WS_PICSIZE 129 //!>缩放大小
#define WS_PICSIZELIST_ITEM 100
#define MODEL_UI_ICON_NONE ":/image/none.jpg"
#define MODEL_UI_ICON_LEAPER ":/image/leaper"
enum EM_TYPE_PROCESSMODEL
{
emPross_Init = 0,
emPross_SetValue,
emPross_SetLable,
emPross_Finish,
emPross_enable,
emPross_Disable
};
#pragma execution_character_set("utf-8")
using namespace std;
/*模板管理*/
QTime m_ticktimer;
QModelMgrDlg::QModelMgrDlg(IWheelCtrl *ptr, QWidget *parent)
: QWidget(parent),m_pCtrl(ptr)
{
ui.setupUi(this);
setWindowIcon(QIcon(":/image/leaper"));
m_bTrainCancel = false;
connect(ui.m_pbAdd, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbDel, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbMod, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbModName, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbTrain, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbTrainAll, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbAddPic, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbDelAll, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
m_pShowImgList = ui.listWidget;
m_pModelMgr = m_pCtrl->getModelMgr();
m_trainAllTsk = new QMyThread();
m_trainAllTsk->setUser(true);
TaskFunc trainAllFunc = std::bind(&QModelMgrDlg::TrainAllTskFunc, this);
m_trainAllTsk->loadfunc(trainAllFunc);
m_trainOneTsk = new QMyThread();
m_trainOneTsk->setUser(true);
TaskFunc trainOneFunc = std::bind(&QModelMgrDlg::TrainOneTskFunc, this);
m_trainOneTsk->loadfunc(trainOneFunc);
connect(this, SIGNAL(sgPrograssShow(QString, QString, int, int)), this, SLOT(onPrograssShow(QString, QString, int, int)));
connect(this, SIGNAL(sgShowMsg(QString)), this, SLOT(onShowMessage(QString)));
{
m_pModelLists = new ModelsView(ui.ModelMgr_Models_tableView, m_pModelMgr->getAllModelMapPtr());
m_pModelLists->setHideItems(QStringList() << "NG");
connect(m_pModelLists, SIGNAL(sgSelectModel(QString)), this, SLOT(onShowModelInfo(QString)));
connect(m_pModelLists, SIGNAL(sgSelectModel(QString)), this, SLOT(onShowModelPic(QString)));
5 years ago
connect(this, SIGNAL(sgTrainShowInfo(QString)), this, SLOT(onShowModelInfo(QString)));
connect(m_pShowImgList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(onItemDoubleClicked(QListWidgetItem*)));
QString strApp = QApplication::applicationDirPath();
QSettings setting(strApp + "\\user\\systemfile.ini", QSettings::IniFormat);
nGlobalMinImgs = setting.value("/MinImgs/MinNum", 10).toInt();
}
{
connect(ui.ModelMgr_horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
//connect(tabwidget, SIGNAL(currentChanged(int)), this, SLOT(onTableWidget(int)));
connect(ui.ModelMgr_find_lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
QRegExp regExpNum("((6553[0-5])|[655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{3}|[1-9][0-9]{2}|[1-9][0-9]|[0-9])");
ui.ModelMgr_model_edit_height->setValidator(new QRegExpValidator(regExpNum, this));
ui.ModelMgr_model_edit_diameter->setValidator(new QRegExpValidator(regExpNum, this));
}
ui.m_pbDelAll->setVisible(false);
}
QModelMgrDlg::~QModelMgrDlg()
{
for (int nIndex = (m_pShowImgList->count() - 1); nIndex >= 0; nIndex--) {
QListWidgetItem *pItem = m_pShowImgList->takeItem(nIndex);
delete pItem;
}
ui.ModelMgr_find_lineEdit->clear();
if (m_trainAllTsk) {
if (m_trainAllTsk->isRunning())
{
m_trainAllTsk->stop();
m_trainAllTsk->quit();
m_trainAllTsk->wait();
}
delete m_trainAllTsk;
m_trainAllTsk = NULL;
}
if (m_trainOneTsk) {
if (m_trainOneTsk->isRunning())
{
m_trainOneTsk->stop();
}
delete m_trainOneTsk;
m_trainOneTsk = NULL;
}
if (m_pProgressView) {
delete m_pProgressView;
m_pProgressView = NULL;
}
if (m_pModelLists) {
delete m_pModelLists;
m_pModelLists = NULL;
}
}
void QModelMgrDlg::onSetCtrl(IWheelCtrl* ptr)
{
m_pCtrl = ptr;
}
Q_SLOT void QModelMgrDlg::onAddPic2Model()
{
QString strModel = m_pShowImgList->property("model").toString();
if (strModel.isEmpty()) {
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要追加图像的型号!"), 0);
return;
}
if (!m_pModelMgr->getAllModelName().contains(strModel))
{
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要追加图像的型号!"), 0);
return;
}
QStringList lstFiles = FileOper::selectFiles(QString("%1 Add Picture").arg(strModel), "Picture(*.bmp *.jpg *.png)", m_strAddPicPath);// m_pCtrl->appRoot() + "//Data//");
if (lstFiles.empty()) {
return;
}
m_strAddPicPath = lstFiles.at(0);
int size = lstFiles.size();
int nSuc = 0;
int oldImgsCnt = m_pModelMgr->getModel(strModel)->getTempImage()->getImgCount();
ProgressView progress(tr("加载图像"),
tr("图像加载中,请稍后....."), size, MODEL_UI_ICON_LEAPER);
for (int nIndex = 0; nIndex < size; ++nIndex) {
QString filepath = lstFiles.value(nIndex);
if (appImage2Widget(filepath, strModel)) {
nSuc++;
progress.setValue(nSuc);
qApp->processEvents();
}
else {
emit(sgShowMsg(tr("添加失败,不能添加带检测背景的图片作为模板!!!")));
break;
}
}
int newImgCnt = m_pModelMgr->getModel(strModel)->getTempImage()->getImgCount();
if (oldImgsCnt <= 0 && newImgCnt > oldImgsCnt) {
//if (m_RepetBox)
{
cv::Mat srcRepet = imread(string((const char *)m_strAddPicPath.toLocal8Bit()), 0);
int IRepet = m_pModelMgr->getModel(strModel)->getImageComModel()->computeRepeatNum(srcRepet);
m_pModelMgr->getModel(strModel)->getImageComModel()->setRepeatNum(IRepet);
ui.ModelMgr_model_Repet_spinBox->setValue(IRepet);
}
}
progress.setLabel(tr("加载完成"));
qApp->processEvents();
progress.finish();
}
Q_SLOT void QModelMgrDlg::onModifyModel()
{
QString strModel = m_pShowImgList->property("model").toString();
if (strModel.isEmpty()) {
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要修改的型号!"), 0);
return;
}
if (!m_pModelMgr->getAllModelName().contains(strModel))
{
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要修改的型号!"), 0);
return;
}
IWheelModel*pModel = m_pModelMgr->getModel(strModel);
if (!pModel) {
return;
}
pModel->setThickness(ui.ModelMgr_model_edit_height->text().toDouble());
pModel->setDiameter(ui.ModelMgr_model_edit_diameter->text().toDouble());
bool trainFlag = ui.checkBox->isChecked();
pModel->setTrainFlag(trainFlag);
if (trainFlag == false)
{
m_bTrainCancel = true;
emit sgModifyModel(strModel);
}
double thisvalue = pModel->getImageComModel()->getDisThre();
double falsMinDis = pModel->getImageComModel()->getFalseSampleMinDis();
double disMax = pModel->getImageComModel()->getDisMax();
int pthrevalue = ui.ModelMgr_horizontalSlider->value();
double value = (falsMinDis - disMax) / 100.0*pthrevalue;
pModel->getImageComModel()->setDisThre(value + disMax);
pModel->setRepeatNum(ui.ModelMgr_model_Repet_spinBox->value());
QString templatepath = m_pCtrl->appRoot() + "\\pattern\\template\\";
templatepath += strModel;
templatepath += "\\model.yml";
std::string strS = templatepath.toLocal8Bit().toStdString();
pModel->getImageComModel()->save2file(strS);
5 years ago
m_pModelMgr->modModel(strModel);
m_pModelMgr->saveModel(strModel);
QString str = m_pCtrl->getUserName() + ":" + QString("%1:%2 %3").arg(tr("修改了模板")).arg(strModel).arg(tr("的参数"));
m_pCtrl->addLog(str, emTypeUseState);
//m_modLib = true;
emit sgShowMsg(tr("修改成功!"));
5 years ago
}
Q_SLOT void QModelMgrDlg::onDelModel()
{
QString strModel = m_pShowImgList->property("model").toString();
if (strModel.isEmpty()) {
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要删除的型号"), 0);
return;
}
if (!m_pModelMgr->getAllModelName().contains(strModel))
{
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要删除的型号"), 0);
return;
}
QString str = m_pCtrl->getUserName() + ":" + tr("按下了删除模板按钮");
m_pCtrl->addLog(str, emTypeUseState);
if (onMessageBox(QMessageBox::Information, tr("提示"), QString("%1 %2 %3").arg(tr("是否删除型号")).arg(strModel).arg(tr("产品模板")), 1) == false)
return;
m_pModelLists->removeModel(strModel);
m_pModelMgr->delModel(strModel);
m_pModelLists->update2AllModels("NG");
emit(m_pCtrl->sgModelChanged());
emit(sgGenalData(QVariant("removeModel"), QVariant(strModel)));
str = m_pCtrl->getUserName() + ":" + QString("%1:%2").arg(tr("删除了模板")).arg(strModel);
m_pCtrl->addLog(str, emTypeUseState);
QStringList strFilter = m_pCtrl->getAllModelMapPtr()->keys();
strFilter.removeAll("NG");
m_pModelLists->setModelList(strFilter);
QString m_str = QString("%1 %2 %3").arg(tr("")).arg(strFilter.size()).arg(tr("个型号"));
ui.ModelMgr_label_Model_number->setText(m_str);
onClearShow();
emit sgDelOldModel(strModel);
5 years ago
}
Q_SLOT void QModelMgrDlg::onAddModel()
{
QString str = m_pCtrl->getUserName() + ":" + tr("按下了添加模板按钮");
m_pCtrl->addLog(str, emTypeUseState);
QAddModel m_AddModel(this);
m_AddModel.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
m_AddModel.setWindowIcon(QIcon(":/image/leaper"));
m_AddModel.setWindowModality(Qt::ApplicationModal);
m_AddModel.setAttribute(Qt::WA_ShowModal, true);
m_AddModel.SetClearData();
m_AddModel.setWindowIcon(QIcon(":/image/leaper"));
m_AddModel.setWindowTitle(tr("产品信息设置"));
m_AddModel.setFocus();
if (m_AddModel.exec() == QDialog::Accepted) {
QMap<QString, QVariant> m_map = m_AddModel.getData();
IWheelModel *pModel = m_pModelMgr->createModel();
pModel->setDetectState(true);
pModel->setModelID(m_map.value(WF_HUBMODEL_ID).toString());
pModel->setChannel(m_map.value(WF_HUBMODEL_CHANNELS).toInt());
pModel->setDiameter(m_map.value(WF_HUBMODEL_DIAMETER).toDouble());
pModel->setThickness(m_map.value(WF_HUBMODEL_HEIGHT).toDouble());
pModel->setRepeatNum(m_map.value("rotate").toInt());
QString strPath2 = m_pCtrl->appRoot() + "\\pattern\\template\\";
hubBase::mkdir(strPath2);
hubBase::mkdir(strPath2 + pModel->getModelID());
m_pModelMgr->addModel(pModel);
m_pModelMgr->saveModel(pModel->getModelID());
m_pModelLists->update2AllModels("NG");
emit m_pCtrl->sgModelChanged();
QString str = m_pCtrl->getUserName() + ":" + QString("%1:%2").arg(tr("添加了一个新的模型")).arg(pModel->getModelID());
m_pCtrl->addLog(str, emTypeUseState);
onShowModelInfo(pModel->getModelID());
onShowModelPic(pModel->getModelID());
QStringList strFilter = m_pCtrl->getAllModelMapPtr()->keys();
strFilter.removeAll("NG");
m_pModelLists->setModelList(strFilter);
QString m_str = QString("%1 %2 %3").arg(tr("")).arg(strFilter.size()).arg(tr("个型号"));
ui.ModelMgr_label_Model_number->setText(m_str);
emit sgAddNewModel(pModel->getModelID());
5 years ago
}
}
Q_SLOT void QModelMgrDlg::onTrainModel()
{
if (m_trainOneTsk) {
if (!m_trainOneTsk->isRunning()) {
QString strModel = m_pShowImgList->property("model").toString();
if (strModel.isEmpty()) {
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要训练的型号!"), 0);
return;
}
m_strTrainModel = strModel;
m_ticktimer.start();
m_trainOneTsk->InitStopFlag();
m_trainOneTsk->start();
}
else {
m_trainOneTsk->stop();
m_trainOneTsk->quit();
emit sgShowMsg(tr("训练模板线程未退出,请稍等"));
}
}
}
Q_SLOT void QModelMgrDlg::onTrainAllModels()
{
if (m_trainAllTsk) {
if (!m_trainAllTsk->isRunning()) {
if (onMessageBox(QMessageBox::Warning, tr("提示"), tr("将对所有型号模板进行重新训练,期间系统的检测功能将不能正常运行直到模板训练完成,全部重新训练模板将需要很长时间,期间将不能进行其他工作,是否继续执行?"), 1) == false)
return;
m_ticktimer.start();
m_trainAllTsk->InitStopFlag();
m_trainAllTsk->start();
}
else {
m_trainAllTsk->stop();
m_trainAllTsk->quit();
emit sgShowMsg(tr("训练模板线程未退出,请稍等"));
}
}
}
Q_SLOT void QModelMgrDlg::onItemDoubleClicked(QListWidgetItem * item)
{
QObject *obj = sender();
QString objName = obj->objectName();
{
QString strModel = m_pShowImgList->property("model").toString();
QString itemText = item->text();
QString strWarning = tr("确定删除该图像?");
if (onMessageBox(QMessageBox::Warning, tr("模板图片删除"), strWarning, 1) == false)
return;
TempImage *pImg = m_pModelMgr->getTmpImage(strModel);
if (pImg) {
pImg->remove(itemText);
}
delete item;
QString m_str = QString("%1 %2 %3").arg(tr("")).arg(m_pShowImgList->count()).arg(tr("个图像"));
ui.label_imgNum->setText(m_str);
}
}
Q_SLOT void QModelMgrDlg::onPrograssShow(QString title, QString strValue, int size, int model)
{
if (!m_pProgressView)
{
m_pProgressView = new ProgressView(this);
m_pProgressView->setUseCancel(true);
connect(m_pProgressView, SIGNAL(cancel()), this, SLOT(onProgassCancel()));
//return;
}
switch (model)
{
case emPross_Init:
{
m_pProgressView->show(title, strValue, size, MODEL_UI_ICON_LEAPER);
}
break;
case emPross_SetValue:
{
m_pProgressView->setValue(size);
}
break;
case emPross_SetLable:
m_pProgressView->setLabel(strValue);
break;
case emPross_Finish:
{
m_pProgressView->finish();
}
break;
case emPross_enable:
onMessageBox(QMessageBox::Information, tr("提示"), strValue, 0);
break;
case emPross_Disable:
break;
}
}
Q_SLOT void QModelMgrDlg::setValue(int value)
{
ui.ModelMgr_Num->setText(QString::number(value) + "%");
}
Q_SLOT void QModelMgrDlg::onDelAllModels()
{
if (onMessageBox(QMessageBox::Warning, tr("警告"), tr("即将删除模板库里的所有模板,是否继续?"), 1) == false)
return;
if (onMessageBox(QMessageBox::Warning, tr("提示"), tr("再次确认,是否全部删除?"), 1) == false)
return;
QStringList ModeStrList = m_pModelMgr->getAllModelName();
if (ModeStrList.size() <= 0) {
onMessageBox(QMessageBox::Warning, tr("提示"), tr("没有模板!!!"), 0);
return;
}
for (int nIndex = 0; nIndex < ModeStrList.size(); nIndex++) {
QString strModel = ModeStrList.at(nIndex);
if (m_pModelMgr->getAllModelName().contains(strModel))
{
m_pModelLists->removeModel(strModel);
m_pModelMgr->delModel(strModel);
m_pModelLists->update2AllModels("NG");
emit m_pCtrl->sgModelChanged();
}
}
onClearShow();
onMessageBox(QMessageBox::Warning, tr("提示"), tr("全部删除完成!!!"), 0);
}
Q_SLOT void QModelMgrDlg::onChangeModelName()
{
QString strModel = m_pShowImgList->property("model").toString();
if (strModel.isEmpty())
{
onMessageBox(QMessageBox::Information, tr("提示"), tr("请选择一个您要修改的型号!"), 0);
return;
}
QModNamedlg dlg(this);
dlg.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
dlg.setWindowIcon(QIcon(":/image/leaper"));
dlg.setWindowModality(Qt::ApplicationModal);
dlg.setAttribute(Qt::WA_ShowModal, true);
QString m_newName;
IWheelModel *pModel = m_pModelMgr->getModel(strModel);
QString strPath = m_pCtrl->appRoot() + pModel->getPicPath();
dlg.setPixmap(strPath);
dlg.setOldModelName(strModel);
dlg.SetModelsStr(m_pModelMgr->getAllModelName());
if (QDialog::Accepted == dlg.exec())
{
m_newName = dlg.getNewModelName();
if (m_newName.isEmpty())
return;
}
else
return;
m_pModelMgr->reName(strModel, m_newName);
m_pModelLists->update2AllModels("NG");
emit(m_pCtrl->sgModelChanged());
onShowModelInfo(m_newName);
onShowModelPic(m_newName);
}
Q_SLOT void QModelMgrDlg::onProgassCancel()
{
if (m_trainAllTsk) {
m_trainAllTsk->stop();
m_trainAllTsk->quit();
}
if (m_trainOneTsk) {
m_trainOneTsk->stop();
m_trainOneTsk->quit();
}
m_ticktimer.elapsed();
}
Q_SLOT void QModelMgrDlg::onTableWidget(int nIndex)
{
if (nIndex == 1)
emit sgGenalData(QVariant("tabchange"), QVariant(1));
}
Q_SLOT void QModelMgrDlg::onTextChanged(const QString& str)
{
QStringList models = m_pModelMgr->getAllModelName();
models.removeAll("NG");
if (str.isEmpty() || str == "*") {
m_pModelLists->setModelList(models);
QString m_str = QString("%1 %2 %3").arg(tr("")).arg(models.size()).arg(tr("个型号"));
ui.ModelMgr_label_Model_number->setText(m_str);
}
else {
QStringList strFilter = models.filter(str);
m_pModelLists->setModelList(strFilter);
QString m_str = QString("%1 %2 个型号").arg(tr("")).arg(strFilter.size()).arg(tr("个型号"));
ui.ModelMgr_label_Model_number->setText(m_str);
}
}
Q_SLOT void QModelMgrDlg::onShowModelInfo(QString str)
{
ui.ModelMgr_horizontalSlider->setDisabled(false);
ui.ModelMgr_model_edit->setText(m_pModelMgr->getModel(str)->getModelID());
ui.ModelMgr_model_edit_diameter->setText(QString::number(m_pModelMgr->getModel(str)->getDiameter()));
ui.ModelMgr_model_edit_height->setText(QString::number(m_pModelMgr->getModel(str)->getThickness()));
ui.checkBox->setChecked(m_pModelMgr->getModel(str)->getAddTrainFlag());
double disThred = m_pModelMgr->getModel(str)->getImageComModel()->getDisThre();
double FalseMinDis = m_pModelMgr->getModel(str)->getImageComModel()->getFalseSampleMinDis();
double disMax = m_pModelMgr->getModel(str)->getImageComModel()->getDisMax();
ui.ModelMgr_model_Repet_spinBox->setValue(m_pModelMgr->getModel(str)->getRepeatNum());
if (disThred >= 1.7976931348623157e+308)
{
disThred = FalseMinDis;
}
int vvv = 0;
if (disMax <= FalseMinDis)
{
double vv = (FalseMinDis - disMax) / 100.0;
vvv = (disThred - disMax)*1.0 / vv;
}
if (-4.8366978272229995e-026 >= FalseMinDis)
{
ui.ModelMgr_horizontalSlider->setDisabled(true);
FalseMinDis = 0;
vvv = 0;
}
ui.ModelMgr_horizontalSlider->setRange(0, 100);
ui.ModelMgr_horizontalSlider->setValue(vvv);
ui.ModelMgr_Num->setText(QString::number(vvv) + "%");
updateWidget(str);
int IImgNum = m_pShowImgList->count();
QString strShow = QString("%1 %2 %3").arg(tr("")).arg(IImgNum).arg(tr("个图像"));
if (IImgNum > 0 && IImgNum < nGlobalMinImgs)
strShow += QString(",%1 %2 %3.").arg(tr("图像数量少于")).arg(nGlobalMinImgs).arg(tr("个,请及时补充训练样本"));
ui.label_imgNum->setText(strShow);
}
Q_SLOT void QModelMgrDlg::onShowModelPic(QString str)
{
QString nPixpath = m_pModelMgr->getModel(str)->getPicPath();
if (!nPixpath.isEmpty()) {
QString pixpath = m_pCtrl->appRoot() + nPixpath;
QPixmap pix(pixpath);
QSize m_size = pix.size();
qreal width = pix.width();
qreal height = pix.height();
if (pix.isNull()) {
QString filepath;
if (m_pModelMgr->getModel(str)->getTempImage()->getImgCount() > 0)
{
QString n_Pic_path = "\\pattern\\Models\\" + str + ".jpg";
QString strSavePath = m_pCtrl->appRoot();
filepath = strSavePath + n_Pic_path;
imwrite(string((const char *)filepath.toLocal8Bit()), m_pModelMgr->getModel(str)->getTempImage()->getImgVector().at(0));
}
QPixmap pixnuw(filepath);
if (pixnuw.isNull())
{
QPixmap pixnune(MODEL_UI_ICON_NONE);
ui.ModelMgr_modelpic_lable->setPixmap(pixnune.scaled(129, 129));
}
else {
ui.ModelMgr_modelpic_lable->setPixmap(pixnuw.scaled(129, 129));
}
}
else
ui.ModelMgr_modelpic_lable->setPixmap(pix.scaled(WS_PICSIZE, WS_PICSIZE));
}
else {
QPixmap pix(MODEL_UI_ICON_NONE);
ui.ModelMgr_modelpic_lable->setPixmap(pix.scaled(WS_PICSIZE, WS_PICSIZE));
}
}
void QModelMgrDlg::onClearShow()
{
ui.ModelMgr_model_edit->setText("");
ui.ModelMgr_model_edit_diameter->setText("");
ui.ModelMgr_model_edit_height->setText("");
ui.checkBox->setChecked(false);
QPixmap pix(MODEL_UI_ICON_NONE);
ui.ModelMgr_modelpic_lable->setPixmap(pix.scaled(WS_PICSIZE, WS_PICSIZE));
ui.ModelMgr_horizontalSlider->setValue(0);
m_pShowImgList->setProperty("model", "");
m_pShowImgList->clear();
}
void QModelMgrDlg::TrainAllTskFunc()
{
QStringList lstAll = m_pModelMgr->getAllModelName();
QMap<QString, vector<Mat>> mpAllImage = m_pModelMgr->getAllModelImage();
emit sgPrograssShow(tr("模板更新进度"), tr("正在初始化所有模板 共需要训练%1个模板,请稍后.....").arg(lstAll.size()), lstAll.size(), emPross_Init);
int nIndex = 0;
for (; nIndex < lstAll.size() && (!m_trainAllTsk->getStopFlag()); nIndex++) {
emit sgPrograssShow(tr("模板更新进度"), "", nIndex, emPross_SetValue);
QString m_modelName = lstAll.at(nIndex);
TempImage *pImg = m_pModelMgr->getTmpImage(m_modelName);
if (!pImg) {
continue;
}
QString str = tr("正在训练%1的模板 已完成%2/%3,请稍后.....").arg(m_modelName).arg(nIndex + 1).arg(m_pModelMgr->getAllModelName().size());
emit sgPrograssShow(tr("模板更新进度"), str, lstAll.size(), emPross_SetLable);
IWheelModel *pModel = m_pModelMgr->getModel(m_modelName);
std::vector<cv::Mat> mMat = pImg->getImgVector();
ICompareModel *m_compareModel = pModel->getImageComModel();
if (mMat.size() <= 0)
{
continue;
}
if (m_trainAllTsk->getStopFlag())
break;
m_compareModel->train(mMat);
QMap<QString, Mat> imgVec = m_pModelMgr->getAllImgsExcSelf(pModel->getModelID());
QMap<QString, Mat>::iterator it;
vector<QString> names;
std::map<std::string, double> disMap;
for (it = imgVec.begin(); it != imgVec.end(); it++)
{
QString strName = it.key();
double dis = m_compareModel->filterTrainImage(it.value());
disMap[strName.toStdString()] = dis;
}
vector<pair<string, double> > vec(disMap.begin(), disMap.end());
std::sort(vec.begin(), vec.end(), CmpByValue());
disMap.clear();
for (int j = 0; j < ceil(vec.size() / 2.0); j++)
{
names.push_back(QString::fromStdString(vec[j].first));
}
std::vector<modelInfo> m_modelInfo = m_pModelMgr->getAllTarImgs(names);
vector<Mat> falseSamples;
for (int k = 0; k < m_modelInfo.size(); k++)
{
const modelInfo& m_struct = m_modelInfo[k];
const vector<Mat>& vecMats = m_struct.mpAllImage;
falseSamples.insert(falseSamples.end(), vecMats.begin(), vecMats.end());
}
names.clear();
m_compareModel->weightOptimization(falseSamples);
m_compareModel->calculateAllParams(mMat);
if (m_trainAllTsk->getStopFlag())
break;
if (pModel->getPicPath().isEmpty())
{
QString modelName = pModel->getModelID();
QString strSavePath = m_pCtrl->appRoot();
QString n_Pic_path = "\\pattern\\Models\\" + modelName + ".jpg";
QString filepath = strSavePath + n_Pic_path;
imwrite(string((const char *)filepath.toLocal8Bit()), mMat.at(0));
pModel->setPicPath(n_Pic_path);
m_pModelMgr->modModel(m_modelName);
}
str = tr("%1的模板获取特征 已完成%2/%3,请稍后.....").arg(m_modelName).arg(nIndex + 1).arg(m_pModelMgr->getAllModelName().size());
emit sgPrograssShow(tr("模板更新进度"), str, lstAll.size(), emPross_SetLable);
m_compareModel->setIsEnableCache(true);
if (m_trainAllTsk->getStopFlag())
break;
m_compareModel->computeDisThre(falseSamples, NULL);
5 years ago
if (m_trainAllTsk->getStopFlag())
break;
double m_double = m_compareModel->getDisThre();
qWarning() << m_modelName << ":" << "disthre" << m_double;
/******************************************************************/
QString templatepath = m_pCtrl->appRoot() + "\\pattern\\template\\";
templatepath += m_modelName;
templatepath += "\\model.yml";
std::string strS = templatepath.toLocal8Bit().toStdString();
m_compareModel->save2file(strS/*string((const char *)templatepath.toLocal8Bit())*/);
cv::Mat dst = m_compareModel->getM8uMaskImg();
QImage img(dst.data, dst.cols, dst.rows, dst.cols, QImage::Format_Indexed8);
QPixmap pix = QPixmap::fromImage(img);
ui.ModelMgr_modelpic_lable->setPixmap(pix.scaled(129, 129));
emit sgTrainShowInfo(m_modelName);
str = tr("%1的模板训练完成 已完成%2/%3,请稍后.....").arg(m_modelName).arg(nIndex + 1).arg(m_pModelMgr->getAllModelName().size());
emit sgPrograssShow(tr("模板更新进度"), "", nIndex, emPross_SetValue);
}
if (!m_trainAllTsk->getStopFlag())
{
emit sgPrograssShow(tr("模板更新进度"), "", nIndex, emPross_Finish);
emit sgPrograssShow(tr("模板更新进度"), tr("全部模板更新完成!%1s").arg(m_ticktimer.elapsed() / 1000.0), nIndex, emPross_enable);
QString str = m_pCtrl->getUserName() + ":" + tr("对全部模板进行了重新训练,共%1个模板").arg(lstAll.size());
m_pCtrl->addLog(str, emTypeUseState);
}
}
void QModelMgrDlg::TrainOneTskFunc()
{
if (m_strTrainModel.isEmpty())
return;
int m_step = 5;
QString str = tr("模板正在准备请稍后.....");
emit sgPrograssShow(tr("模板训练进度"), str, m_step, emPross_Init);
IWheelModel*pModel = m_pModelMgr->getModel(m_strTrainModel);
if (!pModel) {
return;
}
emit sgPrograssShow(tr("模板训练进度"), str, 0, emPross_SetValue);
if (m_trainOneTsk->getStopFlag())
return;
TempImage *pTempImg = pModel->getTempImage();
std::vector<Mat> m_MatVec = pTempImg->getImgVector();
if (m_MatVec.size() <= 0)
{
emit sgPrograssShow(tr("模板训练进度"), tr("%1型号没有图片不能训练").arg(m_strTrainModel), 6, emPross_enable);
emit sgPrograssShow(tr("模板训练进度"), tr("%1型号没有图片不能训练").arg(m_strTrainModel), 6, emPross_Finish);
return;
}
if (m_trainOneTsk->getStopFlag())
return;
/*这里需要加载提示 说明模板正在训练*/
emit sgPrograssShow(tr("模板训练进度"), str, 1, emPross_SetValue);
ICompareModel *m_compareModel = pModel->getImageComModel();
m_compareModel->train(m_MatVec); //////////////////////////////////////////// tain model
QMap<QString, Mat> imgVec = m_pModelMgr->getAllImgsExcSelf(pModel->getModelID());
QMap<QString, Mat>::iterator it;
vector<QString> names;
std::map<std::string, double> disMap;
if (m_trainOneTsk->getStopFlag())
return;
for (it = imgVec.begin(); it != imgVec.end(); it++)
{
QString strName = it.key();
double dis = m_compareModel->filterTrainImage(it.value());
if (dis != DBL_MAX)
{
disMap[strName.toStdString()] = dis;
names.push_back(strName);
}
}
if (m_trainOneTsk->getStopFlag())
return;
std::vector<modelInfo> m_modelInfo = m_pModelMgr->getAllTarImgs(names);
vector<Mat> falseSamples;
for (int k = 0; k < m_modelInfo.size(); k++)
{
const modelInfo& m_struct = m_modelInfo[k];
const vector<Mat>& vecMats = m_struct.mpAllImage;
falseSamples.insert(falseSamples.end(), vecMats.begin(), vecMats.end());
}
names.clear();
m_compareModel->weightOptimization(falseSamples);
m_compareModel->calculateAllParams(m_MatVec);
if (m_trainOneTsk->getStopFlag())
return;
emit sgPrograssShow(tr("模板训练进度"), str, 2, emPross_SetValue);
hubBase::mkdir(m_pCtrl->appRoot() + "\\pattern");
QString templatepath = m_pCtrl->appRoot() + "\\pattern\\template\\";
hubBase::mkdir(templatepath);
templatepath += m_strTrainModel;
templatepath += "\\model.yml";
m_compareModel->setName(m_strTrainModel.toLatin1().data());
std::string strS = templatepath.toLocal8Bit().toStdString();
m_compareModel->save2file(strS);//////////////////////////////save params
5 years ago
if (pModel->getPicPath().isEmpty())
{
QString modelName = pModel->getModelID();
QString strSavePath = m_pCtrl->appRoot();
QString n_Pic_path = "\\pattern\\Models\\" + modelName + ".jpg";
QString filepath = strSavePath + n_Pic_path;
imwrite(string((const char *)filepath.toLocal8Bit()), m_MatVec.at(0));
pModel->setPicPath(n_Pic_path);
m_pModelMgr->modModel(m_strTrainModel);
}
if (m_trainOneTsk->getStopFlag())
return;
emit sgPrograssShow(tr("模板训练进度"), str, 3, emPross_SetValue);
emit sgPrograssShow(tr("模板训练进度"), str, 4, emPross_SetValue);
emit sgPrograssShow(tr("模板训练进度"), tr("%1的模板 正在获取特征值,请稍后....").arg(m_strTrainModel), 4, emPross_SetLable);
m_compareModel->setIsEnableCache(false);
m_compareModel->computeDisThre(falseSamples, NULL);
strS = templatepath.toLocal8Bit().toStdString();
m_compareModel->save2file(strS);
5 years ago
if (m_trainOneTsk->getStopFlag())
return;
emit sgPrograssShow(tr("模板训练进度"), str, 6, emPross_SetValue);
emit sgPrograssShow(tr("模板训练进度"), str, 6, emPross_Finish);
emit sgPrograssShow(tr("模板训练进度"), tr("训练完成!%1s").arg(m_ticktimer.elapsed() / 1000.0), 6, emPross_enable);
emit sgTrainShowInfo(m_strTrainModel);
str = m_pCtrl->getUserName() + ":" + tr("训练了模板:%1").arg(m_strTrainModel);
m_pCtrl->addLog(str, emTypeUseState);
m_strTrainModel.clear();
}
void QModelMgrDlg::onUserLevel(int level)
{
if (level > 4)
{
ui.m_pbDelAll->setVisible(true);
}
else {
ui.m_pbDelAll->setVisible(false);
}
}
void QModelMgrDlg::updateWidget(QString str)
{
// update images
for (int nIndex = (m_pShowImgList->count() - 1); nIndex >= 0; nIndex--) {
QListWidgetItem *pItem = m_pShowImgList->takeItem(nIndex);
delete pItem;
}
m_pShowImgList->setViewMode(QListView::IconMode);
m_pShowImgList->clear();
m_pShowImgList->setProperty("model", str);
/*
*/
m_pShowImgList->setIconSize(QSize(WS_PICSIZELIST_ITEM, WS_PICSIZELIST_ITEM));
m_pShowImgList->setAcceptDrops(false);
std::vector<TempImageInfo*> imgTemplateLib = m_pModelMgr->getTmpImage(str)->getVectors();
for (int j = 0; j < imgTemplateLib.size(); j++)
{
cv::Mat dst;
cv::Mat src = imgTemplateLib.at(j)->m_img;
cv::resize(src, dst, cv::Size(int(src.cols * 1.5) / 4 * 4, src.rows*1.5));
QImage img(dst.data, dst.cols, dst.rows, QImage::Format_Indexed8);
QPixmap pix = QPixmap::fromImage(img);
QIcon icon(pix.scaled(QSize(WS_PICSIZELIST_ITEM, WS_PICSIZELIST_ITEM)));
QListWidgetItem* pItem = new QListWidgetItem(icon, imgTemplateLib.at(j)->m_strFileName, m_pShowImgList);
m_pShowImgList->addItem(pItem);
}
}
bool QModelMgrDlg::appImage2Widget(QString strPath, QString strModel)
{
//抠图+
cv::Mat dst = imread(string((const char *)strPath.toLocal8Bit()), 0);
if (dst.empty()) {
return false;
}
if (dst.rows >= 900 || dst.cols >= 900)
return false;
//显示
QString strFileName = strModel + "_" + hubBase::genDateTime("MMddhhmmzzz");
cv::Mat show;
cv::resize(dst, show, cv::Size(int(dst.cols * 1.5) / 4 * 4, dst.rows*1.5));
QImage img(show.data, show.cols, show.rows, QImage::Format_Indexed8);
QPixmap pix = QPixmap::fromImage(img);
QIcon icon(pix.scaled(QSize(WS_PICSIZELIST_ITEM, WS_PICSIZELIST_ITEM)));
QFileInfo fi;
fi = QFileInfo(strPath);
QListWidgetItem *pItem = new QListWidgetItem(icon, strFileName);
m_pShowImgList->insertItem(m_pShowImgList->count(), pItem);
m_pShowImgList->setViewMode(QListView::IconMode);
m_pShowImgList->setIconSize(QSize(WS_PICSIZELIST_ITEM, WS_PICSIZELIST_ITEM));
m_pShowImgList->setAcceptDrops(false);
//add 2 lib
IWheelModel *pModel = m_pModelMgr->getModel(strModel);
QString strPath2 = m_pCtrl->appRoot() + "\\pattern\\template\\";
hubBase::mkdir(strPath2);
hubBase::mkdir(strPath2 + strModel);
strPath2 += strModel + "\\" + strFileName + ".png";
cv::imwrite(string((const char *)strPath2.toLocal8Bit()), dst);
pModel->getTempImage()->add(dst, strFileName, strPath2);
QString m_str = QString("%1 %2 %3").arg(tr("")).arg(m_pShowImgList->count()).arg(tr("个图像"));
ui.label_imgNum->setText(m_str);
return true;
}
void QModelMgrDlg::TestAddModel()
{
int m_nIndex = m_pModelMgr->getAllModelName().size();
for (int nIndex = m_nIndex; nIndex < 1000; nIndex++) {
QMap<QString, QVariant> m_map;
m_map.insert(WF_HUBMODEL_ID, nIndex);
m_map.insert(WF_HUBMODEL_HEIGHT, nIndex);
m_map.insert(WF_HUBMODEL_DIAMETER, nIndex);
m_map.insert(WF_HUBMODEL_CHANNELS, 0);
IWheelModel *pModel = m_pModelMgr->createModel();
pModel->setModelID(m_map.value(WF_HUBMODEL_ID).toString());
pModel->setChannel(m_map.value(WF_HUBMODEL_CHANNELS).toInt());
pModel->setDiameter(m_map.value(WF_HUBMODEL_DIAMETER).toDouble());
pModel->setThickness(m_map.value(WF_HUBMODEL_HEIGHT).toDouble());
pModel->setRepeatNum(m_map.value("rotate").toInt());
QString strPath2 = m_pCtrl->appRoot() + "\\pattern\\template\\";
hubBase::mkdir(strPath2);
hubBase::mkdir(strPath2 + pModel->getModelID());
m_pModelMgr->addModel(pModel);
m_pModelLists->update2AllModels("NG");
emit m_pCtrl->sgModelChanged();
}
}
Q_SLOT void QModelMgrDlg::onShowMessage(QString str)
{
QPulpewidget *pw = new QPulpewidget();
pw->showmessage(str);
pw->setParent(this);
connect(pw, SIGNAL(finished()), pw, SLOT(deleteLater()));//
}
Q_SLOT void QModelMgrDlg::onDeleteShow(QWidget* p)
{
delete p;
p = NULL;
}
bool QModelMgrDlg::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
QDir targetDir(toDir);
if (!targetDir.exists()) {
if (!targetDir.mkdir(targetDir.absolutePath()))
return false;
}
sourceDir.setFilter(QDir::Files);
QFileInfoList fileInfoList = sourceDir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList) {
QString str = fileInfo.fileName();
QString filefpath = fileInfo.filePath();
QString newfile = targetDir.filePath(fileInfo.fileName());
if (!QFile::copy(filefpath, newfile)) {
return false;
}
}
return true;
}
bool QModelMgrDlg::onMessageBox(QMessageBox::Icon ntype, QString strTitle, QString strAtl, int onlyOK)
{
QMessageBox::StandardButton button = QMessageBox::Ok;
if (onlyOK == 1)
button = QMessageBox::Cancel;
QMessageBox infobox(ntype, strTitle, strAtl, QMessageBox::Ok | button, this);
infobox.setWindowIcon(QIcon(":/image/leaper"));
infobox.setButtonText(QMessageBox::Ok, tr("确认"));
infobox.setButtonText(QMessageBox::Cancel, tr("取消"));
return (infobox.exec() == QMessageBox::Ok);
}
Q_SLOT void QModelMgrDlg::onButtonClicked()
{
QString strObj = sender()->objectName();
if ("m_pbAdd" == strObj)//添加新型号
{
onAddModel();
}
else if ("m_pbDel" == strObj)//删除型号
{
onDelModel();
}
else if ("m_pbMod" == strObj)//修改型号
{
onModifyModel();
}
else if ("m_pbModName" == strObj)//修改型号名
{
onChangeModelName();
}
else if ("m_pbTrain" == strObj)//训练单个
{
onTrainModel();
}
else if ("m_pbTrainAll" == strObj)//训练全部
{
onTrainAllModels();
}
else if ("m_pbAddPic" == strObj)//添加图片
{
onAddPic2Model();
}
else if ("m_pbDelAll" == strObj)//删除所有
{
onDelAllModels();
}
}
void QModelMgrDlg::showEvent(QShowEvent *event)
{
if (m_pModelLists&&m_pModelMgr)
{
QStringList strModels = m_pModelMgr->getAllModelMapPtr()->keys();
strModels.removeAll("NG");
m_pModelLists->setModelList(strModels);
QString m_str = QString("%1 %2 个型号").arg(tr("")).arg(strModels.size()).arg(tr("个型号"));
ui.ModelMgr_label_Model_number->setText(m_str);
}
}
void QModelMgrDlg::closeEvent(QCloseEvent *event)
{
if (m_bTrainCancel)
{
event->accept();
emit sgUpdatedefect();
m_bTrainCancel = false;
}
return;
}