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

1013 lines
34 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "QModelMgrDlg.h"
#include "ProgressView.h"
#include <QFileInfo>
#include <QDir>
#include "qpulpewidget.h"
#include "HubBase.h"
#include "IWheelModel.h"
#include "TempImage.h"
#include "ImageCompareModel.h"
#include "qaddmodel.h"
#include "qmodnamedlg.h"
#include <vector>
#include "QSettings.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);
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_pState = m_pCtrl->getDetectState();
m_trainAllTsk = new QMyThread();
m_trainAllTsk->setUser(true);
m_trainAllTsk->loadfunc(this, &QModelMgrDlg::TrainAllTskFunc);
m_trainOneTsk = new QMyThread();
m_trainOneTsk->setUser(true);
m_trainOneTsk->loadfunc(this, &QModelMgrDlg::TrainOneTskFunc);
m_pProgressView = new ProgressView(this);
m_pProgressView->setUseCancel(true);
connect(m_pProgressView, SIGNAL(cancel()), this, SLOT(onProgassCancel()));
//m_pProgressView->finish();
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)));
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));
}
}
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());
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);
//if (m_RepetBox)
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/*string((const char *)templatepath.toLocal8Bit())*/);
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("Ð޸ijɹ¦£¡"));
}
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();
}
Q_SLOT void QModelMgrDlg::onAddModel()
{
QString str = m_pCtrl->getUserName() + ":" + tr("°´ÏÂÁËÌí¼ÓÄ£°å°´Å¥");
m_pCtrl->addLog(str, emTypeUseState);
QAddModel m_AddModel;
m_AddModel.SetClearData();
m_AddModel.setWindowIcon(QIcon(MODEL_UI_ICON_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);
}
}
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();
//if ("ModelMgr_showPic_listWidget_tab1" == objName)
{
// if (m_pCtrl->getDetectState()->m_Debug != 100)
// if (m_pCtrl->getUserLevel() <= 0)
// return;
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);
}
// else if ("ModelMgr_listWidget_tab2" == objName)
// {
// int a = 0;
// }
}
Q_SLOT void QModelMgrDlg::onPrograssShow(QString title, QString strValue, int size, int model)
{
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;
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);
//m_FindModel = models;
}
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()));
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);
/* if (m_WarningMsg)
{
int nTypeModel = m_pModelMgr->getModel(str)->getImageModel();
if (nTypeModel == 0)
m_WarningMsg->setText(tr("¸ÃÄ£°åѵÁ·³É¹¦£¬¿ÉÒÔÕý³£Ê¹ÓÃ"));
else if (nTypeModel == 1)
m_WarningMsg->setText(tr("¸ÃÄ£°åûÓÐѵÁ·£¬²¢ÇÒûÓÐͼÏñ"));
else if (nTypeModel == 2)
m_WarningMsg->setText(tr("¸ÃÄ£°åѵÁ·Ê§°Ü£¬Çë¼ì²éÄ£°åͼƬÖÐÊÇ·ñÓÐÆäËûÀàÐ͵ÄͼÏñ»ò¸ÃÄ£°åÊÇ·ñ±»Öظ´Ìí¼Ó"));
else
m_WarningMsg->setText(QString("¡£¡£¡£¡£¡£¡£"));
}*/
}
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("");
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();
ImageCompareModel *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));
}
//vector<Mat> falseSamples = m_pModelMgr->getAllModelImageExcSelf(pModel->getModelID());
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;
/*if (pModel->getDiameter() && pModel->getThickness())
{
m_compareModel->setRealWidth(pModel->getDiameter());
m_compareModel->setRealHeight(pModel->getThickness());
}*/
//for (int h = 0; h < m_modelInfo.size(); h++)
//{
//const vector<Mat>& vectorMats = m_modelInfo[h].mpAllImage;
/*double md_diameter = m_modelInfo[h].md_diameter;
double md_height = m_modelInfo[h].md_height;*/
m_compareModel->computeDisThre(falseSamples, NULL);
//}
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);
ImageCompareModel *m_compareModel = pModel->getImageComModel();
//std::vector<cv::Mat> firstFalseSample = m_pModelMgr->getFirstImgFromFalse(pModel->getModelID());
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;
// 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));
// }
// if (m_trainOneTsk->getStopFlag())
// return;
//vector<Mat> falseSamples = m_pModelMgr->getAllModelImageExcSelf(pModel->getModelID());
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/*string((const char *)templatepath.toLocal8Bit())*/);//////////////////////////////save params
if (pModel->getPicPath().isEmpty())
{
QString modelName = pModel->getModelID();
QString strSavePath = m_pCtrl->appRoot();
//hubBase::mkdir(strSavePath);
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);
//if (pModel->getDiameter() && pModel->getThickness())
//{
// m_compareModel->setRealWidth(pModel->getDiameter());
// m_compareModel->setRealHeight(pModel->getThickness());
//}
//for (int h = 0; h < m_modelInfo.size(); h++)
//{
//const vector<Mat>& vectorMats = m_modelInfo[h].mpAllImage;
/*double md_diameter = m_modelInfo[h].md_diameter;
double md_height = m_modelInfo[h].md_height;*/
m_compareModel->computeDisThre(falseSamples, NULL);
//}
strS = templatepath.toLocal8Bit().toStdString();
m_compareModel->save2file(strS/*string((const char *)templatepath.toLocal8Bit())*/);
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::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);
//cv::imwrite(strPath2.toLatin1().data(), 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, NULL);
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();
}
}