CDiandanAIShibieWorker.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. while (m_is_work == true)
  30. {
  31. try
  32. {
  33. if (m_is_ai_shibie == false)
  34. {
  35. //说明没开启AI识别,就等着
  36. Sleep(2000);
  37. continue;
  38. }
  39. float weight = atof(CChengzhongWorker::GetInstance()->GetWeight().c_str());
  40. if (weight < 0.01)
  41. {
  42. //说明没有重量,没放东西到秤上面,那么就不识别
  43. Sleep(100);
  44. continue;
  45. }
  46. auto time_1 = std::chrono::high_resolution_clock::now();
  47. m_ai_shibie_foodname = YoloFeatureManager::GetInstance()->ClassFromVideoCapture();
  48. if (m_ai_shibie_foodname != "Unknown")
  49. {
  50. std::cout << "检测到类别: " << m_ai_shibie_foodname << std::endl;
  51. }
  52. else
  53. {
  54. std::cout << "未检测到任何类别。" << std::endl;
  55. }
  56. auto time_2 = std::chrono::high_resolution_clock::now();
  57. auto duration_1 = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
  58. std::wstring msg = L"all time: " + std::to_wstring(duration_1.count()) + L" 毫秒";
  59. //DEBUG_LOG(msg.c_str());
  60. //LOG_INFO(msg);
  61. //主线程里面去处理界面刷新
  62. if (m_hwnd != NULL)
  63. {
  64. ::PostMessage(m_hwnd, WM_AI_RECOGNITION_SUCCESS, 0, 0);
  65. }
  66. }
  67. catch (const std::exception& e)
  68. {
  69. std::string aa = std::string(e.what());
  70. DEBUG_LOG(("提取特征失败: " + std::string(e.what())).c_str());
  71. continue;
  72. }
  73. }
  74. //走到这里说明线程要退出了,做一些清理工作
  75. CAppEnv::GetInstance()->DelWorkerNum();
  76. }