|
@@ -8,8 +8,6 @@
|
|
|
#include <numeric>
|
|
#include <numeric>
|
|
|
#include <sstream>
|
|
#include <sstream>
|
|
|
|
|
|
|
|
-#include "../tool/debuglog.h"
|
|
|
|
|
-
|
|
|
|
|
#include "../worker/CVideoCaptureWorker.h"
|
|
#include "../worker/CVideoCaptureWorker.h"
|
|
|
|
|
|
|
|
#include "YoloClassName.h"
|
|
#include "YoloClassName.h"
|
|
@@ -46,7 +44,7 @@ void YoloFeatureManager::loadModel()
|
|
|
catch (const std::exception& e)
|
|
catch (const std::exception& e)
|
|
|
{
|
|
{
|
|
|
std::string aa = std::string(e.what());
|
|
std::string aa = std::string(e.what());
|
|
|
- DEBUG_LOG(("加载模型失败: " + std::string(e.what())).c_str());
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage("加载模型失败: " + std::string(e.what()));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -72,7 +70,7 @@ void YoloFeatureManager::loadModelForOpenVINO()
|
|
|
catch (const std::exception& e)
|
|
catch (const std::exception& e)
|
|
|
{
|
|
{
|
|
|
std::string aa = std::string(e.what());
|
|
std::string aa = std::string(e.what());
|
|
|
- DEBUG_LOG(("加载模型失败: " + std::string(e.what())).c_str());
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage(("加载模型失败: " + std::string(e.what())).c_str());
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -152,12 +150,12 @@ std::vector<float> YoloFeatureManager::extractFeatures(cv::Mat& image)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
float norm_before = cv::norm(featureMat, cv::NORM_L2);
|
|
float norm_before = cv::norm(featureMat, cv::NORM_L2);
|
|
|
- DEBUG_HELPER::debug_printf("归一化前 norm:%.6f\n", norm_before);
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessageFormat("归一化前 norm:%.6f\n", norm_before);
|
|
|
|
|
|
|
|
cv::normalize(featureMat, featureMat, 1.0, 0.0, cv::NORM_L2); //L2归一化
|
|
cv::normalize(featureMat, featureMat, 1.0, 0.0, cv::NORM_L2); //L2归一化
|
|
|
|
|
|
|
|
float norm_after = cv::norm(featureMat, cv::NORM_L2);
|
|
float norm_after = cv::norm(featureMat, cv::NORM_L2);
|
|
|
- DEBUG_HELPER::debug_printf("归一化后 norm:%.6f\n", norm_after);
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessageFormat("归一化后 norm:%.6f\n", norm_after);
|
|
|
|
|
|
|
|
// 将Mat格式的特征转换为vector<float>(方便后续计算/存储)
|
|
// 将Mat格式的特征转换为vector<float>(方便后续计算/存储)
|
|
|
std::vector<float> feature_vector;
|
|
std::vector<float> feature_vector;
|
|
@@ -168,56 +166,54 @@ std::vector<float> YoloFeatureManager::extractFeatures(cv::Mat& image)
|
|
|
|
|
|
|
|
auto duration_1 = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
|
|
auto duration_1 = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
|
|
|
std::wstring msg = L"图片处理耗时: " + std::to_wstring(duration_1.count()) + L" 毫秒";
|
|
std::wstring msg = L"图片处理耗时: " + std::to_wstring(duration_1.count()) + L" 毫秒";
|
|
|
- DEBUG_LOG(msg.c_str());
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage(msg.c_str());
|
|
|
|
|
|
|
|
auto duration_2 = std::chrono::duration_cast<std::chrono::milliseconds>(time_3 - time_2);
|
|
auto duration_2 = std::chrono::duration_cast<std::chrono::milliseconds>(time_3 - time_2);
|
|
|
std::wstring msg2 = L"模型推理耗时: " + std::to_wstring(duration_2.count()) + L" 毫秒";
|
|
std::wstring msg2 = L"模型推理耗时: " + std::to_wstring(duration_2.count()) + L" 毫秒";
|
|
|
- DEBUG_LOG(msg2.c_str());
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage(msg2.c_str());
|
|
|
|
|
|
|
|
auto totalDuration = std::chrono::duration_cast<std::chrono::milliseconds>(time_3 - time_1);
|
|
auto totalDuration = std::chrono::duration_cast<std::chrono::milliseconds>(time_3 - time_1);
|
|
|
std::wstring msg4 = L"总耗时: " + std::to_wstring(totalDuration.count()) + L" 毫秒";
|
|
std::wstring msg4 = L"总耗时: " + std::to_wstring(totalDuration.count()) + L" 毫秒";
|
|
|
- DEBUG_LOG(msg4.c_str());
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage(msg4.c_str());
|
|
|
|
|
|
|
|
return feature_vector;
|
|
return feature_vector;
|
|
|
}
|
|
}
|
|
|
catch (const std::exception& e)
|
|
catch (const std::exception& e)
|
|
|
{
|
|
{
|
|
|
std::string aa = std::string(e.what());
|
|
std::string aa = std::string(e.what());
|
|
|
- DEBUG_LOG(("提取特征失败: " + std::string(e.what())).c_str());
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage("提取特征失败: " + std::string(e.what()));
|
|
|
return {};
|
|
return {};
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-namespace
|
|
|
|
|
|
|
+void YoloFeatureManager::DebugTopResults(const cv::Mat& output, int topK)
|
|
|
{
|
|
{
|
|
|
- void DebugTopResults(const cv::Mat& output, int topK = 10)
|
|
|
|
|
- {
|
|
|
|
|
- cv::Mat scores = output.reshape(1, 1);
|
|
|
|
|
- const int count = scores.cols;
|
|
|
|
|
- topK = std::min(topK, count);
|
|
|
|
|
-
|
|
|
|
|
- std::vector<int> indices(count);
|
|
|
|
|
- std::iota(indices.begin(), indices.end(), 0);
|
|
|
|
|
|
|
+ cv::Mat scores = output.reshape(1, 1);
|
|
|
|
|
+ const int count = scores.cols;
|
|
|
|
|
+ topK = std::min(topK, count);
|
|
|
|
|
|
|
|
- std::partial_sort(indices.begin(), indices.begin() + topK, indices.end(),
|
|
|
|
|
- [&scores](int left, int right)
|
|
|
|
|
- {
|
|
|
|
|
- return scores.at<float>(0, left) > scores.at<float>(0, right);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ std::vector<int> indices(count);
|
|
|
|
|
+ std::iota(indices.begin(), indices.end(), 0);
|
|
|
|
|
|
|
|
- std::string message = "YOLO分类 Top " + std::to_string(topK) + " 结果:";
|
|
|
|
|
- for (int i = 0; i < topK; ++i)
|
|
|
|
|
|
|
+ std::partial_sort(indices.begin(), indices.begin() + topK, indices.end(),
|
|
|
|
|
+ [&scores](int left, int right)
|
|
|
{
|
|
{
|
|
|
- const int classId = indices[i];
|
|
|
|
|
- const float confidence = scores.at<float>(0, classId);
|
|
|
|
|
|
|
+ return scores.at<float>(0, left) > scores.at<float>(0, right);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- message += "\nTop " + std::to_string(i + 1) +
|
|
|
|
|
- ": id=" + std::to_string(classId) +
|
|
|
|
|
- ", confidence=" + std::to_string(confidence);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ std::string message = "YOLO分类 Top " + std::to_string(topK) + " 结果:";
|
|
|
|
|
+ for (int i = 0; i < topK; ++i)
|
|
|
|
|
+ {
|
|
|
|
|
+ const int classId = indices[i];
|
|
|
|
|
+ const float confidence = scores.at<float>(0, classId);
|
|
|
|
|
|
|
|
- DEBUG_LOG(message.c_str());
|
|
|
|
|
|
|
+ message += "\nTop " + std::to_string(i + 1) +
|
|
|
|
|
+ ": id=" + std::to_string(classId) +
|
|
|
|
|
+ ": name=" + this->getClassName(classId) +
|
|
|
|
|
+ ", confidence=" + std::to_string(confidence);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ m_topResultMessage = message;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
std::string YoloFeatureManager::Class(cv::Mat & image)
|
|
std::string YoloFeatureManager::Class(cv::Mat & image)
|
|
@@ -239,13 +235,16 @@ std::string YoloFeatureManager::Class(cv::Mat & image)
|
|
|
// ====================== 模型推理 ======================
|
|
// ====================== 模型推理 ======================
|
|
|
cv::Mat output = net.forward(); // 输出形状:1x1000(对应ImageNet 1000类)
|
|
cv::Mat output = net.forward(); // 输出形状:1x1000(对应ImageNet 1000类)
|
|
|
|
|
|
|
|
- DebugTopResults(output);
|
|
|
|
|
|
|
+ this->DebugTopResults(output);
|
|
|
|
|
|
|
|
auto time_2 = std::chrono::high_resolution_clock::now();
|
|
auto time_2 = std::chrono::high_resolution_clock::now();
|
|
|
-
|
|
|
|
|
auto totalDuration = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
|
|
auto totalDuration = std::chrono::duration_cast<std::chrono::milliseconds>(time_2 - time_1);
|
|
|
- std::wstring msg = L"Class 总耗时: " + std::to_wstring(totalDuration.count()) + L" 毫秒";
|
|
|
|
|
- DEBUG_LOG(msg.c_str());
|
|
|
|
|
|
|
+
|
|
|
|
|
+ std::wstring msg = L"Class 总耗时: " + std::to_wstring(totalDuration.count()) + L" 毫秒\r\n";
|
|
|
|
|
+ msg += CLewaimaiString::ANSIToUnicode(m_topResultMessage);
|
|
|
|
|
+
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage(msg.c_str());
|
|
|
|
|
+ CLewaimaiLog::OutputMessageBox(msg.c_str());
|
|
|
|
|
|
|
|
// ====================== 解析结果 ======================
|
|
// ====================== 解析结果 ======================
|
|
|
int topClassIdx = this->getTopClass(output);
|
|
int topClassIdx = this->getTopClass(output);
|
|
@@ -264,7 +263,7 @@ std::string YoloFeatureManager::Class(cv::Mat & image)
|
|
|
}
|
|
}
|
|
|
catch (const std::exception& e)
|
|
catch (const std::exception& e)
|
|
|
{
|
|
{
|
|
|
- DEBUG_LOG(("YOLO分类失败: " + std::string(e.what())).c_str());
|
|
|
|
|
|
|
+ CLewaimaiLog::OutputDebugMessage(("YOLO分类失败: " + std::string(e.what())).c_str());
|
|
|
return {};
|
|
return {};
|
|
|
}
|
|
}
|
|
|
|
|
|