generated from zhangwei/Matrixai
939 lines
31 KiB
C++
939 lines
31 KiB
C++
#include "SelectBestEngine.h"
|
||
|
||
using namespace ai_matrix;
|
||
|
||
SelectBestEngine::SelectBestEngine() {}
|
||
|
||
SelectBestEngine::~SelectBestEngine() {}
|
||
|
||
APP_ERROR SelectBestEngine::Init()
|
||
{
|
||
strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
|
||
strPort1_ = engineName_ + "_" + std::to_string(engineId_) + "_1";
|
||
|
||
this->baseConfig_ = Config::getins()->getBaseConfig();
|
||
this->dataSourceConfig_ = Config::getins()->getDataSourceConfig();
|
||
this->identifyConfig_ = Config::getins()->getIdentifyConfig();
|
||
|
||
InitParam();
|
||
LogInfo << "SelectBestEngine Init ok";
|
||
return APP_ERR_OK;
|
||
}
|
||
|
||
APP_ERROR SelectBestEngine::DeInit()
|
||
{
|
||
LogInfo << "SelectBestEngine DeInit ok";
|
||
return APP_ERR_OK;
|
||
}
|
||
|
||
/**
|
||
* 初始化集装箱参数信息
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::InitContainerParam()
|
||
{
|
||
this->fCenter_X_pre_ = 0.0f;
|
||
iContainerIndex_ = 0;
|
||
mapContainerInfo_.clear();
|
||
// strBestContainerImg_ = "";
|
||
// fMaxScoreSumContainer_ = 0;
|
||
|
||
mapBestContainerImg_.clear();
|
||
mapMaxScoreSumContainer_.clear();
|
||
// memset(&step1BestContainer_, 0, sizeof(step1BestContainer_));
|
||
for (auto &it : mapStep1BestContainer_)
|
||
{
|
||
it.second = step1BestContainer_;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 初始化车号参数信息
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::InitNumParam()
|
||
{
|
||
mapTrainTypeId_.clear();
|
||
mapNumInfo_.clear();
|
||
strBestNumImg_ = "";
|
||
fMaxScoreSumNum_ = 0;
|
||
memset(&step1BestNum_, 0, sizeof(step1BestNum_));
|
||
}
|
||
|
||
/**
|
||
* 初始化属性参数信息
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::InitProParam()
|
||
{
|
||
mapProInfo_.clear();
|
||
strBestProImg_ = "";
|
||
fMaxScoreSumPro_ = 0;
|
||
memset(&step1BestPro_, 0, sizeof(step1BestPro_));
|
||
}
|
||
|
||
/**
|
||
* 初始化车号参数信息(只有车结束调用)
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::InitHeadParam()
|
||
{
|
||
mapHeadInfo_.clear();
|
||
strBestHeadImg_ = "";
|
||
fMaxScoreSumHead_ = 0;
|
||
memset(&step1BestHead_, 0, sizeof(step1BestHead_));
|
||
}
|
||
|
||
/**
|
||
* 初始化参数信息
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::InitParam()
|
||
{
|
||
InitContainerParam();
|
||
InitNumParam();
|
||
InitProParam();
|
||
InitHeadParam();
|
||
}
|
||
|
||
/**
|
||
* 获取最优长度
|
||
* inParam : std::vector<TransInfo> &vecAllTransInfo 待汇总取优的结果集
|
||
* : TargetMaxLen iMaxLen 目标最大长度
|
||
* outParam: N/A
|
||
* return : 最优长度
|
||
*/
|
||
int SelectBestEngine::GetBestLength(std::vector<TransInfo> &vecAllTransInfo, TargetMaxLen iMaxLen)
|
||
{
|
||
//1.获取结果中每种长度出现的次数
|
||
std::map<int, int> mapResultSize;
|
||
for (size_t i = 0; i < vecAllTransInfo.size(); i++)
|
||
{
|
||
int iNowSize = vecAllTransInfo[i].vecValue.size();
|
||
if (mapResultSize.find(iNowSize) != mapResultSize.end())
|
||
{
|
||
mapResultSize[iNowSize]++;
|
||
continue;
|
||
}
|
||
mapResultSize[iNowSize] = 1;
|
||
}
|
||
|
||
int iBestLen = 0;
|
||
//2.获取最优长度
|
||
if (iSelectBestMode_ == FREQUENCY)
|
||
{
|
||
//2.1 按长度出现的频次最多,作为最优长度
|
||
int iCnt = 0;
|
||
for (auto it = mapResultSize.begin(); it != mapResultSize.end(); it++)
|
||
{
|
||
if (iCnt < it->second && it->first <= iMaxLen)
|
||
{
|
||
iCnt = it->second;
|
||
iBestLen = it->first;
|
||
}
|
||
}
|
||
}
|
||
else if (iSelectBestMode_ == LENGTH)
|
||
{
|
||
//2.2 按最长长度且不能大于最大长度,作为最优长度
|
||
for (auto rit = mapResultSize.rbegin(); rit != mapResultSize.rend(); rit++)
|
||
{
|
||
if (rit->first <= iMaxLen)
|
||
{
|
||
iBestLen = rit->first;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
return iBestLen;
|
||
}
|
||
|
||
/**
|
||
* 获取最优结果
|
||
* inParam : std::vector<TransInfo> &vecAllTransInfo 待汇总取优的结果集
|
||
* : TargetMaxLen iMaxLen 目标最大长度
|
||
* outParam: N/A
|
||
* return : 最优结果
|
||
*/
|
||
std::string SelectBestEngine::GetBest(std::vector<TransInfo> &vecAllTransInfo, TargetMaxLen iMaxLen)
|
||
{
|
||
std::string strValue = "";
|
||
if (vecAllTransInfo.size() <= 0)
|
||
{
|
||
LogInfo << "<<< --- 没数据 --- >>>";
|
||
return strValue;
|
||
}
|
||
|
||
//优先使用校验通过的数据选优。
|
||
std::vector<TransInfo> vecTransInfoTemp;
|
||
for (size_t i = 0; i < vecAllTransInfo.size(); i++)
|
||
{
|
||
TransInfo transInfo = vecAllTransInfo[i];
|
||
if (transInfo.IsChkFlag)
|
||
{
|
||
vecTransInfoTemp.emplace_back(transInfo);
|
||
}
|
||
}
|
||
if (vecTransInfoTemp.size() > 0)
|
||
{
|
||
vecAllTransInfo = vecTransInfoTemp;
|
||
} else {
|
||
// LogInfo << "--->>> 没有符合正则的??";
|
||
// 此处因车厢太脏。识别效果很差,难以与RFID识别结果融合,所以增加eles
|
||
return strValue;
|
||
}
|
||
|
||
//获取最优长度
|
||
int iBestLen = GetBestLength(vecAllTransInfo, iMaxLen);
|
||
// LogInfo << "--->>> 最优长度:" << iBestLen;
|
||
|
||
//初始化最优结果
|
||
TransInfo transInfoBest;
|
||
transInfoBest.vecScore.reserve(iBestLen);
|
||
transInfoBest.vecValue.reserve(iBestLen);
|
||
for (int i = 0; i < iBestLen; i++)
|
||
{
|
||
transInfoBest.vecScore.emplace_back(0);
|
||
transInfoBest.vecValue.emplace_back("");
|
||
}
|
||
|
||
std::map<std::string, int> mapResult; //符合最优长度的结果集 [key:结果; value:结果出现次数]
|
||
for (size_t iIndex = 0; iIndex < vecAllTransInfo.size(); iIndex++)
|
||
{
|
||
if (vecAllTransInfo[iIndex].vecValue.size() != iBestLen)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
std::string strValueTemp;
|
||
for (int j = 0; j < iBestLen; j++)
|
||
{
|
||
strValueTemp += vecAllTransInfo[iIndex].vecValue[j];
|
||
if (transInfoBest.vecScore[j] < vecAllTransInfo[iIndex].vecScore[j])
|
||
{
|
||
transInfoBest.vecScore[j] = vecAllTransInfo[iIndex].vecScore[j];
|
||
transInfoBest.vecValue[j] = vecAllTransInfo[iIndex].vecValue[j];
|
||
}
|
||
}
|
||
|
||
if (mapResult.find(strValueTemp) != mapResult.end())
|
||
{
|
||
mapResult[strValueTemp]++;
|
||
}
|
||
else
|
||
{
|
||
mapResult[strValueTemp] = 1;
|
||
}
|
||
}
|
||
|
||
//按字符最高得分汇总结果
|
||
for (size_t i = 0; i < transInfoBest.vecValue.size(); ++i)
|
||
{
|
||
strValue += transInfoBest.vecValue[i];
|
||
}
|
||
|
||
//按出现次数汇总结果
|
||
int iMaxCnt = 0;
|
||
std::string strValue2;
|
||
for (auto iter = mapResult.begin(); iter != mapResult.end(); iter++)
|
||
{
|
||
if (iter->second > iMaxCnt)
|
||
{
|
||
iMaxCnt = iter->second;
|
||
strValue2 = iter->first;
|
||
}
|
||
}
|
||
|
||
//最高得分汇总和最高次数汇总不相等,且识别同一结果次数大于等于3次,则使用按次数汇总结果。
|
||
if (strValue != strValue2)
|
||
{
|
||
// LogWarn << "engineId:" << engineId_ << " value1:" << strValue << " value2:" << strValue2
|
||
// << " not equal value2cnt:" << iMaxCnt;
|
||
if (iMaxCnt >= 3)
|
||
{
|
||
strValue = strValue2;
|
||
}
|
||
}
|
||
|
||
return strValue;
|
||
}
|
||
|
||
/**
|
||
* 集装箱数据加入到待选优集合中
|
||
* inParam : std::shared_ptr<ProcessData> pProcessData :处理帧数据
|
||
: TransSubData &transSubData :转换后子数据
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::containerAddSelectBestMap(const int iFrameId, const Step2ResultData &step2ResultData)
|
||
{
|
||
float fCenter_X = (step2ResultData.fLTX + step2ResultData.fRBX)/2;
|
||
|
||
for (const auto& transInfo : step2ResultData.vecTransInfo)
|
||
{
|
||
if (std::abs(fCenter_X - this->fCenter_X_pre_) > this->identifyConfig_.iContainerSplitFrameSpanPx && this->fCenter_X_pre_)
|
||
{
|
||
bool bSplit = true;
|
||
for (auto &it_pre : this->mapContainerInfo_[this->iContainerIndex_])
|
||
{
|
||
if (transInfo.strTmpResult == it_pre.strTmpResult)
|
||
{
|
||
bSplit = false;
|
||
break;
|
||
}
|
||
}
|
||
if (bSplit)
|
||
{
|
||
// LogDebug << "fCenter_X_pre_:" << fCenter_X_pre_
|
||
// << " fCenter_X:" << fCenter_X
|
||
// << " --- " << this->identifyConfig_.iSplitFrameSpanPx;
|
||
this->iContainerIndex_++;
|
||
}
|
||
|
||
}
|
||
// LogDebug << " Index_:" << this->iContainerIndex_
|
||
// << " info:" << transInfo.strTmpResult;
|
||
this->mapContainerInfo_[this->iContainerIndex_].emplace_back(transInfo);
|
||
this->fCenter_X_pre_ = fCenter_X;
|
||
}
|
||
|
||
//最高分和最优帧号
|
||
if (this->mapMaxScoreSumContainer_.find(this->iContainerIndex_) == this->mapMaxScoreSumContainer_.end()
|
||
|| this->mapMaxScoreSumContainer_[this->iContainerIndex_] <= step2ResultData.fSubScoreSum)
|
||
{
|
||
this->mapMaxScoreSumContainer_[this->iContainerIndex_] = step2ResultData.fSubScoreSum;
|
||
this->mapBestContainerImg_[this->iContainerIndex_] = std::to_string(iFrameId) + ".jpg";
|
||
this->mapStep1BestContainer_[this->iContainerIndex_] = step2ResultData;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 车号数据加入到待选优集合中
|
||
* inParam : std::shared_ptr<ProcessData> pProcessData :处理帧数据
|
||
: TransSubData &transSubData :转换后子数据
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::numAddSelectBestMap(const int iFrameId, const Step2ResultData &step2ResultData)
|
||
{
|
||
for (size_t i = 0; i < step2ResultData.vecTransInfo.size(); i++)
|
||
{
|
||
TransInfo transInfo = step2ResultData.vecTransInfo[i];
|
||
mapNumInfo_[transInfo.iLine].emplace_back(transInfo);
|
||
}
|
||
|
||
//记录车厢类型ID
|
||
if (mapTrainTypeId_.find(step2ResultData.iClassId) != mapTrainTypeId_.end())
|
||
{
|
||
mapTrainTypeId_[step2ResultData.iClassId]++;
|
||
}
|
||
else
|
||
{
|
||
mapTrainTypeId_[step2ResultData.iClassId] = 1;
|
||
}
|
||
|
||
//最高分和最优帧号
|
||
if (fMaxScoreSumNum_ < step2ResultData.fSubScoreSum)
|
||
{
|
||
fMaxScoreSumNum_ = step2ResultData.fSubScoreSum;
|
||
strBestNumImg_ = std::to_string(iFrameId) + ".jpg";
|
||
step1BestNum_ = step2ResultData;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 属性数据加入到待选优集合中
|
||
* inParam : std::shared_ptr<ProcessData> pProcessData :处理帧数据
|
||
: TransSubData &transSubData :转换后子数据
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::proAddSelectBestMap(const int iFrameId, const Step2ResultData &step2ResultData)
|
||
{
|
||
for (size_t i = 0; i < step2ResultData.vecTransInfo.size(); i++)
|
||
{
|
||
TransInfo transInfo = step2ResultData.vecTransInfo[i];
|
||
mapProInfo_[transInfo.iLine].emplace_back(transInfo);
|
||
}
|
||
|
||
//最高分和最优帧号
|
||
if (fMaxScoreSumPro_ < step2ResultData.fSubScoreSum)
|
||
{
|
||
fMaxScoreSumPro_ = step2ResultData.fSubScoreSum;
|
||
strBestProImg_ = std::to_string(iFrameId) + ".jpg";
|
||
step1BestPro_ = step2ResultData;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 车头数据加入到待选优集合中
|
||
* inParam : std::shared_ptr<ProcessData> pProcessData :处理帧数据
|
||
: TransSubData &transSubData :转换后子数据
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::headAddSelectBestMap(const int iFrameId, const Step2ResultData &step2ResultData)
|
||
{
|
||
// for (size_t i = 0; i < step2ResultData.vecTransInfo.size(); i++)
|
||
// {
|
||
// TransInfo transInfo = step2ResultData.vecTransInfo[i];
|
||
// this->mapHeadInfo_[transInfo.iLine].emplace_back(transInfo);
|
||
// }
|
||
//
|
||
// //记录车头大框识别次数
|
||
// auto iter = this->mapTrainTypeId_.find(0);
|
||
// if (iter == this->mapTrainTypeId_.end())
|
||
// {
|
||
// this->mapTrainTypeId_.insert(std::make_pair(0, 1));
|
||
// }
|
||
// else
|
||
// {
|
||
// iter->second++;
|
||
// }
|
||
//
|
||
// //最高分和最优帧号
|
||
// if (fMaxScoreSumHead_ < step2ResultData.fSubScoreSum)
|
||
// {
|
||
// fMaxScoreSumHead_ = step2ResultData.fSubScoreSum;
|
||
// strBestHeadImg_ = std::to_string(iFrameId) + ".jpg";
|
||
// step1BestHead_ = step2ResultData;
|
||
// }
|
||
}
|
||
|
||
/**
|
||
* 汇总车头最佳值
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
//bool SelectBestEngine::getHeadBest(std::shared_ptr<ProcessData> pProcessData)
|
||
//{
|
||
// //2.无识别车头信息,不汇总
|
||
// if (mapHeadInfo_.empty())
|
||
// {
|
||
// LogDebug << "engineId:" << engineId_ << " no head info";
|
||
// return false;
|
||
// }
|
||
//
|
||
// std::shared_ptr<VTrainInfo> pTrain = std::make_shared<VTrainInfo>();
|
||
//
|
||
// //车头型号
|
||
// if (mapHeadInfo_.find(0) != mapHeadInfo_.end())
|
||
// {
|
||
// pTrain->strTType = GetBest(mapHeadInfo_.at(0), TYPE_MAXLEN);
|
||
// }
|
||
//
|
||
// //车头编号
|
||
// if (mapHeadInfo_.find(1) != mapHeadInfo_.end())
|
||
// {
|
||
// pTrain->strTNum = GetBest(mapHeadInfo_.at(1), NUM_MAXLEN);
|
||
// }
|
||
//
|
||
// pTrain->strTNum_image = strBestHeadImg_;
|
||
// pTrain->numCoordinate.iClassId = step1BestHead_.iClassId;
|
||
// pTrain->numCoordinate.fScore = step1BestHead_.fScore;
|
||
// pTrain->numCoordinate.fLTX = step1BestHead_.fLTX;
|
||
// pTrain->numCoordinate.fLTY = step1BestHead_.fLTY;
|
||
// pTrain->numCoordinate.fRBX = step1BestHead_.fRBX;
|
||
// pTrain->numCoordinate.fRBY = step1BestHead_.fRBY;
|
||
// pTrain->numCoordinate.strClassName = "HEARD";
|
||
// pTrain->iCategory = 0; //车头
|
||
//
|
||
//
|
||
// //push端口1,车头属性
|
||
// pTrain->trainPro.iDataSource = iDataSourceHead_;
|
||
// pTrain->trainPro.bIsEnd = pProcessData->bIsEnd;
|
||
// pTrain->trainPro.strTrainDate = pProcessData->strTrainDate;
|
||
// pTrain->trainPro.strTrainName = pProcessData->strTrainName;
|
||
// pTrain->trainPro.iDirection = pProcessData->iDirection;
|
||
// pTrain->trainPro.iCarXH = 0;
|
||
//
|
||
// pTrain->strTrainDate = pTrain->trainNum.strTrainDate;
|
||
// pTrain->strTrainName = pTrain->trainNum.strTrainName;
|
||
// pTrain->iDirection = pTrain->trainNum.iDirection;
|
||
// pTrain->iDataSource = pTrain->trainNum.iDataSource >= pTrain->trainPro.iDataSource ? pTrain->trainNum.iDataSource : pTrain->trainPro.iDataSource;
|
||
//
|
||
// LogInfo << "\n ---汇总结果--- \n"
|
||
// << "日期时间: " << pTrain->strTrainDate << " " << pTrain->strTrainName << "\n"
|
||
// << "车厢序号: " << pTrain->iCarXH << "\n"
|
||
// << "车型Id: " << pTrain->trainNum.iTrainTypeId << "\n"
|
||
// << "车型: " << pTrain->trainNum.strTrainType << "\n"
|
||
// << "车号: " << pTrain->trainNum.strTrainNum << "\n"
|
||
// << "载重: " << pTrain->trainPro.strLoad << "\n"
|
||
// << "自重: " << pTrain->trainPro.strSelf << "\n"
|
||
// << "容积: " << pTrain->trainPro.strVolume << "\n"
|
||
// << "换长: " << pTrain->trainPro.strChange << "\n"
|
||
// << "容量记表: " << pTrain->trainPro.strVolumeSurface << "\n"
|
||
// << "编号图片: " << pTrain->trainNum.strBestImg << "\n"
|
||
// << "属性图片: " << pTrain->trainPro.strBestImg << "\n"
|
||
// << "行驶方向: " << pTrain->iDirection << "\n"
|
||
// << " ---汇总结果 END--- ";
|
||
//
|
||
// //拷贝最优图片到最优路径下
|
||
// CopyBestImgToBestPath(pTrain);
|
||
//
|
||
// std::shared_ptr<Train> pTrainToCsv = std::make_shared<Train>();
|
||
// *pTrainToCsv = *pTrain;
|
||
// PushData(strPort0_, pTrain);
|
||
// PushData(strPort1_, pTrainToCsv);
|
||
// return true;
|
||
//}
|
||
|
||
/**
|
||
* 汇总集装箱最佳值
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::getContainerBest(VTrainInfo &train, std::shared_ptr<VStep2OutputData> pVStep2OutputData)
|
||
{
|
||
int i = 0;
|
||
for (auto &it : mapContainerInfo_)
|
||
{
|
||
VContainer vContainer;
|
||
vContainer.strContainerNo = GetBest(it.second, CONTAINER_MAXLEN);
|
||
vContainer.strImg = this->mapBestContainerImg_[i];
|
||
VCoordinate vCoordinate;
|
||
vCoordinate.iClassId = this->mapStep1BestContainer_[i].iClassId;
|
||
vCoordinate.fLTX = this->mapStep1BestContainer_[i].fLTX;
|
||
vCoordinate.fLTY = this->mapStep1BestContainer_[i].fLTY;
|
||
vCoordinate.fRBX = this->mapStep1BestContainer_[i].fRBX;
|
||
vCoordinate.fRBY = this->mapStep1BestContainer_[i].fRBY;
|
||
vCoordinate.fScore = this->mapStep1BestContainer_[i].fScore;
|
||
vCoordinate.strClassName = "CONTAINER";
|
||
|
||
vContainer.coordinate = vCoordinate;
|
||
train.vecContainer.emplace_back(vContainer);
|
||
++i;
|
||
}
|
||
//初始化车号信息
|
||
InitContainerParam();
|
||
}
|
||
|
||
/**
|
||
* 汇总车号最佳值
|
||
* inParam : std::shared_ptr<ProcessData> pProcessData
|
||
* outParam: TrainNum &trainNum
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::getNumBest(VTrainInfo &train, std::shared_ptr<VStep2OutputData> pVStep2OutputData)
|
||
{
|
||
//车型
|
||
if (mapNumInfo_.find(0) != mapNumInfo_.end())
|
||
{
|
||
train.strTType = GetBest(mapNumInfo_.at(0), TYPE_MAXLEN);
|
||
// LogInfo << "-->>> " << trainNum.strTrainType;
|
||
}
|
||
//编号
|
||
if (mapNumInfo_.find(1) != mapNumInfo_.end())
|
||
{
|
||
train.strTNum = GetBest(mapNumInfo_.at(1), NUM_MAXLEN);
|
||
}
|
||
|
||
//按出现次数选车厢类型ID
|
||
int iMaxCnt = 0;
|
||
for (auto iter = mapTrainTypeId_.begin(); iter != mapTrainTypeId_.end(); iter++)
|
||
{
|
||
if (iter->second > iMaxCnt)
|
||
{
|
||
iMaxCnt = iter->second;
|
||
train.iCategory = iter->first;
|
||
}
|
||
}
|
||
|
||
train.strTNum_image = strBestNumImg_;
|
||
train.numCoordinate.iClassId = step1BestNum_.iClassId;
|
||
train.numCoordinate.fScore = step1BestNum_.fScore;
|
||
train.numCoordinate.fLTX = step1BestNum_.fLTX;
|
||
train.numCoordinate.fLTY = step1BestNum_.fLTY;
|
||
train.numCoordinate.fRBX = step1BestNum_.fRBX;
|
||
train.numCoordinate.fRBY = step1BestNum_.fRBY;
|
||
train.numCoordinate.strClassName = "NUM";
|
||
//初始化车号信息
|
||
InitNumParam();
|
||
}
|
||
|
||
/**
|
||
* 汇总属性最佳值
|
||
* inParam : std::shared_ptr<ProcessData> pProcessData
|
||
* outParam: TrainPro &trainPro
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::getProBest(VTrainInfo &train, std::shared_ptr<VStep2OutputData> pVStep2OutputData)
|
||
{
|
||
//载重
|
||
if (mapProInfo_.find(0) != mapProInfo_.end())
|
||
{
|
||
train.strTLoadWeight = GetBest(mapProInfo_.at(0), LOAD_MAXLEN);
|
||
}
|
||
//自重
|
||
if (mapProInfo_.find(1) != mapProInfo_.end())
|
||
{
|
||
train.strTTareWeight = GetBest(mapProInfo_.at(1), SELF_MAXLEN);
|
||
if (train.strTTareWeight.length() == 3)
|
||
{
|
||
train.strTTareWeight = train.strTTareWeight.substr(0, 2) + "." + train.strTTareWeight.substr(2, 1);
|
||
}
|
||
}
|
||
//容积
|
||
if (mapProInfo_.find(2) != mapProInfo_.end())
|
||
{
|
||
train.strTVolume = GetBest(mapProInfo_.at(2), VOLUME_MAXLEN);
|
||
//棚车容器一般为1开头得三位整数,因此不需要额外补小数点处理。
|
||
if (train.strTVolume.length() == 3 && train.strTVolume.substr(0, 1) != "1")
|
||
{
|
||
train.strTVolume = train.strTVolume.substr(0, 2) + "." + train.strTVolume.substr(2, 1);
|
||
}
|
||
}
|
||
|
||
//换长
|
||
if (mapProInfo_.find(3) != mapProInfo_.end())
|
||
{
|
||
train.strTChangeLen = GetBest(mapProInfo_.at(3), CHANGE_MAXLEN);
|
||
if (train.strTChangeLen.length() == 1)
|
||
{
|
||
if (train.strTChangeLen.at(0) != '1')
|
||
{
|
||
train.strTChangeLen = "1." + train.strTChangeLen;
|
||
}
|
||
}
|
||
else if (train.strTChangeLen.length() == 2)
|
||
{
|
||
train.strTChangeLen = train.strTChangeLen.substr(0, 1) + "." + train.strTChangeLen.substr(1, 1);
|
||
}
|
||
}
|
||
|
||
train.strTPro_image = strBestProImg_;
|
||
train.proCoordinate.iClassId = step1BestPro_.iClassId;
|
||
train.proCoordinate.fScore = step1BestPro_.fScore;
|
||
train.proCoordinate.fLTX = step1BestPro_.fLTX;
|
||
train.proCoordinate.fLTY = step1BestPro_.fLTY;
|
||
train.proCoordinate.fRBX = step1BestPro_.fRBX;
|
||
train.proCoordinate.fRBY = step1BestPro_.fRBY;
|
||
train.proCoordinate.strClassName = "PRO";
|
||
//初始化属性信息
|
||
InitProParam();
|
||
}
|
||
|
||
/**
|
||
* 拷贝最优图片到最优路径下
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::copyBestImgToBestPath(const VTrainInfo &train)
|
||
{
|
||
std::string strSrc = this->baseConfig_.strDebugResultPath + "/" + train.strTrainDate + "/" + StringUtil::getins()->replace_all_distinct(train.strTrainTime, ":", "-") + "/jpg/";
|
||
std::string strDes = this->baseConfig_.strResultPath + "/" + train.strTrainDate + "/" + StringUtil::getins()->replace_all_distinct(train.strTrainTime, ":", "-") + "/";
|
||
if (!FileUtil::getins()->createDirPath(strDes))
|
||
{
|
||
LogError << "engineId:" << engineId_ << " CreateDirPath err strDes:" << strDes;
|
||
return;
|
||
}
|
||
|
||
//拷贝最优图片到最优路径下
|
||
if (!train.strTNum_image.empty())
|
||
{
|
||
FileUtil::getins()->copyFile(strSrc + train.strTNum_image, strDes + train.strTNum_image);
|
||
}
|
||
if (!train.strTPro_image.empty())
|
||
{
|
||
FileUtil::getins()->copyFile(strSrc + train.strTPro_image, strDes + train.strTPro_image);
|
||
}
|
||
|
||
for (auto &it_container : train.vecContainer)
|
||
{
|
||
FileUtil::getins()->copyFile(strSrc + it_container.strImg, strDes + it_container.strImg);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 汇总车厢最佳值
|
||
* inParam : N/A
|
||
* outParam: N/A
|
||
* return : N/A
|
||
*/
|
||
void SelectBestEngine::getTrainBest(std::shared_ptr<VStep2OutputData> pVStep2OutputData)
|
||
{
|
||
VTrainInfo train;
|
||
//1.汇总车号信息
|
||
this->getNumBest(train, pVStep2OutputData);
|
||
|
||
//2.汇总属性信息
|
||
this->getProBest(train, pVStep2OutputData);
|
||
|
||
//3.汇总集装箱信息
|
||
this->getContainerBest(train, pVStep2OutputData);
|
||
|
||
// 修正载重
|
||
for (std::map<std::string, std::string>::iterator it =
|
||
this->mapLoad_.begin();
|
||
it != this->mapLoad_.end(); ++it) {
|
||
if (train.strTType.find(it->first) !=
|
||
std::string::npos) {
|
||
train.strTLoadWeight = it->second;
|
||
}
|
||
}
|
||
// 修正容积
|
||
if (train.iCategory == 6)
|
||
{
|
||
train.strTVolume = "";
|
||
}
|
||
|
||
train.strTrainDate = pVStep2OutputData->strTrainDate;
|
||
train.strTrainTime = pVStep2OutputData->strTrainTime;
|
||
train.strTrackName = this->baseConfig_.strTrackName;
|
||
train.iCarOrder = this->partionInfo_.iTrainIndex;
|
||
train.iStartFrame = this->partionInfo_.iStartframe;
|
||
train.iEndFrame = this->partionInfo_.iEndframe;
|
||
|
||
std::string strFirameJsonDir = this->baseConfig_.strDebugResultPath + "/"
|
||
+ pVStep2OutputData->strTrainDate + "/"
|
||
+ StringUtil::getins()->replace_all_distinct(pVStep2OutputData->strTrainTime, ":", "-") + "/"
|
||
+ "jpg/";
|
||
|
||
std::string strStartFramePath = strFirameJsonDir + std::to_string(train.iStartFrame) + ".json";
|
||
|
||
Json::Value jvStartFrameJson;
|
||
if (FileUtil::getins()->readJsonInfo(jvStartFrameJson, strStartFramePath))
|
||
{
|
||
// train.iTDirection = jvStartFrameJson.isMember("direction") ? jvStartFrameJson["direction"].asInt() : 0;
|
||
train.strStartTime = jvStartFrameJson.isMember("timeStamp") ? jvStartFrameJson["timeStamp"].asString() : "";
|
||
}
|
||
|
||
std::string strEndFramePath = strFirameJsonDir + std::to_string(train.iEndFrame) + ".json";
|
||
|
||
Json::Value jvEndFrameJson;
|
||
if (FileUtil::getins()->readJsonInfo(jvEndFrameJson, strEndFramePath))
|
||
{
|
||
train.iTDirection = jvStartFrameJson.isMember("direction") ? jvStartFrameJson["direction"].asInt() : 0;
|
||
train.bIsTheLast = jvEndFrameJson.isMember("isEnd") ? jvEndFrameJson["isEnd"].asBool() : false;
|
||
train.strEndTime = jvEndFrameJson.isMember("timeStamp") ? jvEndFrameJson["timeStamp"].asString() : "";
|
||
}
|
||
|
||
int iFrameFirst = 0;
|
||
switch (this->dataSourceConfig_.iDirection)
|
||
{
|
||
case DIRECTION_LEFT:
|
||
iFrameFirst = this->dataSourceConfig_.iLeftFirst;
|
||
break;
|
||
case DIRECTION_RIGHT:
|
||
iFrameFirst = this->dataSourceConfig_.iRightFirst;
|
||
break;
|
||
case DIRECTION_UNKNOWN:
|
||
default:
|
||
break;
|
||
}
|
||
|
||
if (train.strTNum_image.empty())
|
||
{
|
||
switch (iFrameFirst)
|
||
{
|
||
case LEFT_RUN_AND_NUM_FIRST:
|
||
case RIGHT_RUN_AND_NUM_FIRST:
|
||
train.strTNum_image = std::to_string(train.iStartFrame) + ".jpg";
|
||
break;
|
||
case LEFT_RUN_AND_PRO_FIRST:
|
||
case RIGHT_RUN_AND_PRO_FIRST:
|
||
train.strTNum_image = std::to_string(train.iEndFrame) + ".jpg";
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (train.strTPro_image.empty())
|
||
{
|
||
switch (iFrameFirst)
|
||
{
|
||
case LEFT_RUN_AND_PRO_FIRST:
|
||
case RIGHT_RUN_AND_PRO_FIRST:
|
||
train.strTPro_image = std::to_string(train.iStartFrame) + ".jpg";
|
||
break;
|
||
case LEFT_RUN_AND_NUM_FIRST:
|
||
case RIGHT_RUN_AND_NUM_FIRST:
|
||
train.strTPro_image = std::to_string(train.iEndFrame) + ".jpg";
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
std::string strContainer, strContainerImage;
|
||
|
||
for (int i = 0; i < train.vecContainer.size(); i++)
|
||
{
|
||
strContainer += train.vecContainer[i].strContainerNo;
|
||
strContainerImage += train.vecContainer[i].strImg;
|
||
if (i < train.vecContainer.size() - 1)
|
||
{
|
||
strContainer += ",";
|
||
strContainerImage += ",";
|
||
}
|
||
|
||
}
|
||
|
||
LogInfo << "\n ---汇总结果--- \n"
|
||
<< "日期时间: " << train.strTrainDate << " " << train.strTrainTime << "\n"
|
||
<< "股道: " << train.strTrackName << "\n"
|
||
<< "行驶方向: " << train.iTDirection << "\n"
|
||
<< "车厢序号: " << train.iCarOrder << "\n"
|
||
<< "车型Id: " << train.iCategory << "\n"
|
||
<< "车型: " << train.strTType << "\n"
|
||
<< "车号: " << train.strTNum << "\n"
|
||
<< "载重: " << train.strTLoadWeight << "\n"
|
||
<< "自重: " << train.strTTareWeight << "\n"
|
||
<< "容积: " << train.strTVolume << "\n"
|
||
<< "换长: " << train.strTChangeLen << "\n"
|
||
<< "编号图片: " << train.strTNum_image << "\n"
|
||
<< "属性图片: " << train.strTPro_image << "\n"
|
||
<< "车节开始时间: " << train.strStartTime << "\n"
|
||
<< "车节结束时间: " << train.strEndTime << "\n"
|
||
<< "集装箱:" << strContainer << "\n"
|
||
<< "集装箱图片:" << strContainerImage << "\n"
|
||
<< " ---汇总结果 END--- ";
|
||
|
||
//拷贝最优图片到最优路径下
|
||
this->copyBestImgToBestPath(train);
|
||
|
||
std::shared_ptr<VTrainInfo> pTrain = std::make_shared<VTrainInfo>();
|
||
*pTrain = train;
|
||
outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pTrain));
|
||
}
|
||
|
||
bool SelectBestEngine::readPartionJson(std::string &strJsonPath, PartionInfo &partionInfo)
|
||
{
|
||
if (access(strJsonPath.c_str(), F_OK) != 0)
|
||
{
|
||
LogWarn << "文件:" << strJsonPath << " 不存在";
|
||
return false;
|
||
}
|
||
|
||
Json::Value jvInfo;
|
||
if (!FileUtil::getins()->readJsonInfo(jvInfo, strJsonPath))
|
||
{
|
||
LogError << "读取json文件失败:" << strJsonPath;
|
||
return false;
|
||
}
|
||
|
||
partionInfo.iStartframe = jvInfo["startFrame"].asInt();
|
||
partionInfo.iEndframe = jvInfo["endFrame"].asInt();
|
||
partionInfo.iTrainIndex = jvInfo["trainIndex"].asInt();
|
||
return true;
|
||
}
|
||
|
||
APP_ERROR SelectBestEngine::Process()
|
||
{
|
||
int iRet = APP_ERR_OK;
|
||
while (!isStop_)
|
||
{
|
||
//pop端口0
|
||
std::shared_ptr<void> pVoidData0 = nullptr;
|
||
iRet = inputQueMap_[strPort0_]->pop(pVoidData0);
|
||
if (nullptr == pVoidData0)
|
||
{
|
||
usleep(1000);
|
||
continue;
|
||
}
|
||
|
||
std::shared_ptr<VStep2OutputData> pVStep2OutputData = std::static_pointer_cast<VStep2OutputData>(pVoidData0);
|
||
|
||
if (!this->partionInfo_.iTrainIndex)
|
||
{
|
||
std::string strJsonPath = this->baseConfig_.strDebugResultPath + "/"
|
||
+ pVStep2OutputData->strTrainDate + "/"
|
||
+ StringUtil::getins()->replace_all_distinct(pVStep2OutputData->strTrainTime, ":", "-") + "/"
|
||
+ "1.json";
|
||
if (!this->readPartionJson(strJsonPath, this->partionInfo_)) continue;
|
||
}
|
||
|
||
if (!this->partionInfo_.iTrainIndex) continue;
|
||
|
||
for (size_t i = 0; i < pVStep2OutputData->vecStep2ResultData.size(); i++)
|
||
{
|
||
Step2ResultData step2ResultData = pVStep2OutputData->vecStep2ResultData[i];
|
||
|
||
if (step2ResultData.iTrainIndex != this->partionInfo_.iTrainIndex) continue;
|
||
|
||
switch (step2ResultData.iClassId)
|
||
{
|
||
case TRAIN_HEAD:
|
||
//车头加入待汇总集合中
|
||
this->headAddSelectBestMap(pVStep2OutputData->iFrameId, step2ResultData);
|
||
break;
|
||
case TRAIN_PRO:
|
||
// if (step2ResultData.iTrainIndex != this->mapTrainIndex_["PRO"])
|
||
// {
|
||
//
|
||
// }
|
||
//属性加入待汇总集合中
|
||
this->proAddSelectBestMap(pVStep2OutputData->iFrameId, step2ResultData);
|
||
break;
|
||
case K_TRAIN_NUM ... NX_TRAIN_NUM:
|
||
//车号加入待汇总集合中
|
||
this->numAddSelectBestMap(pVStep2OutputData->iFrameId, step2ResultData);
|
||
break;
|
||
case CONTAINERNUM:
|
||
this->containerAddSelectBestMap(pVStep2OutputData->iFrameId, step2ResultData);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
}
|
||
// LogDebug << "-->" << this->partionInfo_.iEndframe << "--" << pVStep2OutputData->iFrameId;
|
||
|
||
if (pVStep2OutputData->iFrameId > this->partionInfo_.iEndframe || pVStep2OutputData->bIsEnd)
|
||
{
|
||
bool bHeadFlag = false;
|
||
//第一节或最后一节汇总车头
|
||
//(注:针对2个车头的场景,除判断第一节和最后一节外,额外增加车头的次数,当车头大框次数大于3次时,则认为车头。因此后续第一个节和最后一节的条件可以删除)
|
||
// if (this->partionInfo_.iTrainIndex == 1 || pVStep2OutputData->bIsEnd ||
|
||
// (mapTrainTypeId_.find(0) != mapTrainTypeId_.end() && (mapTrainTypeId_[0] >= 2 || mapTrainTypeId_.size() == 1)))
|
||
// {
|
||
// bHeadFlag = this->getHeadBest(pVStep2OutputData);
|
||
// }
|
||
|
||
//汇总车厢 车号和属性
|
||
if (!bHeadFlag)
|
||
{
|
||
this->getTrainBest(pVStep2OutputData);
|
||
}
|
||
|
||
if (!pVStep2OutputData->bIsEnd)
|
||
{
|
||
std::string strJsonPath = this->baseConfig_.strDebugResultPath + "/"
|
||
+ pVStep2OutputData->strTrainDate + "/"
|
||
+ StringUtil::getins()->replace_all_distinct(pVStep2OutputData->strTrainTime, ":", "-") + "/"
|
||
+ std::to_string(this->partionInfo_.iTrainIndex + 1)
|
||
+ ".json";
|
||
this->readPartionJson(strJsonPath, this->partionInfo_);
|
||
|
||
}
|
||
else
|
||
{
|
||
this->partionInfo_.iTrainIndex = 0;
|
||
// 当节车厢出现的开始帧帧号
|
||
this->partionInfo_.iStartframe = 0;
|
||
// 当节车厢出现的结束帧帧号
|
||
this->partionInfo_.iEndframe = 0;
|
||
}
|
||
|
||
//初始化参数变量
|
||
InitParam();
|
||
}
|
||
}
|
||
return APP_ERR_OK;
|
||
}
|