CDiandanAIShibieWorker.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "../pch/pch.h"
  2. #include "CDiandanAIShibieWorker.h"
  3. #include "CChengzhongWorker.h"
  4. //启动工作线程
  5. void CDiandanAIShibieWorker::StartWork()
  6. {
  7. //默认先关闭
  8. m_is_work = true;
  9. //创建一个新线程,专门处理AI识别的结果,避免因为AI识别的结果处理比较慢,导致界面卡顿
  10. std::thread(&CDiandanAIShibieWorker::HandleDiandanAIShibie, this).detach();
  11. }
  12. //结束工作线程
  13. void CDiandanAIShibieWorker::StopWork()
  14. {
  15. m_is_work = false;
  16. }
  17. void CDiandanAIShibieWorker::HandleDiandanAIShibie()
  18. {
  19. std::wstring wsExePath = CSystem::getExePath();
  20. std::wstring wsProgramDir = CSystem::GetProgramDir();
  21. std::filesystem::path mainDir = wsProgramDir;
  22. std::string sMainDir = mainDir.string();
  23. std::string modelPath = sMainDir + "/ai/best_448.onnx"; // YOLO2026模型路径
  24. std::string databasePath = sMainDir + "/image_features.db"; // SQLite数据库路径
  25. std::cout << "=== 图像AI识别线程启动 ===" << std::endl;
  26. std::cout << "模型路径: " << modelPath << std::endl;
  27. std::cout << "数据库路径: " << databasePath << std::endl;
  28. std::cout << "=========================================" << std::endl;
  29. m_yoloFeatureManager.loadModel(modelPath);
  30. while (m_is_work == true)
  31. {
  32. if (m_is_ai_shibie == false)
  33. {
  34. //说明没开启AI识别,就等着
  35. Sleep(2000);
  36. continue;
  37. }
  38. float weight = atof(CChengzhongWorker::GetInstance()->GetWeight().c_str());
  39. if (weight < -0.01)
  40. {
  41. //说明没有重量,没放东西到秤上面,那么就不识别
  42. Sleep(100);
  43. continue;
  44. }
  45. //auto time_1 = std::chrono::high_resolution_clock::now();
  46. m_ai_shibie_foodname = m_yoloFeatureManager.ClassFromVideoCapture();
  47. if (m_ai_shibie_foodname != "Unknown")
  48. {
  49. std::cout << "检测到类别: " << m_ai_shibie_foodname << std::endl;
  50. }
  51. else
  52. {
  53. std::cout << "未检测到任何类别。" << std::endl;
  54. }
  55. //auto time_2 = std::chrono::high_resolution_clock::now();
  56. //auto duration_1 = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
  57. //std::wstring msg = L"摄像头识别耗时: " + std::to_wstring(duration_1.count()) + L" 毫秒";
  58. //DEBUG_LOG(msg.c_str());
  59. //主线程里面去处理界面刷新
  60. if (m_hwnd != NULL)
  61. {
  62. ::SendMessage(m_hwnd, WM_AI_RECOGNITION_SUCCESS, 0, 0);
  63. }
  64. }
  65. }