/* * @Author: your name * @Date: 2022-04-20 15:50:00 * @LastEditTime: 2025-09-22 16:44:43 * @LastEditors: xiewenji 527774126@qq.com * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @FilePath: /ZCXD_MonitorPlatform/src/CoreLogicModule/src/CamDeal.cpp */ #include "ALLImgCheckAnalysisy.hpp" #include "CheckUtil.hpp" #include "Define.h" #include "AI_Factory.h" #include double calculateDistanceBetweenRectCenters(const cv::Rect &rect1, const cv::Rect &rect2, float fx, float fy) { // 计算矩形1的中心点 cv::Point center1(rect1.x + rect1.width / 2, rect1.y + rect1.height / 2); // 计算矩形2的中心点 cv::Point center2(rect2.x + rect2.width / 2, rect2.y + rect2.height / 2); center1.x *= fx; center1.y *= fy; center2.x *= fx; center2.y *= fy; // 计算中心点之间的欧几里得距离 double distance = cv::norm(center1 - center2); return distance; } ALLImgCheckAnalysisy::ALLImgCheckAnalysisy() { m_strTest = ""; m_pdetlog = std::make_shared(); m_bInitSucc = false; m_bExit = false; } ALLImgCheckAnalysisy::~ALLImgCheckAnalysisy() { StopThread(); } int ALLImgCheckAnalysisy::RunStart(void *pconfig1) { VERSION_INFO *pVersionconfig = (VERSION_INFO *)pconfig1; if (pVersionconfig == NULL) { printf("**************************** \n"); m_nErrorCode = CHECK_ERROR_VERSION; // 参数或接口版本问题 return m_nErrorCode; } int v1 = ALL_INTERFACE_VERSION; int v2 = CONFIGBASE_VERSION; int v3 = RESULT_VERSION; printf("**************************** ALL_INTERFACE_VERSION so: %d in:%d CONFIGBASE_VERSION so:%d in:%d RESULT_VERSION so:%d in:%d\n", v1, pVersionconfig->InterfaceVersion, v2, pVersionconfig->ConfigVersion, v3, pVersionconfig->ResultVersion); // 版本控制 if (pVersionconfig->InterfaceVersion != ALL_INTERFACE_VERSION || pVersionconfig->ConfigVersion != CONFIGBASE_VERSION || pVersionconfig->ResultVersion != RESULT_VERSION) { m_nErrorCode = CHECK_ERROR_VERSION; // 参数或接口版本问题 return m_nErrorCode; } int re = 0; re = InitCameraCheckAnalysisy(); if (re != 0) { printf("*************InitCameraCheckAnalysisy error %d*************** \n", re); m_nErrorCode = INIT_CameraCheck_Error; return m_nErrorCode; } re = InitRun(); if (CHECK_OK != re) { m_nErrorCode = re; return m_nErrorCode; } re = InitAIFactory(); if (CHECK_OK != re) { m_nErrorCode = re; return m_nErrorCode; } re = InitTemplateMask(); if (CHECK_OK != re) { m_nErrorCode = re; return m_nErrorCode; } // 把模板信息下发到各个相机检测实例 for (auto &item : m_pCameraCheckAnalysisyList) { if (item.second) { item.second->SetTplInfo(m_tplOuterRect, m_tplInnerRect, m_tplPinContours); } } m_nErrorCode = CHECK_OK; printf(">>>> ALLImgCheckAnalysisy Start Succ \n"); m_nErrorCode = CHECK_OK; m_bInitSucc = true; return m_nErrorCode; } int ALLImgCheckAnalysisy::SetDataRun_SharePtr(std::shared_ptr p) { int re = PushInImg_New(p); // printf("SetDataRun_SharePtr======================= re %d \n", re); return re; } int ALLImgCheckAnalysisy::GetCheckReuslt(std::shared_ptr &pResult) { std::unique_lock lk(mtx_CheckResult); // 使用 unique_lock CheckResult_cond.wait(lk, [this] { return !m_CheckResultList.empty(); }); // 等待直到数据队列非空 pResult = m_CheckResultList.front(); // 从队列中取出数 m_CheckResultList.pop(); lk.unlock(); return 0; } int ALLImgCheckAnalysisy::ReJson(std::shared_ptr p, std::shared_ptr &pResult) { return 0; } int ALLImgCheckAnalysisy::CheckImg(std::shared_ptr p, std::shared_ptr &pResult) { return 0; } int ALLImgCheckAnalysisy::GetStatus() { return 0; } int ALLImgCheckAnalysisy::UpdateConfig(void *pconfig, int nConfigType) { int re = 0; RunInfoST *tempconfig; switch (nConfigType) { case CHECK_CONFIG_Run: tempconfig = (RunInfoST *)pconfig; m_RunConfig.copy(*tempconfig); break; case CHECK_CONFIG_Module: m_pConfigManager = (ConfigManagerBase *)pconfig; // 参数更新时,若系统已初始化,则重新计算模板并下发给各相机 if (m_bInitSucc) { re = InitTemplateMask(); if (re == 0) { for (auto &item : m_pCameraCheckAnalysisyList) { if (item.second) { item.second->SetTplInfo(m_tplOuterRect, m_tplInnerRect, m_tplPinContours); } } } } break; default: break; } return 0; } std::string ALLImgCheckAnalysisy::GetVersion() { return std::string("BOE_2.0.5"); } std::string ALLImgCheckAnalysisy::GetErrorInfo() { std::string result = ""; return result; } int ALLImgCheckAnalysisy::ErrorReturn(std::shared_ptr p) { std::shared_ptr result = std::make_shared(); result->in_shareImage = p; result->checkStatus = 1; result->nresult = -CHECK_ERROR_PushImg_ID_Error; result->basicResult.img_id = p->img_id; result->basicResult.imgtype = p->imgtype; result->basicResult.imgstr = p->imgstr; result->basicResult.strChannel = p->strChannel; // printf("ErrorReturn==== %s %s\n", p->strImgProductID.c_str(), p->strChannel.c_str()); { std::lock_guard lock(mtx_CheckResult); m_CheckResultList.push(result); } CheckResult_cond.notify_all(); return 0; } std::shared_ptr ALLImgCheckAnalysisy::GetProduct(std::string strProduct) { std::lock_guard lock(mtx_ProductList); for (int i = 0; i < m_ProductList.size(); i++) { // printf("ddd %s \n", m_ProductList.at(i)->productBaseResult->strproductName.c_str()); if (m_ProductList.at(i)->productBaseResult->strproductName == strProduct) { return m_ProductList.at(i); } } return std::shared_ptr(); } int ALLImgCheckAnalysisy::InitAIFactory() { std::shared_ptr AI_Factory; AI_Factory = AIFactory::GetInstance(); GPU_Config gpu; gpu.gpu_0 = true; gpu.gpu_1 = true; AI_Factory->InitALLAIModle(gpu); return 0; } int ALLImgCheckAnalysisy::InitTemplateMask() { std::shared_ptr AI_Factory = AIFactory::GetInstance(); if (!AI_Factory || !AI_Factory->Align_Outer || !AI_Factory->Align_Inner || !AI_Factory->Pin_Loc) { return -1; } m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "---Init Tpl---" , "start"); // 模板图路径,暂时使用固定路径 string tplImgPath = "/home/aidlux/BOE/ZB_SOCKET/Tpl/tpl.bmp"; // 模板图片路径 Mat tplImg = imread(tplImgPath, IMREAD_UNCHANGED); if (tplImg.empty()) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "", "Failed to load template image!"); return -2; } // 模板外接矩形,通过模型Align_Outer得到 RotatedRect tplOuterRect; // 模板外接矩形 std::shared_ptr pSocket_Align; pSocket_Align = AI_Factory->Align_Outer; cv::Size sz; sz.width = pSocket_Align->input_0.width; sz.height = pSocket_Align->input_0.height; cv::Mat detImg; cout << "imgSize: " << tplImg.size() << ", " << "detImgSize: " << detImg.size() << ", " << "szSize: " << sz << endl; cv::resize(tplImg, detImg, sz); int re = 0; cv::Mat mask; if (detImg.channels() != 1) { cv::cvtColor(detImg, detImg, cv::COLOR_RGB2GRAY); } re = pSocket_Align->AIDet(detImg, mask); if (re != 0) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "Init Tpl", "AICheck_Edge----error %d ", re); int re123 = 100 + re; return re123; } // 找到最大轮廓 bool found; std::vector boundingBox = CheckUtil::getLargestContourROI(mask, found); if (!found) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Edge_Algin ", "No contours found!----error "); return 4; } // 边缘轮廓点集还原为原图坐标,并计算得到RotateROI if (sz.width <= 0 || sz.height <= 0) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Edge_Algin ", "invalid model input size!----error "); return 5; } // 检测小图 到 原图 的缩放比例 const float scale_x = static_cast(tplImg.cols) / sz.width; const float scale_y = static_cast(tplImg.rows) / sz.height; // 轮廓点还原到原图坐标系 std::vector srcContour; srcContour.reserve(boundingBox.size()); for (const auto &pt : boundingBox) { srcContour.emplace_back(pt.x * scale_x, pt.y * scale_y); } if (srcContour.size() < 3) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Edge_Algin ", "contour points too few!----error "); return 6; } tplOuterRect = cv::minAreaRect(srcContour); // 模板内边缘矩形,通过模型Align_Inner得到 // 与 AI_Edge_Algin::Detect 的内定位流程保持一致:按外接矩形裁剪模板图 -> Align_Inner 推理 -> 轮廓还原并把裁剪偏移加回,得到模板图坐标系下的内边缘矩形 // PIN 偏移/缺失检测的仿射变换以内定位为基准,模板侧需先准备好该矩形;计算失败时保持空值,运行时自动回退外定位 cv::RotatedRect tplInnerRect; bool bTplInnerOk = false; std::shared_ptr pSocket_AlignInner = AI_Factory->Align_Inner; cv::Rect tplOuterBound = tplOuterRect.boundingRect() & cv::Rect(0, 0, tplImg.cols, tplImg.rows); if (pSocket_AlignInner && tplOuterBound.width > 0 && tplOuterBound.height > 0) { cv::Mat innerCropImg = tplImg(tplOuterBound).clone(); cv::Size innerSz; innerSz.width = pSocket_AlignInner->input_0.width; innerSz.height = pSocket_AlignInner->input_0.height; cv::Mat innerDetImg; cv::resize(innerCropImg, innerDetImg, innerSz); if (innerDetImg.channels() != 1) { // 与运行时 AI_Edge_Algin::Get_Edge 内定位的灰度化保持一致(BGR 顺序) cv::cvtColor(innerDetImg, innerDetImg, cv::COLOR_BGR2GRAY); } cv::Mat innerMask; int reInner = pSocket_AlignInner->AIDet(innerDetImg, innerMask); if (reInner != 0) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "Init Tpl", "AICheck_Inner----error %d ", reInner); } else { // 找到最大轮廓 bool innerFound = false; std::vector innerBoundingBox = CheckUtil::getLargestContourROI(innerMask, innerFound); if (!innerFound || innerBoundingBox.size() < 3) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "Init Tpl", "Inner align: No contours found!----error "); } else { // 内定位小图 到 裁剪图 的缩放比例 const float inner_scale_x = static_cast(innerCropImg.cols) / innerSz.width; const float inner_scale_y = static_cast(innerCropImg.rows) / innerSz.height; std::vector innerContour; innerContour.reserve(innerBoundingBox.size()); for (const auto &pt : innerBoundingBox) { innerContour.emplace_back(pt.x * inner_scale_x, pt.y * inner_scale_y); } tplInnerRect = cv::minAreaRect(innerContour); // 裁剪图坐标 -> 模板图坐标(与 AI_Edge_Algin::Detect 中 smallroi 偏移加回一致) tplInnerRect.center.x += static_cast(tplOuterBound.x); tplInnerRect.center.y += static_cast(tplOuterBound.y); bTplInnerOk = true; } } } else { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "Init Tpl", "Inner align: model null or outer bound invalid!----error "); } if (bTplInnerOk) { m_tplInnerRect = tplInnerRect; m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "Init Tpl", "tplInnerRect center(%.1f %.1f) size(%.1f %.1f) angle %.1f", tplInnerRect.center.x, tplInnerRect.center.y, tplInnerRect.size.width, tplInnerRect.size.height, tplInnerRect.angle); } else { m_tplInnerRect = cv::RotatedRect(); m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "Init Tpl", "Tpl Inner align invalid, runtime fallback to Outer align!"); } // 模板Pinmask,通过模型Pin_Loc得到 Mat tplPinMask; // 模板mask std::shared_ptr pSocket_Pin; pSocket_Pin = AI_Factory->Pin_Loc; sz.width = pSocket_Pin->input_0.width; sz.height = pSocket_Pin->input_0.height; cv::Mat pinImg = tplImg(tplOuterRect.boundingRect()).clone(); // 裁剪模板图像为外接矩形区域 cout << "imgSize: " << tplImg.size() << ", " << "pinImgSize: " << pinImg.size() << ", " << "szSize: " << sz << endl; cv::resize(pinImg, detImg, sz); if (detImg.channels() != 1) { cv::cvtColor(detImg, detImg, cv::COLOR_RGB2GRAY); } re = pSocket_Pin->AIDet(detImg, tplPinMask); if (re != 0) { m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Pin ", "AICheck_Pin----error %d ", re); int re123 = 100 + re; return re123; } // 统计tplPinMask的blob边缘点,并绘制到tplImg上 vector> pin_contours; vector hierarchy; cv::findContours(tplPinMask, pin_contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE); // mask 尺寸 -> 裁剪图(pinImg) 尺寸 的缩放比例 const float pin_scale_x = (tplPinMask.cols > 0) ? static_cast(pinImg.cols) / tplPinMask.cols : 0.0f; const float pin_scale_y = (tplPinMask.rows > 0) ? static_cast(pinImg.rows) / tplPinMask.rows : 0.0f; // 将轮廓点从 mask 坐标系 映射回 tplImg 坐标系 m_tplPinContours.clear(); m_tplPinContours.reserve(pin_contours.size()); for (size_t i = 0; i < pin_contours.size(); i++) { std::vector mapped; mapped.reserve(pin_contours[i].size()); for (const auto &pt : pin_contours[i]) { mapped.emplace_back(cvRound(pt.x * pin_scale_x), cvRound(pt.y * pin_scale_y)); } m_tplPinContours.push_back(mapped); } // 保存模板外接矩形 m_tplOuterRect = tplOuterRect; // 绘制到 tplImg 上 { Mat showImg = pinImg.clone(); cv::drawContours(pinImg, m_tplPinContours, -1, Scalar(0, 255, 0), 2); cv::imwrite("/home/aidlux/BOE/ZB_SOCKET/Tpl/tpl_pin_contours.png", pinImg); } m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "---Init Tpl---" , "end"); return 0; } int ALLImgCheckAnalysisy::LoadRunConfig(void *p) { return 0; } int ALLImgCheckAnalysisy::LoadCheckConfig(void *p) { return 0; } int ALLImgCheckAnalysisy::InitRun() { int re; re = StartThread(); if (CHECK_OK != re) { return re; } return 0; } int ALLImgCheckAnalysisy::StartCheck() { return 0; } int ALLImgCheckAnalysisy::SetIDLE() { return 0; } int ALLImgCheckAnalysisy::StartThread() { m_bExit = false; { // 开启检测线程 ptr_thread_Run = std::make_shared(std::bind(&ALLImgCheckAnalysisy::Run, this)); } return 0; } int ALLImgCheckAnalysisy::StopThread() { m_bExit = true; if (ptr_thread_Run != nullptr) { if (ptr_thread_Run->joinable()) { ptr_thread_Run->join(); } } return 0; } int ALLImgCheckAnalysisy::ExitSystem() { return 0; } int ALLImgCheckAnalysisy::InitCameraCheckAnalysisy() { m_pCameraCheckAnalysisyList.clear(); for (const auto &config : m_pConfigManager->Config_instances_) { std::cout << "key: " << config.first << std::endl; std::shared_ptr p = std::make_shared(); p->m_RunConfig.copy(m_RunConfig); p->m_pConfig = config.second; int re = p->Init(config.first); if (re != 0) { std::cout << "Init CameraCheckAnalysisy " << config.first << " Failed" << std::endl; return -1; } m_pCameraCheckAnalysisyList[config.first] = p; } if (m_pCameraCheckAnalysisyList.size() <= 0) { printf("ALLImgCheckAnalysisy::InitCameraCheckAnalysisy error camera config is null \n"); return 1; /* code */ } // for (const auto &pCameraCheckAnalysisy : m_pCameraCheckAnalysisyList) // { // std::cout << "key: " << pCameraCheckAnalysisy.first << std::endl; // } printf("ALLImgCheckAnalysisy::InitCameraCheckAnalysisy end \n"); return 0; } int ALLImgCheckAnalysisy::InitData() { return 0; } int ALLImgCheckAnalysisy::Det_Product(std::shared_ptr &product) { string cur_time_s = CheckUtil::getCurTimeHMS(); printf("[%s]>>>>>>>>>>>>>>>Det_Product****************det Start************\n", cur_time_s.c_str()); // 处理每个相机 while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(20)); { std::lock_guard lock(product->mtx_CameraResultList); // for (auto &ptr : product->m_pCameraResultList) { if (ptr->GetCheckStep() == Check_Step_NODet) { m_pCameraCheckAnalysisyList[ptr->cameraBaseResult->strCameraName]->StartCheck(ptr); } } } // 判断每个相机是否都处理完了。 bool bdetComplete = product->bCheckCamplate(); // 处理完成,退出循环 if (bdetComplete) { break; } } // 联合分析 // AnalysiyAll(0); SetProductResult(product); product->SetCheckEnd(); string cur_time_e = CheckUtil::getCurTimeHMS(); printf("[%s]>>>>>>>>>>>>>>>Det_Product****************det End************\n", cur_time_e.c_str()); return 0; } std::shared_ptr ALLImgCheckAnalysisy::GetProduct() { std::unique_lock lk(mtx_ProductList); // 使用 unique_lock ProductList_cond.wait(lk, [this] { return !m_ProductList.empty(); }); // 等待直到数据队列非空 std::shared_ptr tem = m_ProductList.at(0); lk.unlock(); return tem; } int ALLImgCheckAnalysisy::AnalysiyAll(int productIdx) { return 0; } int ALLImgCheckAnalysisy::SetProductResult(std::shared_ptr product) { int nDetCamNum = product->m_pCameraResultList.size(); // 相机2的起始位置 bool bNG = false; for (int icam = 0; icam < nDetCamNum; icam++) { std::shared_ptr pcam = product->m_pCameraResultList.at(icam); for (int i = 0; i < pcam->ImageALLDetResultList.size(); i++) { std::shared_ptr pCheckResult = pcam->ImageALLDetResultList.at(i)->result; if (pCheckResult->qxImageResult.size() > 0) { bNG = true; } if (bNG) { break; } } if (bNG) { break; } } if (bNG) { for (int icam = 0; icam < nDetCamNum; icam++) { std::shared_ptr pcam = product->m_pCameraResultList.at(icam); for (int i = 0; i < pcam->ImageALLDetResultList.size(); i++) { std::shared_ptr pCheckResult = pcam->ImageALLDetResultList.at(i)->result; pCheckResult->nProductResult = 1; } } } return 0; } int ALLImgCheckAnalysisy::SetSetComplet_New(std::shared_ptr product, int nerror) { if (product) { int nDetCamNum = product->m_pCameraResultList.size(); for (int icam = 0; icam < nDetCamNum; icam++) { std::shared_ptr pcam = product->m_pCameraResultList.at(icam); for (int i = 0; i < pcam->ImageALLDetResultList.size(); i++) { std::shared_ptr pCheckResult = pcam->ImageALLDetResultList.at(i)->result; { std::lock_guard lock123(mtx_CheckResult); m_CheckResultList.push(pCheckResult); } CheckResult_cond.notify_all(); } } } return 0; } int ALLImgCheckAnalysisy::PushInImg_New(std::shared_ptr p) { if (!m_bInitSucc) { printf("Init fail exit \n"); return CHECK_ERROR_Config_Null; } // 对传入图片做一步判断,如果高大于宽,则顺时针旋转90° if (p->Det_Mode != DET_MODE_ReJson) { if (!p->img.empty() && p->img.rows > p->img.cols) { cv::Mat imgRot; cv::rotate(p->img, imgRot, cv::ROTATE_90_CLOCKWISE); printf("PushInImg rotate 90CW: SN %s channel %s img [%d x %d] -> [%d x %d]\n", p->strImgProductID.c_str(), p->strChannel.c_str(), p->img.cols, p->img.rows, imgRot.cols, imgRot.rows); p->img = imgRot; } if (!p->img_B.empty() && p->img_B.rows > p->img_B.cols) { cv::Mat imgBRot; cv::rotate(p->img_B, imgBRot, cv::ROTATE_90_CLOCKWISE); printf("PushInImg rotate 90CW: SN %s channel %s img_B [%d x %d] -> [%d x %d]\n", p->strImgProductID.c_str(), p->strChannel.c_str(), p->img_B.cols, p->img_B.rows, imgBRot.cols, imgBRot.rows); p->img_B = imgBRot; } } std::string strlog = ""; m_strTest += " PushInImg->SN:"; m_strTest += p->strImgProductID; m_strTest += " Channel:"; m_strTest += p->strChannel; m_strTest += " CameraID:"; m_strTest += p->strCameraName; std::string strBase = ""; strBase += " PushInImg->SN:"; strBase += p->strImgProductID; strBase += " Channel:"; strBase += p->strChannel; printf("strBase %s p->Status %d\n", strBase.c_str(), p->Status); std::shared_ptr product; { std::string strcameraName = p->strCameraName; cout << "strcameraName: " << strcameraName << endl; if(m_pCameraCheckAnalysisyList.size() <= 0){ cout << "m_pCameraCheckAnalysisyList.size(): " << m_pCameraCheckAnalysisyList.size() << endl; } for (auto it = m_pCameraCheckAnalysisyList.begin(); it != m_pCameraCheckAnalysisyList.end(); ++it) { std::cout << "m_pCameraCheckAnalysisyList_Key: " << it->first << std::endl; } // 判断 相机名称是否存在 if (!m_pCameraCheckAnalysisyList.count(strcameraName) && -1 != p->Status) { return CHECK_ERROR_Camear_ID_Error; } product = GetProduct(p->strImgProductID); bool ProductID_Exist = false; // 是否存在当前产品ID if (product) { ProductID_Exist = true; } // 产品存在 并且 模式 是第一张图, 则,返回异常。 if (IN_IMG_Status_Start == p->Status && ProductID_Exist) { return CHECK_ERROR_PRODUCT_ID_EXIST; } // 产品存在,但是 图片都已经完了的状态 if (ProductID_Exist && product->bIsImgComplete) { ErrorReturn(p); return CHECK_ERROR_PRODUCT_ID_EXIST; } // 产品不存在 新建一个 if (!product) { std::lock_guard lock(mtx_ProductList); { if (m_ProductList.size() > 5) { return CHECK_ERROR_PushImg_ListSize; } } product = std::make_shared(); product->productBaseResult->strproductName = p->strImgProductID; m_ProductList.push_back(product); std::string strTimg = CheckUtil::getCurTimeHMS(); product->productBaseResult->detlog->AddCheckstr(PrintLevel_0, "PushInImg", "add new product %s %s ", p->strImgProductID.c_str(), strTimg.c_str()); } // 送入图的状态 不是结束 if (-1 != p->Status) { // 对应相机的 检测结果 std::shared_ptr pCamera = product->GetCameraResult(strcameraName); if (pCamera.get() == nullptr) { ErrorReturn(p); return CHECK_ERROR_Camear_ID_Error; } // 添加产品信息 pCamera->AddDetImage(p); } product->UpdatePushStatus(p->Status); if (-1 == p->Status || IN_IMG_Status_End == p->Status) { m_strTest += "\n"; product->AddLog(m_strTest); m_strTest = ""; } ProductList_cond.notify_all(); } return 0; } int ALLImgCheckAnalysisy::Run() { while (!m_bExit) { std::this_thread::sleep_for(std::chrono::milliseconds(20)); std::shared_ptr product = GetProduct(); int re = Det_Product(product); // // 检测失败 // if (re != 0) // { // continue; // } SetSetComplet_New(product, re); // 更新 产品队列 { std::lock_guard lock(mtx_ProductList); // if (!m_ProductList.empty()) { m_ProductList.erase(m_ProductList.begin()); } } } return 0; } int ALLImgCheckAnalysisy::set_cpu_id(const std::vector &cpu_set_vec) { // for cpu affinity int nRet = 0; #ifdef __linux cpu_set_t _cur_cpu_set; CPU_ZERO(&_cur_cpu_set); for (auto _id : cpu_set_vec) { CPU_SET(_id, &_cur_cpu_set); } if (0 > pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &_cur_cpu_set)) { perror("set cpu affinity failed: "); printf("Warning: set cpu affinity failed ... ...\n"); nRet = -1; } #endif //__linux return nRet; }