CDiandanAIShibieWorker.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "../pch/pch.h"
  2. #include "CDiandanAIShibieWorker.h"
  3. #include "CChengzhongWorker.h"
  4. #include "../tool/CAppEnv.h"
  5. #include "../ai/SQLiteVecManager.h"
  6. #include "CVideoCaptureWorker.h"
  7. #include "../tool/CSqlite3.h"
  8. CDiandanAIShibieWorker::CDiandanAIShibieWorker()
  9. {
  10. }
  11. CDiandanAIShibieWorker::~CDiandanAIShibieWorker()
  12. {
  13. }
  14. //启动工作线程
  15. void CDiandanAIShibieWorker::StartWork()
  16. {
  17. long int threadId = GetCurrentThreadId();
  18. m_is_work = true;
  19. //创建一个新线程,专门处理AI识别的结果,避免因为AI识别的结果处理比较慢,导致界面卡顿
  20. std::thread(&CDiandanAIShibieWorker::HandleDiandanAIShibie, this).detach();
  21. }
  22. //结束工作线程
  23. void CDiandanAIShibieWorker::StopWork()
  24. {
  25. m_is_work = false;
  26. }
  27. void CDiandanAIShibieWorker::HandleDiandanAIShibie()
  28. {
  29. long int threadId = GetCurrentThreadId();
  30. while (m_is_work == true)
  31. {
  32. try
  33. {
  34. if (m_is_ai_shibie == false)
  35. {
  36. //说明没开启AI识别,就等着
  37. Sleep(2000);
  38. continue;
  39. }
  40. float weight = atof(CChengzhongWorker::GetInstance()->GetWeight().c_str());
  41. if (weight < -0.01)
  42. {
  43. //说明没有重量,没放东西到秤上面,那么就不识别
  44. Sleep(100);
  45. continue;
  46. }
  47. auto time_1 = std::chrono::high_resolution_clock::now();
  48. cv::Mat image;
  49. CVideoCaptureWorker::GetInstance()->GetFrame(image);
  50. if (image.empty())
  51. {
  52. //DEBUG_LOG("从摄像头获取帧失败");
  53. Sleep(2000);
  54. continue;
  55. }
  56. m_ai_shibie_foodname = YoloFeatureManager::GetInstance()->Class(image);
  57. if (m_ai_shibie_foodname != "Unknown")
  58. {
  59. std::cout << "检测到类别: " << m_ai_shibie_foodname << std::endl;
  60. m_ai_shibie_foodname = CLewaimaiString::ANSIToUTF8(m_ai_shibie_foodname);
  61. }
  62. else
  63. {
  64. std::cout << "未检测到任何类别。" << std::endl;
  65. //开始调用向量数据库检索
  66. if (SQLiteVecManager::GetInstance()->getFeatureCount() > 0)
  67. {
  68. std::vector<float> feature_vector = YoloFeatureManager::GetInstance()->extractFeatures(image);
  69. std::vector<FeatureRecord> searchResults = SQLiteVecManager::GetInstance()->searchSimilarVectors(feature_vector, 5);
  70. if (!searchResults.empty())
  71. {
  72. std::cout << "向量数据库检索结果:" << std::endl;
  73. for (const auto& result : searchResults)
  74. {
  75. std::string food_id = result.foodId;
  76. std::string food_name = result.foodName;
  77. std::string image_name = result.imageName;
  78. std::string image_path = result.imagePath;
  79. float similarity = result.similarity;
  80. std::cout << "食品ID: " << food_id << ", 食品名称: " << food_name << ", 图片名称: " << image_name << ", 图片路径: " << image_path << ", 相似度: " << similarity << std::endl;
  81. CSqlite3 sqlite;
  82. CFood newFood;
  83. bool ret = sqlite.GetFoodById(food_id, newFood);
  84. if (!ret)
  85. {
  86. std::cout << "该相似的商品已被删除" << std::endl;
  87. Sleep(100);
  88. continue;
  89. }
  90. //UTF8格式,sqlite里面存的都是UTF8格式的字符串
  91. m_ai_shibie_foodname = newFood.name;
  92. }
  93. }
  94. else
  95. {
  96. std::cout << "向量数据库中没有相似的特征。" << std::endl;
  97. Sleep(100);
  98. continue;
  99. }
  100. }
  101. else
  102. {
  103. std::cout << "向量数据库中没有任何特征。" << std::endl;
  104. Sleep(100);
  105. continue;
  106. }
  107. }
  108. auto time_2 = std::chrono::high_resolution_clock::now();
  109. auto duration_1 = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
  110. std::wstring msg = L"all time: " + std::to_wstring(duration_1.count()) + L" 毫秒";
  111. //主线程里面去处理界面刷新
  112. if (m_hwnd != NULL)
  113. {
  114. ::PostMessage(m_hwnd, WM_AI_RECOGNITION_SUCCESS, 0, 0);
  115. }
  116. }
  117. catch (const std::exception& e)
  118. {
  119. std::string aa = std::string(e.what());
  120. DEBUG_LOG(("提取特征失败: " + std::string(e.what())).c_str());
  121. continue;
  122. }
  123. }
  124. //走到这里说明线程要退出了,做一些清理工作
  125. CAppEnv::GetInstance()->DelWorkerNum();
  126. }