CDiandanAIShibieWorker.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "../pch/pch.h"
  2. #include "CDiandanAIShibieWorker.h"
  3. #include "CChengzhongWorker.h"
  4. #include "../tool/CAppEnv.h"
  5. CDiandanAIShibieWorker::CDiandanAIShibieWorker()
  6. {
  7. }
  8. CDiandanAIShibieWorker::~CDiandanAIShibieWorker()
  9. {
  10. int a = 1;
  11. }
  12. //启动工作线程
  13. void CDiandanAIShibieWorker::StartWork()
  14. {
  15. long int threadId = GetCurrentThreadId();
  16. //默认先关闭
  17. m_is_work = true;
  18. //创建一个新线程,专门处理AI识别的结果,避免因为AI识别的结果处理比较慢,导致界面卡顿
  19. std::thread(&CDiandanAIShibieWorker::HandleDiandanAIShibie, this).detach();
  20. }
  21. //结束工作线程
  22. void CDiandanAIShibieWorker::StopWork()
  23. {
  24. m_is_work = false;
  25. }
  26. void CDiandanAIShibieWorker::HandleDiandanAIShibie()
  27. {
  28. long int threadId = GetCurrentThreadId();
  29. std::wstring wsExePath = CSystem::getExePath();
  30. std::wstring wsProgramDir = CSystem::GetProgramDir();
  31. std::filesystem::path mainDir = wsProgramDir;
  32. std::string sMainDir = mainDir.string();
  33. std::string modelPath = sMainDir + "/ai/best_2026n_320.onnx"; // YOLO2026模型路径
  34. std::string openvino_modelPath = sMainDir + "/ai/best.xml"; // YOLO2026模型路径
  35. std::string openvino_configPath = sMainDir + "/ai/best.bin"; // YOLO2026模型路径
  36. std::string databasePath = sMainDir + "/image_features.db"; // SQLite数据库路径
  37. std::cout << "=== 图像AI识别线程启动 ===" << std::endl;
  38. std::cout << "模型路径: " << modelPath << std::endl;
  39. std::cout << "数据库路径: " << databasePath << std::endl;
  40. std::cout << "=========================================" << std::endl;
  41. m_yoloFeatureManager.loadModel(modelPath);
  42. while (m_is_work == true)
  43. {
  44. try
  45. {
  46. if (m_is_ai_shibie == false)
  47. {
  48. //说明没开启AI识别,就等着
  49. Sleep(2000);
  50. continue;
  51. }
  52. float weight = atof(CChengzhongWorker::GetInstance()->GetWeight().c_str());
  53. if (weight < 0.01)
  54. {
  55. //说明没有重量,没放东西到秤上面,那么就不识别
  56. Sleep(100);
  57. continue;
  58. }
  59. auto time_1 = std::chrono::high_resolution_clock::now();
  60. m_ai_shibie_foodname = m_yoloFeatureManager.ClassFromVideoCapture();
  61. if (m_ai_shibie_foodname != "Unknown")
  62. {
  63. std::cout << "检测到类别: " << m_ai_shibie_foodname << std::endl;
  64. }
  65. else
  66. {
  67. std::cout << "未检测到任何类别。" << std::endl;
  68. }
  69. auto time_2 = std::chrono::high_resolution_clock::now();
  70. auto duration_1 = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
  71. std::wstring msg = L"all time: " + std::to_wstring(duration_1.count()) + L" 毫秒";
  72. //DEBUG_LOG(msg.c_str());
  73. //LOG_INFO(msg);
  74. //主线程里面去处理界面刷新
  75. if (m_hwnd != NULL)
  76. {
  77. ::PostMessage(m_hwnd, WM_AI_RECOGNITION_SUCCESS, 0, 0);
  78. }
  79. }
  80. catch (const std::exception& e)
  81. {
  82. std::string aa = std::string(e.what());
  83. DEBUG_LOG(("提取特征失败: " + std::string(e.what())).c_str());
  84. continue;
  85. }
  86. }
  87. //走到这里说明线程要退出了,做一些清理工作
  88. CAppEnv::GetInstance()->DelWorkerNum();
  89. }