1、修改相机打开逻辑,支持其中一个相机异常情况下能打开其他相机。2、修复图片旋转180度错误的问题。3、优化数量匹配算法,修改使数量模板和当前数据量一致的逻辑。

LanXin3D
QuShuailong 2 years ago
parent 9288237f10
commit c6d7805f0b

@ -5,6 +5,7 @@ ADModule::ADModule(QObject *parent): QObject(parent)
,m_mutex(QMutex::Recursive)
{
qRegisterMetaType<QVector<QVector<double>>>("QVector<QVector<double>>");
qRegisterMetaType<QVector<QString>>("QVector<QString>");
m_vecFmat = std::make_shared<matGroup>();
m_pThread = new QThread();
moveToThread(m_pThread);

@ -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
{
qWarning() << "Open camera failed";
return false;
QStringList serialNumberList = m_mapSNIndex.keys();
for each (const QString& serialNumber in serialNumberList)
{
if (!openCamera(serialNumber))
{
qWarning() << "Open camera: " << serialNumber << " failed";
continue;
}
else {
if (!startCamera(serialNumberList))
else
{
qWarning() << "Start camera failed";
return false;
qDebug() << "Open camera: " << serialNumber << " successed";
if (!startCamera(serialNumber))
{
qWarning() << "Start camera: " << serialNumber << " failed";
continue;
}
else {
if (!setSoftwareTrigger(serialNumberList))
else
{
qDebug() << "Start camera: " << serialNumber << " successed";
if (!setSoftwareTrigger(serialNumber))
{
qWarning() << "Set camera: " << serialNumber << " software trigger failed";
}
else
{
qWarning() << "Set software trigger failed";
qDebug() << "Set camera: " << serialNumber << " software trigger successed";
}
}
}
}
@ -212,10 +223,12 @@ bool CameraControl::loadCamera(const QList<CameraInfo>& 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<ImageInfo> vecImageInfo(len);
QVector<ImageInfo> 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:

@ -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);

@ -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)

@ -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))
{

@ -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;

@ -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<int> dataPreProcessing::modelPrediction()
return finalLabelVec;
}
int dataPreProcessing::modelPrediction(const QVector<QVector<double>> vec, QString typeSerial, QString leftRightJudge)
int dataPreProcessing::modelPrediction(const QVector<QVector<double>> dataVec, QString typeSerial, QString leftRightJudge)
{
if (leftRightJudge.isEmpty())
{
@ -358,137 +357,161 @@ int dataPreProcessing::modelPrediction(const QVector<QVector<double>> vec, QStri
}
//mComparedValMapLabel.clear();
if (vec.size() <= 0)
if (dataVec.size() <= 0)
{
qDebug() << "model vec is empty";
return -1;
}
QMultiMap<int, QMultiMap<QString, QVector<QVector<double>>>> typeTarLabelMapSampleVec = m_StandardnormFileMapLabelSsample[typeSerial];
QMultiMap<int, QMultiMap<QString, QVector<QVector<double>>>> typeTarLabelMapSampleVec = m_StandardnormFileMapLabelSample[typeSerial];
if (typeTarLabelMapSampleVec.size() == 0)
{
qDebug() << "typeTarsize";
qDebug() << typeSerial << " do not have template file";
return -1;
}
auto convertToVector = [&](const cv::Mat& m, std::vector<double>& v) {
v.clear();
double *p = (double*)m.row(0).data;
for (int i = 0; i < m.cols; i++)
{
v.push_back(p[i]);
}
};
auto dataReshapeAndPretreatment = [&](const QVector<QVector<double>>& srcVec, std::vector<double>& reshapedVec)->bool
{
QVector<double> reshapedTarVec;
loadedDataReshapeForComparison(vec, reshapedTarVec);
loadedDataReshapeForComparison(srcVec, reshapedTarVec);
if (reshapedTarVec.size() <= 0)
{
qDebug() << "reshape";
return -1;
qDebug() << "reshapedTarVec is empty";
return false;
}
qDebug() << "tarReshapedSize:" << reshapedTarVec.size();
//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<double> dstTarShapeVec;
auto convertToVector = [&](const cv::Mat& m, std::vector<double>& v) {
double *p = (double*)m.row(0).data;
for (int i = 0; i < m.cols; i++)
if (!dataReshapeAndPretreatment(dataVec, dstTarShapeVec))
{
v.push_back(p[i]);
return -1;
}
};
convertToVector(dstTarShapeMat, dstTarShapeVec);
LB_Improved filter(dstTarShapeVec, reshapedTarVec.size() / 50.0);
qDebug() << "filter v size: " << filter.getVSize();
//LB_Improved filter(dstTarShapeVec, dstTarShapeVec.size() / 50.0);
//qDebug() << "filter v size: " << filter.getVSize();
//double bestSofar = filter.getLowestCost();
QMultiMap<int, QMultiMap<QString, QVector<QVector<double>>>>::iterator it;
QVector<QVector<QVector<double>>> loopVec;
QVector<int> 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<int, QMultiMap<QString, QVector<QVector<double>>>>::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<QString, QVector<QVector<double>>> rightLeftMapValueVec = it.value();
QMultiMap<QString, QVector<QVector<double>>>::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<QVector<double>>& srcSampleVec : rightLeftMapValueVec.values(leftRightJudge))
{
QVector<double> reshapedSampleVec;
loadedDataReshapeForComparison(iterRighLeftMap.value(), reshapedSampleVec);
if (reshapedSampleVec.size() < 0)
if (srcSampleVec.size() <= 0)
{
QString msg = QString("%1: %2 %3 data is empty").arg(typeSerial).arg(label).arg(leftOrRight);
qDebug() << msg;
qWarning() << QString("%1 %2 %3 template data is empty!").arg(typeSerial)
.arg(label).arg(leftRightJudge);
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<double> sampleVec;
convertToVector(dstSampleTarShapeMat, sampleVec);
std::vector<double> sampleNewVec(sampleVec);
std::vector<double> 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())
{
//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;
std::vector<double> newDstTarShapeVec(dstTarShapeVec);
if (srcSampleVec.first().size() < dataVec.first().size())
{
QVector<QVector<double>> newDataVec;
int minSize = srcSampleVec.first().size();
for (int i = 0; i < dataVec.size(); ++i)
{
QVector<double> subVec = dataVec[i];
subVec.resize(minSize);
newDataVec.push_back(subVec);
}
//qDebug() << "new filter v size: " << filterNew.getVSize();
//qDebug() << "disMin: " << disMin;
if (!dataReshapeAndPretreatment(newDataVec, newDstTarShapeVec))
{
continue;
}
if (!dataReshapeAndPretreatment(srcSampleVec, sampleVec))
{
continue;
}
}
else if (srcSampleVec.first().size() > dataVec.first().size())
{
QVector<QVector<double>> newSrcSampleVec;
int minSize = dataVec.first().size();
for (int i = 0; i < srcSampleVec.size(); ++i)
{
QVector<double> subVec = srcSampleVec[i];
subVec.resize(minSize);
newSrcSampleVec.push_back(subVec);
}
if (!dataReshapeAndPretreatment(newSrcSampleVec, sampleVec))
{
continue;
}
}
else
{
double dis = filter.test(sampleNewVec);
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;
}
//qDebug() << "disMin: " << disMin;
}
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(dstTarShapeMat, dstSampleTarShapeMat);
double val = cv::norm(dstTarShapeMatNew, dstSampleTarShapeMatNew);
//double val = cv::Mahalanobis(dstTarShapeMatNew, dstSampleTarShapeMatNew);
if (val < globalVal)
{
globalVal = val;
bestLabel = label;
}*/
}
bestLabel1 = label;
}
//double val = valueComparison(reshapedSampleVec, reshapedTarVec);
//if (val < globalVal)
//{
// globalVal = val;
// bestLabel1 = label;
//}
}
//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;
}

@ -37,7 +37,7 @@ public:
dataPreProcessing();
~dataPreProcessing();
public:
using QStandardnormFileMapLabelSsample =
using QStandardnormFileMapLabelSample =
QMap<QString, QMultiMap <int, QMultiMap<QString, QVector<QVector<double>>>>>;
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<QVector<double>> & loadedData, QVector<double>& reshapedData);
double valueComparison(QVector<double> sampleVec, const QVector<double> tarVec);
QMap<double, int> mComparedValMapLabel;
QMap<double, int> mDisMapLabel;
void setPredictionDataFromComm(QVector<QVector<double>> _comData) {
m_comDecData = _comData;
}
@ -156,7 +157,7 @@ private:
QVector<QVector<double>> m_comDecData;
QString m_szSide;
QVector<QString> m_normAndabnormDir;
QStandardnormFileMapLabelSsample m_StandardnormFileMapLabelSsample;
QStandardnormFileMapLabelSample m_StandardnormFileMapLabelSample;
QString stationName;
};

@ -6,10 +6,16 @@
<rect>
<x>0</x>
<y>0</y>
<width>682</width>
<height>467</height>
<width>735</width>
<height>512</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>735</width>
<height>512</height>
</size>
</property>
<property name="windowTitle">
<string>SmokeBoxIdentification</string>
</property>
@ -19,7 +25,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>682</width>
<width>735</width>
<height>34</height>
</rect>
</property>

Loading…
Cancel
Save