You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
9.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* @Author: your name
* @Date: 2022-04-20 15:49:50
* @LastEditTime: 2025-09-30 16:17:49
* @LastEditors: xiewenji 527774126@qq.com
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: /ZCXD_MonitorPlatform/src/CoreLogicModule/include/CamDeal.h
*/
#ifndef ImgCheckAnalysisy_H_
#define ImgCheckAnalysisy_H_
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <opencv2/opencv.hpp>
#include <condition_variable>
#include <mutex>
#include <vector>
#include <thread>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "BlobBase.h"
#include "ImgCheckBase.h"
#include "ImageDetBase.h"
#include "ImgCheckConfig.h"
#include "CheckErrorCodeDefine.hpp"
#include "CheckConfigDefine.h"
#include "ConfigBase.h"
#include "ImageDetConfig.h"
#include "OtherDetect.h"
#include "Define_Error.h"
#include "AI_Factory.h"
#include "ImageAllResult.h"
#include "Task.h"
#include "ImageStorage.h"
#include "AIClassify.h"
#include "AI_Second_Det.h"
#include "AI_Edge_QX_Det.h"
using namespace std;
using namespace cv;
enum AT_THRESHOLD_TYPE_
{
AT_THRESHOLD_TYPE_NULL,
AT_THRESHOLD_TYPE_READY,
AT_THRESHOLD_TYPE_BUSY,
AT_THRESHOLD_TYPE_COMPLETE,
};
// 全局静态变量, 记录图像灰度值异常累计数量
static int g_nImgBrightnessErrorCount = 0;
class ImgCheckAnalysisy : public ImgCheckBase
{
public:
ImgCheckAnalysisy();
~ImgCheckAnalysisy();
// 初始化参数 pconfig 参数指针 返回0 成功 其他异常
int RunStart(void *pconfig1);
// 设置检测数据,并开启检测 返回0 成AT_THRESHOLD_TYPE_READY功 其他异常
int SetDataRun_SharePtr(std::shared_ptr<ImageAllResult> p);
// 获取结果信息 返回0 成功 其他异常
int GetCheckReuslt(std::shared_ptr<ImageDetResult> &pResult);
int CheckImg(std::shared_ptr<ImageDetconfig> p, std::shared_ptr<ImageDetResult> &pResult);
int ReJsonResul(std::shared_ptr<ImageDetconfig> p, std::shared_ptr<ImageDetResult> &pResult);
// 获取检测库 状态信息 返回CHECK_THREAD_RUN_STATUS
int GetStatus();
// 更新参数 pconfig 参数指针nConfigType 需要更新的参数类型 返回0 成功 其他异常
int UpdateConfig(void *pconfig, int nConfigType);
std::string GetVersion();
std::string GetErrorInfo();
int creatsavedir();
private:
// 加载运行参数
int LoadRunConfig(void *p);
// 加载分析参数
int LoadCheckConfig(void *p);
/// @brief 初始化并且启动程序
/// @return
int InitRun(int nId);
/// @brief 开启检测
/// @return
int StartCheck();
/// @brief 设置空闲
/// @return
int SetIDLE();
private:
// 开启线程
int StartThread(int nId);
// 停止线程
int StopThread();
// 退出系统
int ExitSystem();
// 初始化模型
int InitModel();
double CalBlobHJ(cv::Mat &img, const cv::Mat &mask, cv::Rect &det_roi, cv::Rect &qx_roi, int expand = 10);
cv::Scalar calc_blob_info_withstats(cv::Mat &img, const cv::Mat &mask, cv::Rect &stats, cv::Size k_size = cv::Size(5, 5), int expand = 10, double threshold = 0.7);
// 检测
int CheckRun();
// 计算产品尺寸
int CalProductSize();
// 图片预处理
int ImgPreDet();
// 处理签的 预处理
int DetPreImage();
// 每个检测通道 都更新 屏蔽区域
int UpdateSheildMask(std::string strChannel, cv::Rect roi);
// 异显检测
int AI_Det_YX(const cv::Mat &cropImg);
// 缺失pol检测
int Detect_LackPol(const cv::Mat &cropImg);
std::string ConfigTypeToWebDetType(int nconfigType);
bool JudgeQXAnalysis(int nqx_configType, std::shared_ptr<DetLog> pQxlog);
// 设置新的检测参数
int SetNewConfig();
int GetParamidx(); // 获得参数索引
ChannelCheckFunction *GetChannelFuntion(std::string strChannelName); // 获得 通道的检测功能
private:
// 图片处理线程
std::shared_ptr<std::thread> ptr_thread_Run;
int Run(int nId); // 运行;
// 图片处理线程
std::shared_ptr<std::thread> ptr_thread_AI;
int set_cpu_id(const std::vector<int> &cpu_set_vec);
// 计算BLob 其他一些属性值。
int CalBlob_Other();
// 计算灰阶
int CalBLobMean_GrayDis();
// 分类
int GetClassImg(const cv::Mat &img, cv::Mat &AIdetImg, cv::Rect qx_roi, int detwidth, int detheight);
// 计算缺陷长度
float Cal_QXLen(cv::Mat qx_maskImg, int qx_type, float fsc_x, float fsc_y);
float CalImgScorl(cv::Mat det_img, cv::Mat up_img);
float CalImgScorl_t(cv::Mat det_img, cv::Mat up_img);
// 获取检测结果blob用于后续分析。
int GetCheckResultBLob();
// 亮的判断
int LDJudge(int nconfigtype, cv::Rect roi, float JudgeArea, int maxV, int hj, std::shared_ptr<DetLog> pQxlog);
// 使用 DP mask 进行二次判断
int UseDPMask(int L0, int nconfigtype, cv::Rect roi, float JudgeArea, int maxV, int hj, std::shared_ptr<DetLog> pQxlog);
// 更新亮点参数
int UPdateLDConfig();
// 获得所有blob
int GetALLBlob();
// 获得YX blob
int GetBLob_YX();
// 获得 缺pol blob
int GetBlob_LackPol();
// 获得 缺陷 blob
int GetBlob_QX();
// 获得 127cell blob
int GetBlob_127cell();
// 对AI mask图片进行 结果处理
int AIMaskDet();
// 轮廓处理
int Contours();
// 检测初始化
int CheckImgInit();
// 参数 核对
int ConfigCheck(cv::Mat img);
// 多线程方式处理
int AI_Detect_Thread();
// 多线程方式处理Other AI检测
int AI_Other_Det_Thread();
// 缺陷检测
int AI_Detect_QX();
// 127cell
int AI_Detect_127Cell();
// 缺陷分类
int AI_QX_Class_Thread();
// 多线程 任务运行管理器
int ThreadTask(int nId); // 运行;
// resize 图片
int ResizeImg();
// AI 推理任务函数
void TaskFun_AIDet(std::shared_ptr<TaskInfo> task);
// AI 推理任务函数
void TaskFun_AI_Other(std::shared_ptr<TaskInfo> task);
// 缺陷分类任务函数
void TaskFun_QxClass(std::shared_ptr<TaskInfo> task);
// 更新 检测区域
int Update_DetRoiList();
// 边缘检测
int Edge_Det();
// 临时绘制结果
int DrawResult_Step_1();
// 参数缺陷类型转 结果参数类型
int AIClassTypeToConfigType(int nAIQXType, cv::Rect qx_roi);
// 更新成像精度
int UpdateImgageScale();
// 把blob 汇总成 检测结果。
int BLobToDetResult();
// 二次计算缺陷面积
int ReCalQX_AreaAndLen(AI_SecondDet::DetConfigResult *pdetConfig);
// 创建检测 小图区域。
int CreatAIDetSmallRoi(const cv::Mat &detimg, std::shared_ptr<AIModel_Base> pAI_Model, std::vector<cv::Rect> &SmallList);
// 多区域分类
int AI_Classify_New(const cv::Mat &src_Img, cv::Rect qx_roi, float fjustarea, float *fmaxScore);
// 利用Up 画面过滤
float UseUpMaskAnalysis(cv::Rect teroi, const cv::Mat AIMaskImg, std::shared_ptr<DetLog> pQxlog);
// 缺陷是否要进行检测分析 缺陷是参数类型的缺陷
bool Judge_MarkLine_QX(int nqx_configType, cv::Rect detqx_Roi, std::shared_ptr<DetLog> pQxlog);
std::shared_ptr<AIFactory> AI_Factory;
private:
int m_nErrorCode; // 错误代码
bool m_bInitSucc; // 初始化状态
bool m_bExit; // 是否退出检测
int m_nThreadIdx; // 线程序号 //相机处理的顺序号,该序号 程序运行就已经固定
std::shared_ptr<AIMulThreadRunBase> runner;
// 所有的检测结果
std::shared_ptr<ImageAllResult> m_pImageAllResult;
std::shared_ptr<DetLog> m_pdetlog;
std::shared_ptr<One_Image_CheckResult_> m_pDetResult; // 检测结果 后续主要对pDetResult 继续分析
std::shared_ptr<shareImage> DetImgInfo_shareP;
// 检测结果
std::shared_ptr<CheckResult> m_CheckResult_shareP;
// 检测参数模块
ConfigBase *m_pConfig;
int m_nConfigIdx;
// 线程运行的一些参数
RunInfoST m_RunConfig;
float m_fImgage_Scale_X;
float m_fImgage_Scale_Y;
// 分析参数
AnalysisyConfigST m_AnalysisyConfig;
ChannelCheckFunction *m_pFuntion;
BaseCheckFunction *m_pbaseCheckFunction; // 基础检测
CommonConfigNodeST *m_pCommonAnalysisyConfig;
BasicConfig *m_pBasicConfig;
RegionConfigST *m_pRegionAnalysisyParam; // 分析参数
int m_QxInParamListIdx[CONFIG_QX_NAME_count];
cv::Rect m_CutRoi;
cv::Rect m_Crop_Roi_paramImg; // 裁切在参数模板图上的 位置。
std::string m_strCurDetCamChannel; // 当前处理的图片通道
std::string m_strCurDetChannel; // 当前处理的图片通道
int m_nRun_Status; // 运行状态:空闲,运行中,异常,。。。。。
int m_nCheckResultErrorCode;
Detect_ROI_Config m_DetRoiList; // 检测区域roi List
ERROR_DOTS_BLOBS blobs;
OtherDet_Config m_OtherDet_Config;
std::vector<QXImageResult> m_Draw_qxImageResult; // 缺陷小图结果
std::vector<cv::Rect> SmallRoiList;
int det_SmallImgNum;
std::string m_strRootPath_TA_cls;
std::string m_strRootPath_CA_cls;
std::string m_strSavePath_cls;
std::string m_strLastDate;
ImageStorage *m_pImageStorage;
AI_SecondDet m_SecondDet; // 二次分割检测
// AI 推理的所有结果, 可能会返回去。需要临时存储。
std::queue<std::shared_ptr<AIMulThreadRunBase::AITask>> m_AIMaskImgBLobQueue;
std::mutex mtx_AIMaskImgBLobQueue;
AIClassify m_AIClassify; // 缺陷分类
AI_Edge_QX_Det m_AIEdge_QX_Det; // 边缘缺陷检测
Task m_task;
std::shared_ptr<TaskInfo> m_AItask;
std::shared_ptr<TaskInfo> m_AI_Other_task; // 其他类型的AI推理
std::shared_ptr<TaskInfo> m_Classtask; // 缺陷分类任务
LD_ConfigT_ m_LDConfig;
LD_ConfigT_ m_LD_WTBConfig;
LD_ConfigT_ m_LD_HSConfig;
};
#endif