update 优化edge耗时

dev_lsy
liusiyang 7 days ago
parent 92c603b2a6
commit 3938c9b1da

@ -160,7 +160,7 @@ public:
private: private:
int InitModel_Big(); int InitModel_Big();
int Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector<cv::RotatedRect> &RoiList, Mat &outmask); int Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector<cv::RotatedRect> &RoiList, Mat &outmask, bool bResizeOut = false);
int creatsavedir(); int creatsavedir();

@ -9,6 +9,7 @@
#include "AI_Edge_Algin.h" #include "AI_Edge_Algin.h"
#include "CheckErrorCodeDefine.hpp" #include "CheckErrorCodeDefine.hpp"
#include <thread>
#define EDGE_GPU 0 #define EDGE_GPU 0
@ -231,8 +232,10 @@ int AI_Edge_Algin::Detect(const cv::Mat &img, DetConfig *pDetConfig, std::shared
return 1; return 1;
} }
// 1、初步定位 找到产品大致区域 // 1、初步定位 找到产品大致区域(三个模型输入输出尺寸不同,各自 resize推理相互独立并行执行
int re = 0; int re0 = 0;
int re1 = 0;
int re2 = 0;
std::vector<cv::RotatedRect> Big_roi; std::vector<cv::RotatedRect> Big_roi;
std::vector<cv::RotatedRect> Small_roi; std::vector<cv::RotatedRect> Small_roi;
@ -241,35 +244,41 @@ int AI_Edge_Algin::Detect(const cv::Mat &img, DetConfig *pDetConfig, std::shared
cv::Mat Small_outimg; cv::Mat Small_outimg;
cv::Mat Tag_outimg; cv::Mat Tag_outimg;
re = Get_Edge(0, img, pDetConfig, m_pDetConfig->strChannel, Big_roi, Big_outimg); std::vector<std::thread> vecThreads;
if (re != 0) vecThreads.emplace_back([&]() { re0 = Get_Edge(0, img, pDetConfig, m_pDetConfig->strChannel, Big_roi, Big_outimg, true); });
vecThreads.emplace_back([&]() { re1 = Get_Edge(1, img, pDetConfig, m_pDetConfig->strChannel, Small_roi, Small_outimg, false); });
vecThreads.emplace_back([&]() { re2 = Get_Edge(2, img, pDetConfig, m_pDetConfig->strChannel, Tag_roi, Tag_outimg, false); });
for (auto &t : vecThreads)
{
t.join();
}
if (re0 != 0)
{ {
m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Big----error %d ", re); m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Big----error %d ", re0);
if (m_pDetConfig->IsSaveProcessImg()) if (m_pDetConfig->IsSaveProcessImg())
{ {
cv::imwrite(str_error, img); cv::imwrite(str_error, img);
} }
return re; return re0;
} }
re = Get_Edge(1, img, pDetConfig, m_pDetConfig->strChannel, Small_roi, Small_outimg); if (re1 != 0)
if (re != 0)
{ {
m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Small----error %d ", re); m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Small----error %d ", re1);
if (m_pDetConfig->IsSaveProcessImg()) if (m_pDetConfig->IsSaveProcessImg())
{ {
cv::imwrite(str_error, img); cv::imwrite(str_error, img);
} }
return re; return re1;
} }
re = Get_Edge(2, img, pDetConfig, m_pDetConfig->strChannel, Tag_roi, Tag_outimg); if (re2 != 0)
if (re != 0)
{ {
m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Tag----error %d ", re); m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Tag----error %d ", re2);
if (m_pDetConfig->IsSaveProcessImg()) if (m_pDetConfig->IsSaveProcessImg())
{ {
cv::imwrite(str_error, img); cv::imwrite(str_error, img);
} }
return re; return re2;
} }
m_pCheckResult_Aling->bigroi = Big_roi; m_pCheckResult_Aling->bigroi = Big_roi;
@ -348,7 +357,7 @@ int AI_Edge_Algin::InitModel_ALL()
return 0; return 0;
} }
int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector<cv::RotatedRect> &RoiList, Mat &outmask) int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector<cv::RotatedRect> &RoiList, Mat &outmask, bool bResizeOut)
{ {
std::shared_ptr<AIModel_Base> pBackPlate_Align; std::shared_ptr<AIModel_Base> pBackPlate_Align;
switch (AIModel_type) switch (AIModel_type)
@ -370,8 +379,8 @@ int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDe
sz.width = pBackPlate_Align->input_0.width; sz.width = pBackPlate_Align->input_0.width;
sz.height = pBackPlate_Align->input_0.height; sz.height = pBackPlate_Align->input_0.height;
cv::Mat detImg; cv::Mat detImg;
cout<< pDetConfig->strChannel << ": " << "---Get_Edge-resize-" << to_string(AIModel_type) <<"-- "; // cout<< pDetConfig->strChannel << ": " << "---Get_Edge-resize-" << to_string(AIModel_type) <<"-- ";
cout << "imgSize: " << img.size() << ", " << "detImgSize: " << detImg.size() << ", " << "szSize: " << sz << endl; // cout << "imgSize: " << img.size() << ", " << "detImgSize: " << detImg.size() << ", " << "szSize: " << sz << endl;
cv::resize(img, detImg, sz); cv::resize(img, detImg, sz);
int re = 0; int re = 0;
@ -413,8 +422,15 @@ int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDe
cv::imwrite(strChannel +"_edge_"+ to_string(AIModel_type) +"_in.png", detImg); cv::imwrite(strChannel +"_edge_"+ to_string(AIModel_type) +"_in.png", detImg);
cv::imwrite(strChannel +"_edge_"+ to_string(AIModel_type) +"_out_mask.png", mask); cv::imwrite(strChannel +"_edge_"+ to_string(AIModel_type) +"_out_mask.png", mask);
} }
// resize out_mask并返回 // resize out_mask并返回仅大轮廓 mask 需要原图尺寸,后续用于把手检测;其余保持小图)
cv::resize(mask, outmask, img.size()); if (bResizeOut)
{
cv::resize(mask, outmask, img.size());
}
else
{
outmask = mask;
}
// 检查模型输入尺寸 // 检查模型输入尺寸
if (sz.width <= 0 || sz.height <= 0) if (sz.width <= 0 || sz.height <= 0)

@ -566,9 +566,7 @@ int ImageResultJudge::ResultJudge(std::shared_ptr<ImageAllResult> pImageResult)
GetAIDetImg(pImageResult, pCenter, tem.AI_in_Img, tem.AI_out_img); GetAIDetImg(pImageResult, pCenter, tem.AI_in_Img, tem.AI_out_img);
m_CheckResult_shareP->qxImageResult.push_back(tem); m_CheckResult_shareP->qxImageResult.push_back(tem);
pQxLog->bPrintStr = true;
pQxLog->AddCheckstr(PrintLevel_4, DET_LOG_LEVEL_3, " result ", " ---name: %s ---code: %s ---srcImgroi.x: %d ---srcImgroi.y: %d\n", tem.strTypeName.c_str(), tem.qx_Code.c_str(), tem.srcImgroi.x, tem.srcImgroi.y); pQxLog->AddCheckstr(PrintLevel_4, DET_LOG_LEVEL_3, " result ", " ---name: %s ---code: %s ---srcImgroi.x: %d ---srcImgroi.y: %d\n", tem.strTypeName.c_str(), tem.qx_Code.c_str(), tem.srcImgroi.x, tem.srcImgroi.y);
pQxLog->bPrintStr = false;
} }
else else
{ {

@ -425,7 +425,7 @@ int ImgCheckAnalysisy::CheckRun()
{ {
m_pdetlog->bPrintStr = true; m_pdetlog->bPrintStr = true;
} }
m_pdetlog->bPrintStr = true; // m_pdetlog->bPrintStr = true;
m_pdetlog->AddCheckstr(PrintLevel_0, "1、basic Info", "---------------------------1、basic Info---------------------------------"); m_pdetlog->AddCheckstr(PrintLevel_0, "1、basic Info", "---------------------------1、basic Info---------------------------------");
m_pdetlog->AddCheckstr(PrintLevel_0, "Version", "%s", GetVersion().c_str()); m_pdetlog->AddCheckstr(PrintLevel_0, "Version", "%s", GetVersion().c_str());
@ -2065,13 +2065,17 @@ int ImgCheckAnalysisy::AI_Edge(const cv::Mat &img, cv::RotatedRect &outerRoi, cv
} }
/*使用m_pEdge_Align_Result->bigmask检测把手是否缺失*/ /*使用m_pEdge_Align_Result->bigmask检测把手是否缺失*/
// 截取把手大致区域 // 截取把手大致区域(使用把手模板完整 boundingRect而非整图高度
RotatedRect tpl_handle_rroi = m_pFuntion->function.f_supportDet.handleRect; RotatedRect tpl_handle_rroi = m_pFuntion->function.f_supportDet.handleRect;
Rect handle_rect = Rect(tpl_handle_rroi.boundingRect().x, 0, tpl_handle_rroi.boundingRect().width, m_pEdge_Align_Result->bigmask.rows); Rect handle_rect = tpl_handle_rroi.boundingRect() & Rect(0, 0, m_pEdge_Align_Result->bigmask.cols, m_pEdge_Align_Result->bigmask.rows);
Mat handle_roi = m_pEdge_Align_Result->bigmask(handle_rect & Rect(0, 0, m_pEdge_Align_Result->bigmask.cols, m_pEdge_Align_Result->bigmask.rows)).clone(); if (handle_rect.width <= 0 || handle_rect.height <= 0)
{
return re;
}
Mat handle_roi = m_pEdge_Align_Result->bigmask(handle_rect).clone();
// 对handle_roi做一下开运算 // 对handle_roi做一下开运算
Mat element = getStructuringElement(MORPH_RECT, Size(tpl_handle_rroi.boundingRect().width / 3, tpl_handle_rroi.boundingRect().height / 3)); Mat element = getStructuringElement(MORPH_RECT, Size(std::max(1, tpl_handle_rroi.boundingRect().width / 3), std::max(1, tpl_handle_rroi.boundingRect().height / 3)));
morphologyEx(handle_roi, handle_roi, MORPH_OPEN, element); morphologyEx(handle_roi, handle_roi, MORPH_OPEN, element);
// imwrite("handle_roi.png", handle_roi); // imwrite("handle_roi.png", handle_roi);

Loading…
Cancel
Save