VCarContainer/engine/SaveDebugImageEngine/SaveDebugImageEngine.cpp

134 lines
5.2 KiB
C++
Raw Normal View History

2024-11-24 00:58:06 +00:00
//
// Created by nvidia on 24-10-14.
//
#include "SaveDebugImageEngine.h"
using namespace ai_matrix;
SaveDebugImageEngine::SaveDebugImageEngine() {}
SaveDebugImageEngine::~SaveDebugImageEngine() {}
APP_ERROR SaveDebugImageEngine::Init()
{
strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
this->baseConfig_ = Config::getins()->getBaseConfig();
vecCompressionParams_.push_back(cv::IMWRITE_JPEG_QUALITY); //选择jpeg
vecCompressionParams_.push_back(this->iPicQuality_); //图片质量
LogInfo << "SaveDebugImageEngine Init ok";
return APP_ERR_OK;
}
APP_ERROR SaveDebugImageEngine::DeInit()
{
LogInfo << "SaveDebugImageEngine DeInit ok";
return APP_ERR_OK;
}
APP_ERROR SaveDebugImageEngine::Process()
{
int iRet = APP_ERR_OK;
while (!isStop_)
{
std::shared_ptr<void> pvoidd = nullptr;
inputQueMap_[strPort0_]->pop(pvoidd);
if (nullptr == pvoidd)
{
usleep(1000);
continue;
}
if (!this->baseConfig_.bTestModel)
{
usleep(1000);
continue;
}
2024-11-24 00:58:06 +00:00
std::shared_ptr<VStep2OutputData> pVStep2OutputData = std::static_pointer_cast<VStep2OutputData>(pvoidd);
std::string strDataDir = this->baseConfig_.strDebugResultPath + "/"
+ pVStep2OutputData->strDetectDate + "/"
+ StringUtil::getins()->replace_all_distinct(pVStep2OutputData->strDetectTime, ":", "-")
+ "/";
std::string strJsonPath = strDataDir
+ std::to_string(pVStep2OutputData->iFrameId)
+ "_"
+ std::to_string(pVStep2OutputData->iDataSource) + ".json";
std::string strImagePath = strDataDir
+ std::to_string(pVStep2OutputData->iFrameId)
+ "_"
+ std::to_string(pVStep2OutputData->iDataSource) + ".jpg";
LogInfo << "strJsonPath:" << strJsonPath << " strImagePath:" << strImagePath;
2024-11-24 00:58:06 +00:00
Json::Value jvFrameInfo;
if (!FileUtil::getins()->readJsonInfo(jvFrameInfo, strJsonPath))
{
continue;
}
cv::Mat image = cv::imread(strImagePath);
std::stringstream ss;
std::vector<std::string> vecTitle;
vecTitle.emplace_back("FrameID:" + to_string(pVStep2OutputData->iFrameId));
vecTitle.emplace_back("TimeStamp:" + jvFrameInfo["timeStamp"].asString());
int base_line = 0;
cv::Point text_org(15, 45); // 文本起始点
for (int i = 0; i < vecTitle.size(); ++i)
{
cv::Size text_size = cv::getTextSize(vecTitle.at(i), cv::FONT_HERSHEY_SIMPLEX, 1, 2, 0);
cv::Point baseline_loc(text_org);
baseline_loc.y += base_line + text_size.height;
cv::putText(image, vecTitle.at(i), baseline_loc, cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0, 255), 2);
// 手动调整x位置为下一个单词留出空间
text_org.x += text_size.width + 10;
text_org.x = text_org.x > IMAGE_WIDTH ? 15 : text_org.x;
base_line++;
}
bool bIsDivideSpace = false;
cv::Scalar cvScalar = {0, 255, 0, 255};
float centerX = pVStep2OutputData->step2ResultData.fLTX + (pVStep2OutputData->step2ResultData.fRBX - pVStep2OutputData->step2ResultData.fLTX)/2;
float centerY = pVStep2OutputData->step2ResultData.fLTY + (pVStep2OutputData->step2ResultData.fRBY - pVStep2OutputData->step2ResultData.fLTY)/2;
cv::rectangle(image,
cv::Point(pVStep2OutputData->step2ResultData.fLTX, pVStep2OutputData->step2ResultData.fLTY),
cv::Point(pVStep2OutputData->step2ResultData.fRBX, pVStep2OutputData->step2ResultData.fRBY),
cvScalar, 2);
cv::line(image,
cv::Point(centerX, pVStep2OutputData->step2ResultData.fLTY-30), cv::Point(centerX, pVStep2OutputData->step2ResultData.fRBY+30),
cvScalar, 1);
cv::Point linePoint(pVStep2OutputData->step2ResultData.fLTX + 10, pVStep2OutputData->step2ResultData.fRBY + 10);
cv::putText(image,
pVStep2OutputData->step2ResultData.transInfo.strTmpResult,
linePoint,
cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0, 180), 2);
2024-11-24 00:58:06 +00:00
for (auto &step2ResultData : pVStep2OutputData->vecCornerResultData)
{
cvScalar = {0, 255, 255, 255};
float centerX_corner = step2ResultData.fLTX + (step2ResultData.fRBX - step2ResultData.fLTX)/2;
2024-11-24 00:58:06 +00:00
cv::rectangle(image,
cv::Point(step2ResultData.fLTX, step2ResultData.fLTY),
cv::Point(step2ResultData.fRBX, step2ResultData.fRBY),
cvScalar, 2);
cv::line(image,
cv::Point(centerX_corner, step2ResultData.fLTY-30), cv::Point(centerX_corner, step2ResultData.fRBY+30),
2024-11-24 00:58:06 +00:00
cvScalar, 1);
}
if (!cv::imwrite(strImagePath, image, this->vecCompressionParams_))
{
LogError << "图片存储失败:" << strImagePath;
}
}
return APP_ERR_OK;
}