/* * @Author: xiewenji 527774126@qq.com * @Date: 2025-09-03 09:48:19 * @LastEditors: xiewenji 527774126@qq.com * @LastEditTime: 2025-09-14 20:25:35 * @FilePath: /AI_SO_Test/AIEngineModule/include_base/AI_Factory.h * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ /* * @Author: xiewenji 527774126@qq.com * @Date: 2025-07-04 14:41:18 * @LastEditors: xiewenji 527774126@qq.com * @LastEditTime: 2025-09-03 10:27:59 * @FilePath: /AI_SO_Test/AIEngineModule/include_base/AI_Engine_Base.h * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ #ifndef AI_Factory_H_ #define AI_Factory_H_ #include #include #include #include #include #include #include #include #include // GPU 可用信息 struct GPU_Config { bool gpu_0; bool gpu_1; GPU_Config() { gpu_0 = true; gpu_1 = true; } int GetNum() { int num = 0; if (gpu_0) { num++; } if (gpu_1) { num++; } return num; } void copy(GPU_Config tem) { this->gpu_0 = tem.gpu_0; this->gpu_1 = tem.gpu_1; } }; // AI 模型 基础接口 class AIModel_Base { public: enum Model_Input_Type { Input_NULL, Input_HW, Input_CHW, Input_HWC, }; // 模型相关参数 struct AIModelRun_Config { int Stream_num; // 流的个数 bool IsClass; // 是分类 std::string strPath; // 模型类型 std::string strName; // 名称,别名 Model_Input_Type inputType; // 数据格式类型 GPU_Config gpuconfig; AIModelRun_Config() { strPath = ""; strName = ""; inputType = Input_NULL; Stream_num = 1; IsClass = false; } void Copy(AIModelRun_Config tem) { this->Stream_num = tem.Stream_num; this->strPath = tem.strPath; this->strName = tem.strName; this->inputType = tem.inputType; this->IsClass = tem.IsClass; this->gpuconfig.copy(tem.gpuconfig); } void print(std::string str) { printf("%s:Stream_num %d Path:%s inputType %d\n", str.c_str(), Stream_num, strPath.c_str(), inputType); } }; struct AI_Image { int channel; int width; int height; AI_Image() { channel = 0; width = 0; height = 0; } void copy(AI_Image tem) { this->channel = tem.channel; this->height = tem.height; this->width = tem.width; } void print() { printf("[c w h] %d %d %d\n", channel, width, height); } }; public: virtual ~AIModel_Base() = default; static std::shared_ptr GetInstance(); // 初始化函数 virtual int Init(AIModelRun_Config config) = 0; virtual int AIDet(const cv::Mat &inImg, cv::Mat &outimg) = 0; virtual int AIDet(const cv::Mat &inImg, cv::Mat &outimg0, cv::Mat &outimg1) = 0; virtual int AIClass(const cv::Mat &inImg, float *fmaxScore) = 0; public: bool m_bInitSuccess = false; AI_Image input_0; AI_Image input_1; AI_Image output_0; AI_Image output_1; AI_Image output_2; }; // 管理所有 模型 class AIFactory { public: AIFactory(); ~AIFactory(); static std::shared_ptr GetInstance(); // 初始化所有模型 int InitALLAIModle(GPU_Config gupconfig); private: public: std::shared_ptr Defect; std::shared_ptr Defect_QueXi; std::shared_ptr Class; std::shared_ptr Pin_Loc; std::shared_ptr Align_Outer; std::shared_ptr Align_Inner; private: // 模型是否都加载完成了。 bool m_bInitSucc = false; }; // 多线程推理 class AIMulThreadRunBase { public: struct AITask { cv::Rect roi; cv::Size srcroi; int id; cv::Mat input; std::shared_ptr output; std::shared_ptr engine; bool bclass = false; int cls_label = 0; float cls_score = 0.0; }; public: AIMulThreadRunBase(); ~AIMulThreadRunBase(); void Start(int num_threads = 2); void Stop(); void SubmitTask(std::shared_ptr task); bool PopResult(std::shared_ptr &result); // 新增接口,查询当前正在处理的任务数 int GetProcessingCount() const { return m_processing_count_.load(); } std::atomic m_detnum; // 当前正在执行任务数 private: void ThreadLoop(); private: std::queue> m_tasks_; std::queue> m_results_; std::vector m_workers_; std::mutex m_task_mutex_; std::mutex m_result_mutex_; std::mutex m_AI_mutex_; std::condition_variable m_task_cv_; std::atomic m_running_; std::atomic m_processing_count_; // 当前正在执行任务数 }; #endif