diff --git a/src/ADModule.cpp b/src/ADModule.cpp index 87d0d38..b0e841c 100644 --- a/src/ADModule.cpp +++ b/src/ADModule.cpp @@ -5,6 +5,7 @@ ADModule::ADModule(QObject *parent): QObject(parent) ,m_mutex(QMutex::Recursive) { qRegisterMetaType>>("QVector>"); + qRegisterMetaType>("QVector"); m_vecFmat = std::make_shared(); m_pThread = new QThread(); moveToThread(m_pThread); diff --git a/src/CameraControl.cpp b/src/CameraControl.cpp index 6cf71a0..6654856 100644 --- a/src/CameraControl.cpp +++ b/src/CameraControl.cpp @@ -163,24 +163,35 @@ bool CameraControl::initCamera() qWarning() << "Load camera failed"; return false; } - else { - QStringList serialNumberList = m_mapSerialNumberIdSide.keys(); - serialNumberList.append(m_mapSerialNumberIdTop.keys()); - if (!openCamera(serialNumberList)) + else + { + QStringList serialNumberList = m_mapSNIndex.keys(); + for each (const QString& serialNumber in serialNumberList) { - qWarning() << "Open camera failed"; - return false; - } - else { - if (!startCamera(serialNumberList)) + if (!openCamera(serialNumber)) { - qWarning() << "Start camera failed"; - return false; + qWarning() << "Open camera: " << serialNumber << " failed"; + continue; } - else { - if (!setSoftwareTrigger(serialNumberList)) + else + { + qDebug() << "Open camera: " << serialNumber << " successed"; + if (!startCamera(serialNumber)) + { + qWarning() << "Start camera: " << serialNumber << " failed"; + continue; + } + else { - qWarning() << "Set software trigger failed"; + qDebug() << "Start camera: " << serialNumber << " successed"; + if (!setSoftwareTrigger(serialNumber)) + { + qWarning() << "Set camera: " << serialNumber << " software trigger failed"; + } + else + { + qDebug() << "Set camera: " << serialNumber << " software trigger successed"; + } } } } @@ -212,10 +223,12 @@ bool CameraControl::loadCamera(const QList& cameraInfoList) else { qWarning() << "SerialNumber: " << serialNumber << " init failed"; - flag = true; + DELETE_POINTER(pHikCamera); + //flag = false; } } } + qDebug() << "m_vecPCamera size: " << m_vecPCamera.size(); return flag; } bool CameraControl::openCamera(const QStringList& serialNumberList) @@ -439,12 +452,20 @@ void CameraControl::capture(const QStringList& serialNumberList) { int len = serialNumberList.length(); qDebug() << "start capture, length: " << len; - QVector vecImageInfo(len); + QVector vecImageInfo; for (int i = 0; i < len; ++i) { QString serialNumber = serialNumberList.at(i); - getSaveImgNameInfo(serialNumber, vecImageInfo[i].saveImgInfo); - int id = vecImageInfo.at(i).saveImgInfo.strId.toInt(); + if (!m_mapSNIndex.contains(serialNumber)) + { + qWarning() << "m_vecPCamera does not have camera: " << serialNumber; + continue; + } + ImageInfo imgInfo; + FileInfo saveImgInfo; + getSaveImgNameInfo(serialNumber, saveImgInfo); + imgInfo.saveImgInfo = saveImgInfo; + int id = saveImgInfo.strId.toInt(); int rotationAngle = 0; if (m_mapIdCameraInfoTop.contains(id)) { @@ -454,9 +475,9 @@ void CameraControl::capture(const QStringList& serialNumberList) { rotationAngle = m_mapIdCameraInfoSide.value(id).rotationAngle; } - flipImg(capture(serialNumber), vecImageInfo[i].image, rotationAngle); - //vecImageInfo[i].image = capture(serialNumber); - vecImageInfo[i].cameraSn = serialNumber; + flipImg(capture(serialNumber), imgInfo.image, rotationAngle); + imgInfo.cameraSn = serialNumber; + vecImageInfo.push_back(imgInfo); } emit sgCapturedImage(vecImageInfo, m_eCameraLocation); } @@ -476,7 +497,7 @@ void CameraControl::flipImg(const cv::Mat& imgSrc, cv::Mat& imgDst, int rotation qDebug() << " 旋转90度"; break; case 180: - cv::flip(imgSrc, imgDst, 0); + cv::flip(imgSrc, imgDst, -1); qDebug() << " 旋转180度"; break; case 270: diff --git a/src/CategoryMatcher.cpp b/src/CategoryMatcher.cpp index f4e286c..c0ae83b 100644 --- a/src/CategoryMatcher.cpp +++ b/src/CategoryMatcher.cpp @@ -65,8 +65,14 @@ void CategoryMatcher::setILMatch(const MatchParam& matchParam) m_ilmatchPtr->AcceptScore = m_matchParam.AcceptScore; } } + bool CategoryMatcher::smokeMatch(const QString& typeNo, const cv::Mat& inputMat, cv::Mat& rltMat) { + if (inputMat.empty()) + { + qWarning() << "inputMat is empty"; + return false; + } ILImagePtr ilImgGray(__uuidof(LImage)); cv::Mat imgMatGray; ensureGrayImg(inputMat, imgMatGray); diff --git a/src/HikCamera.cpp b/src/HikCamera.cpp index 710ce68..52c8080 100644 --- a/src/HikCamera.cpp +++ b/src/HikCamera.cpp @@ -29,6 +29,7 @@ bool HikCamera::initCamera(const QString& serialNumber) return false;; } int deviceNum = stDeviceList.nDeviceNum; + bool findInitFlag = false; if (deviceNum == 0) { qWarning() << "Do not found any device!"; @@ -68,12 +69,13 @@ bool HikCamera::initCamera(const QString& serialNumber) qWarning() << "Can't connect: " << serialNumber; return false; } + findInitFlag = true; break; } } } } - return true; + return findInitFlag; } //void HikCamera::initCamera(const void* pDeviceInfo) diff --git a/src/SmokeBoxIdentification.cpp b/src/SmokeBoxIdentification.cpp index 5fb0dd4..b080680 100644 --- a/src/SmokeBoxIdentification.cpp +++ b/src/SmokeBoxIdentification.cpp @@ -41,7 +41,8 @@ SmokeBoxIdentification::SmokeBoxIdentification(QWidget *parent) m_pTcpClientThread = new QThread(); m_pNetControl->moveToThread(m_pTcpClientThread); m_pTcpClientThread->start(); - connect(m_pNetControl, &NetControl::sgReceiveData, this, &SmokeBoxIdentification::onDecodeMsg); + connect(m_pNetControl, &NetControl::sgReceiveData, this, &SmokeBoxIdentification::onDecodeMsg, Qt::QueuedConnection); + connect(this, &SmokeBoxIdentification::sgSendMsg, m_pNetControl, &NetControl::onSendMsg, Qt::QueuedConnection); ADModule::instance()->loadParameters(filePath); filePath = applicationDirPath + COM_CONFIG; lpSerialStation::instance()->loadParameters(filePath); @@ -72,7 +73,6 @@ SmokeBoxIdentification::SmokeBoxIdentification(QWidget *parent) //connect(this, &SmokeBoxIdentification::sgSendData2AndAlgo, ADModule::instance(), &ADModule::onSendData2Algo); //connect(this, &SmokeBoxIdentification::sgSaveSensorData2Local, ADModule::instance(), &ADModule::onSaveData2File); //connect(this, &SmokeBoxIdentification::sgSendResults, this, &SmokeBoxIdentification::onSendResults); - connect(this, &SmokeBoxIdentification::sgSendMsg, m_pNetControl, &NetControl::onSendMsg, Qt::QueuedConnection); connect(ui.action_templateNum, SIGNAL(triggered()), this, SLOT(onActionClicked())); emit sgControlSideLight(lightCtrl::emTurnOnLight); } @@ -343,7 +343,7 @@ Q_SLOT void SmokeBoxIdentification::onNumStatistic(QString typeNum) { templateName = m_mapSmokeName2TemplateFolderName.value(smokeCnName); QtConcurrent::run([=]() { - dataPreProcessing::QStandardnormFileMapLabelSsample m = m_pDataProcess->getDataStructure(); + dataPreProcessing::QStandardnormFileMapLabelSample m = m_pDataProcess->getDataStructure(); int matchLabel = -2; if (m.contains(templateName)) { diff --git a/src/SmokeBoxIdentification.h b/src/SmokeBoxIdentification.h index 6727191..e9dfb7c 100644 --- a/src/SmokeBoxIdentification.h +++ b/src/SmokeBoxIdentification.h @@ -37,6 +37,14 @@ enum WorkMode IntoStock }; +enum StockCheckMode +{ + NoVirtual = 0, + TypeVirtual, //品规模拟盘点 + NumVirtual, //数量模拟盘点 + TypeNumVirtual //品规数量模拟盘点 +}; + struct IntoStockInfo { QString streetName; @@ -150,6 +158,7 @@ private: StockCheckInfo m_currtStockCheckInfo; EmptyCheckInfo m_currtEmptyCheckInfo; QNumTemplateBuild* m_pNumTemplateBuild{ nullptr }; + StockCheckMode m_stockCheckMode; QThread* m_pTcpClientThread; //QLabel* m_pShowRltImgLabel; diff --git a/src/numStatisticAlgo/dataPreProcessing.cpp b/src/numStatisticAlgo/dataPreProcessing.cpp index 085abac..054fbbb 100644 --- a/src/numStatisticAlgo/dataPreProcessing.cpp +++ b/src/numStatisticAlgo/dataPreProcessing.cpp @@ -304,7 +304,7 @@ bool dataPreProcessing::loadSampleWithItsCorresLabel(const QString& samplePath) } mLabelMapSample.insert(mLabel.toInt(), mRightLeftMapSample); } - m_StandardnormFileMapLabelSsample[dir.dirName()] = mLabelMapSample; + m_StandardnormFileMapLabelSample[dir.dirName()] = mLabelMapSample; return true; } @@ -349,8 +349,7 @@ QVector dataPreProcessing::modelPrediction() return finalLabelVec; } - -int dataPreProcessing::modelPrediction(const QVector> vec, QString typeSerial, QString leftRightJudge) +int dataPreProcessing::modelPrediction(const QVector> dataVec, QString typeSerial, QString leftRightJudge) { if (leftRightJudge.isEmpty()) { @@ -358,137 +357,161 @@ int dataPreProcessing::modelPrediction(const QVector> vec, QStri } //mComparedValMapLabel.clear(); - if (vec.size() <= 0) + if (dataVec.size() <= 0) { qDebug() << "model vec is empty"; return -1; } - QMultiMap>>> typeTarLabelMapSampleVec = m_StandardnormFileMapLabelSsample[typeSerial]; + QMultiMap>>> typeTarLabelMapSampleVec = m_StandardnormFileMapLabelSample[typeSerial]; if (typeTarLabelMapSampleVec.size() == 0) { - qDebug() << "typeTarsize"; - return -1; - } - QVector reshapedTarVec; - loadedDataReshapeForComparison(vec, reshapedTarVec); - if (reshapedTarVec.size() <= 0) - { - qDebug() << "reshape"; + qDebug() << typeSerial << " do not have template file"; return -1; } - qDebug() << "tarReshapedSize:" << reshapedTarVec.size(); - cv::Mat tarShapeMat(1, reshapedTarVec.size(), CV_64FC1, &reshapedTarVec[0]); - cv::Mat test; - tarShapeMat.convertTo(test, CV_32FC1); - cv::Mat dstTarShapeMat; - cv::medianBlur(test, dstTarShapeMat, 7); - dstTarShapeMat.convertTo(dstTarShapeMat, CV_64FC1); - std::vector dstTarShapeVec; auto convertToVector = [&](const cv::Mat& m, std::vector& v) { + v.clear(); double *p = (double*)m.row(0).data; for (int i = 0; i < m.cols; i++) { v.push_back(p[i]); } }; - convertToVector(dstTarShapeMat, dstTarShapeVec); - LB_Improved filter(dstTarShapeVec, reshapedTarVec.size() / 50.0); - qDebug() << "filter v size: " << filter.getVSize(); + auto dataReshapeAndPretreatment = [&](const QVector>& srcVec, std::vector& reshapedVec)->bool + { + QVector reshapedTarVec; + loadedDataReshapeForComparison(srcVec, reshapedTarVec); + if (reshapedTarVec.size() <= 0) + { + qDebug() << "reshapedTarVec is empty"; + return false; + } + //qDebug() << "tarReshapedSize:" << reshapedTarVec.size(); + cv::Mat tarShapeMat(1, reshapedTarVec.size(), CV_64FC1, &reshapedTarVec[0]); + cv::Mat test; + tarShapeMat.convertTo(test, CV_32FC1); + cv::Mat dstTarShapeMat; + cv::medianBlur(test, dstTarShapeMat, 7); + dstTarShapeMat.convertTo(dstTarShapeMat, CV_64FC1); + convertToVector(dstTarShapeMat, reshapedVec); + return true; + }; + std::vector dstTarShapeVec; + if (!dataReshapeAndPretreatment(dataVec, dstTarShapeVec)) + { + return -1; + } + //LB_Improved filter(dstTarShapeVec, dstTarShapeVec.size() / 50.0); + //qDebug() << "filter v size: " << filter.getVSize(); //double bestSofar = filter.getLowestCost(); - QMultiMap>>>::iterator it; QVector>> loopVec; QVector labelLoopVec; double disMin = DBL_MAX; int bestLabel = -1; - + int bestLabel1 = -1; //test double globalVal = DBL_MAX; - for (it = typeTarLabelMapSampleVec.begin(); it != typeTarLabelMapSampleVec.end(); ++it) { + QMultiMap>>>::iterator it; + for (it = typeTarLabelMapSampleVec.begin(); it != typeTarLabelMapSampleVec.end(); ++it) + { //loopVec.push_back(it.value()); //labelLoopVec.push_back(it.key()); const int label = it.key(); //qDebug() << "num folder: " << label; QMultiMap>> rightLeftMapValueVec = it.value(); - QMultiMap>>::iterator iterRighLeftMap = rightLeftMapValueVec.begin(); - - for (iterRighLeftMap; iterRighLeftMap != rightLeftMapValueVec.end(); iterRighLeftMap++) + if (!rightLeftMapValueVec.contains(leftRightJudge)) { - const QString& leftOrRight = iterRighLeftMap.key(); - //qDebug() << "leftOrRight: " << leftOrRight; - if (leftOrRight == leftRightJudge) + qWarning() << QString("%1 %2 do not have %3 template file!").arg(typeSerial) + .arg(label).arg(leftRightJudge); + continue; + } + for(const QVector>& srcSampleVec : rightLeftMapValueVec.values(leftRightJudge)) + { + if (srcSampleVec.size() <= 0) + { + + qWarning() << QString("%1 %2 %3 template data is empty!").arg(typeSerial) + .arg(label).arg(leftRightJudge); + continue; + } + + std::vector sampleVec; + std::vector newDstTarShapeVec(dstTarShapeVec); + if (srcSampleVec.first().size() < dataVec.first().size()) { - QVector reshapedSampleVec; - loadedDataReshapeForComparison(iterRighLeftMap.value(), reshapedSampleVec); - if (reshapedSampleVec.size() < 0) + QVector> newDataVec; + int minSize = srcSampleVec.first().size(); + for (int i = 0; i < dataVec.size(); ++i) + { + QVector subVec = dataVec[i]; + subVec.resize(minSize); + newDataVec.push_back(subVec); + } + if (!dataReshapeAndPretreatment(newDataVec, newDstTarShapeVec)) { - QString msg = QString("%1: %2 %3 data is empty").arg(typeSerial).arg(label).arg(leftOrRight); - qDebug() << msg; continue; } - cv::Mat tarShapeSampleMat(1, reshapedSampleVec.size(), CV_64FC1, &reshapedSampleVec[0]); - cv::Mat testSample; - tarShapeSampleMat.convertTo(testSample, CV_32FC1); - cv::Mat dstSampleTarShapeMat; - cv::medianBlur(testSample, dstSampleTarShapeMat, 5); - dstSampleTarShapeMat.convertTo(dstSampleTarShapeMat, CV_64FC1); - - std::vector sampleVec; - convertToVector(dstSampleTarShapeMat, sampleVec); - std::vector sampleNewVec(sampleVec); - std::vector dstTarShapeNewVec(dstTarShapeVec); - //qDebug() << "sampleVec size: " << sampleVec.size() << ", dstTarShapeVec size: " - // << dstTarShapeVec.size(); - //qDebug() << "reshapedSampleVec size: " << reshapedSampleVec.size() << ", reshapedTarVec size: " - // << reshapedTarVec.size(); - if (reshapedSampleVec.size() != reshapedTarVec.size()) + if (!dataReshapeAndPretreatment(srcSampleVec, sampleVec)) { - //qDebug() << "something wrong with sample vec size"; - if (sampleVec.size() > dstTarShapeVec.size()) - { - sampleNewVec.resize(dstTarShapeVec.size()); - //qDebug() << "sampleNewVec size:" << sampleNewVec.size(); - } - else if (dstTarShapeVec.size() > sampleVec.size()) - { - dstTarShapeNewVec.resize(sampleVec.size()); - //qDebug() << "dstTarShapeNewVec size: " << dstTarShapeNewVec.size(); - LB_Improved filterNew(dstTarShapeNewVec, dstTarShapeNewVec.size() / 50.0); - double dis = filterNew.test(sampleNewVec); - if (disMin > dis) { - disMin = dis; - bestLabel = label; - } - //qDebug() << "new filter v size: " << filterNew.getVSize(); - //qDebug() << "disMin: " << disMin; - } - + continue; } - else + } + else if (srcSampleVec.first().size() > dataVec.first().size()) + { + QVector> newSrcSampleVec; + int minSize = dataVec.first().size(); + for (int i = 0; i < srcSampleVec.size(); ++i) { - double dis = filter.test(sampleNewVec); - if (disMin > dis) { - disMin = dis; - bestLabel = label; - } - //qDebug() << "disMin: " << disMin; + QVector subVec = srcSampleVec[i]; + subVec.resize(minSize); + newSrcSampleVec.push_back(subVec); } -// test - - /* double val = cv::norm(dstTarShapeMat, dstSampleTarShapeMat); - if (val < globalVal) + if (!dataReshapeAndPretreatment(newSrcSampleVec, sampleVec)) { - globalVal = val; - bestLabel = label; - }*/ - + continue; + } } + else + { + if (!dataReshapeAndPretreatment(srcSampleVec, sampleVec)) + { + continue; + } + } + LB_Improved filter(newDstTarShapeVec, newDstTarShapeVec.size() / 50.0); + if (newDstTarShapeVec.size() != sampleVec.size()) + { + qWarning() << " newDstTarShapeVec.size != sampleVec.size"; + continue; + } + double dis = filter.test(sampleVec); + if (disMin > dis) { + disMin = dis; + bestLabel = label; + } + cv::Mat dstTarShapeMatNew(1, newDstTarShapeVec.size(), CV_64FC1, &newDstTarShapeVec[0]); + cv::Mat dstSampleTarShapeMatNew(1, sampleVec.size(), CV_64FC1, &sampleVec[0]); + // test + double val = cv::norm(dstTarShapeMatNew, dstSampleTarShapeMatNew); + //double val = cv::Mahalanobis(dstTarShapeMatNew, dstSampleTarShapeMatNew); + if (val < globalVal) + { + globalVal = val; + bestLabel1 = label; + } + //double val = valueComparison(reshapedSampleVec, reshapedTarVec); + //if (val < globalVal) + //{ + // globalVal = val; + // bestLabel1 = label; + //} } - //double val = valueComparison(reshapedSampleVec, reshapedTarVec); //mComparedValMapLabel[val] = label; } mComparedValMapLabel[disMin] = bestLabel; + mDisMapLabel[globalVal] = bestLabel1; qDebug() << "disMin: " << disMin << " bestLabel: " << bestLabel; + //qDebug() << "disMin1: " << globalVal << " bestLabel1: " << bestLabel1; //cv::parallel_for_(cv::Range(0, loopVec.size()), parallelInvoker(this, loopVec, labelLoopVec, reshapedTarVec)); return 1; } @@ -500,9 +523,9 @@ int dataPreProcessing::dataRepeatMatch(int mBoundary, { qDebug() << "m data:" << dataVec.size(); if (dataVec[0].size() <= 0) return -1; - int startIndex = 5; + int startIndex = 20; int step = 1; - int win = mBoundary + 5; + int win = mBoundary + startIndex; qWarning() << "I print mBoundary:" << mBoundary; qWarning() << "I print winSize:" << win; qWarning() << "I print sensor data size:" << dataVec[0].size(); @@ -527,8 +550,17 @@ int dataPreProcessing::dataRepeatMatch(int mBoundary, if (mComparedValMapLabel.size() > 0) { label = mComparedValMapLabel.first(); + qDebug() << "final bestLabel: " << mComparedValMapLabel.begin().value() + << " disMin: " << mComparedValMapLabel.begin().key(); } - + //int label1 = -1; + //if (mDisMapLabel.size() > 0) + //{ + // label1 = mDisMapLabel.first(); + // qDebug() << "final bestLabel1: " << mDisMapLabel.begin().value() + // << " disMin1: " << mDisMapLabel.begin().key(); + //} + mDisMapLabel.clear(); mComparedValMapLabel.clear(); return label; } @@ -537,7 +569,7 @@ int dataPreProcessing::dataRepeatMatch(QString typeSerial, QString leftRightJudg { if (m_comDecData.size() <= 0) { - qDebug() << "com size"; + qDebug() << "num com size is empty"; return -1; } diff --git a/src/numStatisticAlgo/dataPreprocessing.h b/src/numStatisticAlgo/dataPreprocessing.h index 7ff9e68..87234c9 100644 --- a/src/numStatisticAlgo/dataPreprocessing.h +++ b/src/numStatisticAlgo/dataPreprocessing.h @@ -37,7 +37,7 @@ public: dataPreProcessing(); ~dataPreProcessing(); public: - using QStandardnormFileMapLabelSsample = + using QStandardnormFileMapLabelSample = QMap>>>>; void setRawMat(const cv::Mat& rawImage) { rawMat = rawImage; @@ -58,12 +58,12 @@ public: // new version bool saveWinSize2File(int winSize); - QStandardnormFileMapLabelSsample getDataStructure() { - return m_StandardnormFileMapLabelSsample; + QStandardnormFileMapLabelSample getDataStructure() { + return m_StandardnormFileMapLabelSample; } - void setDataStructure(const QStandardnormFileMapLabelSsample data) + void setDataStructure(const QStandardnormFileMapLabelSample data) { - m_StandardnormFileMapLabelSsample = data; + m_StandardnormFileMapLabelSample = data; } bool qstring2stdString(const QString& str, std::string &stdStr); bool creatNumFolder(const QString& typeStr); @@ -96,6 +96,7 @@ public: void loadedDataReshapeForComparison(const QVector> & loadedData, QVector& reshapedData); double valueComparison(QVector sampleVec, const QVector tarVec); QMap mComparedValMapLabel; + QMap mDisMapLabel; void setPredictionDataFromComm(QVector> _comData) { m_comDecData = _comData; } @@ -156,7 +157,7 @@ private: QVector> m_comDecData; QString m_szSide; QVector m_normAndabnormDir; - QStandardnormFileMapLabelSsample m_StandardnormFileMapLabelSsample; + QStandardnormFileMapLabelSample m_StandardnormFileMapLabelSample; QString stationName; }; diff --git a/tpvs17/SmokeBoxIdentification/SmokeBoxIdentification.ui b/tpvs17/SmokeBoxIdentification/SmokeBoxIdentification.ui index f8e1fd8..99dd599 100644 --- a/tpvs17/SmokeBoxIdentification/SmokeBoxIdentification.ui +++ b/tpvs17/SmokeBoxIdentification/SmokeBoxIdentification.ui @@ -6,10 +6,16 @@ 0 0 - 682 - 467 + 735 + 512 + + + 735 + 512 + + SmokeBoxIdentification @@ -19,7 +25,7 @@ 0 0 - 682 + 735 34