| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include "../pch/pch.h"
- #include "CDiandanAIShibieWorker.h"
- #include "CChengzhongWorker.h"
- #include "../tool/CAppEnv.h"
- #include "../ai/SQLiteVecManager.h"
- #include "CVideoCaptureWorker.h"
- #include "../tool/CSqlite3.h"
- CDiandanAIShibieWorker::CDiandanAIShibieWorker()
- {
- }
- CDiandanAIShibieWorker::~CDiandanAIShibieWorker()
- {
-
- }
- //启动工作线程
- 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();
- 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();
- cv::Mat image;
- CVideoCaptureWorker::GetInstance()->GetFrame(image);
- if (image.empty())
- {
- //DEBUG_LOG("从摄像头获取帧失败");
- Sleep(2000);
- continue;
- }
- m_ai_shibie_foodname = YoloFeatureManager::GetInstance()->Class(image);
- if (m_ai_shibie_foodname != "Unknown")
- {
- std::cout << "检测到类别: " << m_ai_shibie_foodname << std::endl;
- m_ai_shibie_foodname = CLewaimaiString::ANSIToUTF8(m_ai_shibie_foodname);
- }
- else
- {
- std::cout << "未检测到任何类别。" << std::endl;
- //开始调用向量数据库检索
- if (SQLiteVecManager::GetInstance()->getFeatureCount() > 0)
- {
- std::vector<float> feature_vector = YoloFeatureManager::GetInstance()->extractFeatures(image);
- std::vector<FeatureRecord> searchResults = SQLiteVecManager::GetInstance()->searchSimilarVectors(feature_vector, 5);
- if (!searchResults.empty())
- {
- std::cout << "向量数据库检索结果:" << std::endl;
- for (const auto& result : searchResults)
- {
- std::string food_id = result.foodId;
- std::string food_name = result.foodName;
- std::string image_name = result.imageName;
- std::string image_path = result.imagePath;
- float similarity = result.similarity;
- std::cout << "食品ID: " << food_id << ", 食品名称: " << food_name << ", 图片名称: " << image_name << ", 图片路径: " << image_path << ", 相似度: " << similarity << std::endl;
- CSqlite3 sqlite;
- CFood newFood;
- bool ret = sqlite.GetFoodById(food_id, newFood);
- if (!ret)
- {
- std::cout << "该相似的商品已被删除" << std::endl;
- Sleep(100);
- continue;
- }
- //UTF8格式,sqlite里面存的都是UTF8格式的字符串
- m_ai_shibie_foodname = newFood.name;
- }
- }
- else
- {
- std::cout << "向量数据库中没有相似的特征。" << std::endl;
- Sleep(100);
- continue;
- }
- }
- else
- {
- std::cout << "向量数据库中没有任何特征。" << std::endl;
- Sleep(100);
- continue;
- }
- }
- 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" 毫秒";
- //主线程里面去处理界面刷新
- 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();
- }
|