| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #include "../pch/pch.h"
- #include "CDiandanAIShibieWorker.h"
- #include "CChengzhongWorker.h"
- #include "../tool/CAppEnv.h"
- CDiandanAIShibieWorker::CDiandanAIShibieWorker()
- {
- }
- CDiandanAIShibieWorker::~CDiandanAIShibieWorker()
- {
- int a = 1;
- }
- //启动工作线程
- void CDiandanAIShibieWorker::StartWork()
- {
- long int threadId = GetCurrentThreadId();
- //默认先关闭
- m_is_work = true;
- //创建一个新线程,专门处理AI识别的结果,避免因为AI识别的结果处理比较慢,导致界面卡顿
- std::thread(&CDiandanAIShibieWorker::HandleDiandanAIShibie, this).detach();
- }
- //结束工作线程
- void CDiandanAIShibieWorker::StopWork()
- {
- m_is_work = false;
- }
- void CDiandanAIShibieWorker::HandleDiandanAIShibie()
- {
- long int threadId = GetCurrentThreadId();
- std::wstring wsExePath = CSystem::getExePath();
- std::wstring wsProgramDir = CSystem::GetProgramDir();
- std::filesystem::path mainDir = wsProgramDir;
- std::string sMainDir = mainDir.string();
- std::string modelPath = sMainDir + "/ai/best_2026n_320.onnx"; // YOLO2026模型路径
- std::string openvino_modelPath = sMainDir + "/ai/best.xml"; // YOLO2026模型路径
- std::string openvino_configPath = sMainDir + "/ai/best.bin"; // YOLO2026模型路径
- std::string databasePath = sMainDir + "/image_features.db"; // SQLite数据库路径
- std::cout << "=== 图像AI识别线程启动 ===" << std::endl;
- std::cout << "模型路径: " << modelPath << std::endl;
- std::cout << "数据库路径: " << databasePath << std::endl;
- std::cout << "=========================================" << std::endl;
- m_yoloFeatureManager.loadModel(modelPath);
- while (m_is_work == true)
- {
- try
- {
- if (m_is_ai_shibie == false)
- {
- //说明没开启AI识别,就等着
- Sleep(2000);
- continue;
- }
- float weight = atof(CChengzhongWorker::GetInstance()->GetWeight().c_str());
- if (weight < 0.01)
- {
- //说明没有重量,没放东西到秤上面,那么就不识别
- Sleep(100);
- continue;
- }
- auto time_1 = std::chrono::high_resolution_clock::now();
- m_ai_shibie_foodname = m_yoloFeatureManager.ClassFromVideoCapture();
- if (m_ai_shibie_foodname != "Unknown")
- {
- std::cout << "检测到类别: " << m_ai_shibie_foodname << std::endl;
- }
- else
- {
- std::cout << "未检测到任何类别。" << std::endl;
- }
- auto time_2 = std::chrono::high_resolution_clock::now();
- auto duration_1 = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
- std::wstring msg = L"all time: " + std::to_wstring(duration_1.count()) + L" 毫秒";
- //DEBUG_LOG(msg.c_str());
- //LOG_INFO(msg);
- //主线程里面去处理界面刷新
- if (m_hwnd != NULL)
- {
- ::PostMessage(m_hwnd, WM_AI_RECOGNITION_SUCCESS, 0, 0);
- }
- }
- catch (const std::exception& e)
- {
- std::string aa = std::string(e.what());
- DEBUG_LOG(("提取特征失败: " + std::string(e.what())).c_str());
- continue;
- }
- }
- //走到这里说明线程要退出了,做一些清理工作
- CAppEnv::GetInstance()->DelWorkerNum();
- }
|