diff --git a/AlgorithmModule/src/ImgCheckAnalysisy.cpp b/AlgorithmModule/src/ImgCheckAnalysisy.cpp index 87ddcb9..083d897 100644 --- a/AlgorithmModule/src/ImgCheckAnalysisy.cpp +++ b/AlgorithmModule/src/ImgCheckAnalysisy.cpp @@ -1960,105 +1960,95 @@ int ImgCheckAnalysisy::AI_Detect_Thread(const cv::Mat &img, cv::Mat &ResultImg) long t1, t2, t3; t1 = CheckUtil::getcurTime(); - SmallRoiList.clear(); - SmallRoiList.erase(SmallRoiList.begin(), SmallRoiList.end()); - cv::Rect cutRoi; - cutRoi.x = 0; - cutRoi.y = 0; - cutRoi.width = img.cols - 0; - cutRoi.height = img.rows - 0; - int deal_image_width = pAIDet->input_0.width; int deal_image_height = pAIDet->input_0.height; - - int re = CheckUtil::cutSmallImg(img, SmallRoiList, cutRoi, deal_image_width, deal_image_height, 0, 0); - - m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_3, strBaseLog, " cutSmallImg Num %d", SmallRoiList.size()); - if (re != 0 || SmallRoiList.size() <= 0) + if (img.empty() || deal_image_width <= 0 || deal_image_height <= 0) { - m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_3, strBaseLog, " cutSmallImg error %d", re); + m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_3, strBaseLog, " img empty %d model input size error [w %d h %d]", img.empty(), deal_image_width, deal_image_height); m_pdetlog->AddCheckstr(PrintLevel_0, DET_LOG_LEVEL_3, strBaseLog, "AI_Detect End"); - return re; - /* code */ + return 1; } - // 临时存图 - if (DetImgInfo_shareP->bsaveProcessImg) + // 不再拆分小图:整图 resize 到模型输入尺寸推理,输出 mask 再 resize 回原图尺寸 + cv::Mat inputImg; + cv::resize(img, inputImg, cv::Size(deal_image_width, deal_image_height)); + // 模型输入要求单通道;原图为彩色时按 BGR 顺序转灰度 + if (inputImg.channels() == 3) { - - cv::Mat imshow = img.clone(); // 保持输入通道,不再灰度转假彩 - for (int i = 0; i < SmallRoiList.size(); i++) - { - cv::rectangle(imshow, SmallRoiList.at(i), CheckUtil::GetDrawColor(imshow, cv::Scalar(0, 0, 255)), 3); - } - cv::imwrite(DetImgInfo_shareP->strChannel + "_AI_Det_ROI.jpg", imshow); + cv::cvtColor(inputImg, inputImg, cv::COLOR_BGR2GRAY); + } + else if (inputImg.channels() == 4) + { + cv::cvtColor(inputImg, inputImg, cv::COLOR_BGRA2GRAY); } - ResultImg = cv::Mat::zeros(img.size(), CV_8UC1); + // 临时存图:模型输入 + if (DetImgInfo_shareP->bsaveProcessImg) + { + cv::imwrite(DetImgInfo_shareP->strCameraName + "_AI_Det_in.png", inputImg); + } - const int totalTasks = SmallRoiList.size(); - int submitted = 0; - int completed = 0; + // 提交整图推理任务并等待结果 + std::shared_ptr task = std::make_shared(); + task->id = 0; + task->roi = cv::Rect(0, 0, img.cols, img.rows); + task->input = inputImg; + task->output = std::make_shared(); + task->engine = pAIDet; - // std::string str_Root = m_strRootPath + pDetConfig->strProductID + "_" + pDetConfig->strchannel + "_"; t2 = CheckUtil::getcurTime(); - while (completed < totalTasks) - { - // 如果任务还没提交完,且当前处理任务数 < 2,提交新任务 - if (submitted < totalTasks && runner->GetProcessingCount() < 10) - { + runner->SubmitTask(task); - std::shared_ptr task = std::make_shared(); - task->id = completed; - task->roi = SmallRoiList.at(submitted); - task->input = img(SmallRoiList.at(submitted)).clone(); - task->output = std::make_shared(); - task->engine = pAIDet; + std::shared_ptr result; + while (!runner->PopResult(result)) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + t3 = CheckUtil::getcurTime(); - runner->SubmitTask(task); - submitted++; - } + cv::Mat &outimg = *(result->output); // 模型输出尺寸的 mask - // 尝试取结果 - std::shared_ptr result; - if (runner->PopResult(result)) - { + // 输出 mask resize 回原图尺寸:mask 为缺陷二值图,用最近邻插值(避免插值晕圈被 Blob 当作缺陷多检出) + ResultImg = cv::Mat::zeros(img.size(), CV_8UC1); + if (!outimg.empty()) + { + cv::resize(outimg, ResultImg, img.size(), 0, 0, cv::INTER_NEAREST); + } + else + { + m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_3, strBaseLog, " model output mask is empty, result mask all zero"); + } - { - std::lock_guard lock(mtx_AIMaskImgBLobQueue); - m_AIMaskImgBLobQueue.push(result); - } + m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_3, strBaseLog, " AI Run Time: sum %ld pre %ld Run %ld model in [w %d h %d] mask [w %d h %d] -> [w %d h %d]", + t3 - t1, t2 - t1, t3 - t2, deal_image_width, deal_image_height, outimg.cols, outimg.rows, img.cols, img.rows); - cv::Mat &outimg = *(result->output); - if (!outimg.empty()) - { - outimg.copyTo(ResultImg(result->roi), outimg); - } - { - std::shared_ptr temAIresult = std::make_shared(); - temAIresult->roi = result->roi; - temAIresult->AI_inImg = result->input; - temAIresult->AI_mask = outimg; - m_pImageAllResult->AI_Qx_MaskList.push_back(temAIresult); - } - completed++; - } - else - { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + // 供 AIMaskDet 标记 mask 有缺陷的行(加速 Blob 计算):整图坐标 + 整图尺寸 + { + std::shared_ptr maskTask = std::make_shared(); + maskTask->id = 0; + maskTask->roi = cv::Rect(0, 0, img.cols, img.rows); + maskTask->input = result->input; + maskTask->output = std::make_shared(ResultImg); + maskTask->engine = pAIDet; + std::lock_guard lock(mtx_AIMaskImgBLobQueue); + m_AIMaskImgBLobQueue.push(maskTask); } - t3 = CheckUtil::getcurTime(); - float mean_AI = (t3 - t2) / SmallRoiList.size(); - m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_3, strBaseLog, " AI Run Time: sum %ld pre %ld Run %ld mean One Small Img %f", t3 - t1, t2 - t1, t3 - t2, mean_AI); + // 留存 AI 输入/输出(模型尺寸),供缺陷结果按中心点取 AI 图(GetAIDetImg) + { + std::shared_ptr temAIresult = std::make_shared(); + temAIresult->roi = cv::Rect(0, 0, img.cols, img.rows); + temAIresult->AI_inImg = result->input; + temAIresult->AI_mask = outimg; + m_pImageAllResult->AI_Qx_MaskList.push_back(temAIresult); + } m_pdetlog->AddCheckstr(PrintLevel_0, DET_LOG_LEVEL_3, strBaseLog, "AI_Detect End"); // 临时存图 if (DetImgInfo_shareP->bsaveProcessImg) { - cv::imwrite(DetImgInfo_shareP->strChannel + "_AI_mask.png", ResultImg); + cv::imwrite(DetImgInfo_shareP->strCameraName + "_AI_mask.png", ResultImg); } // getchar();