From 03302cf155f4ac463465ffceae3863bc2ef6a328 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 29 Apr 2023 18:28:31 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=88=98=E6=96=97=E8=AF=86=E5=88=AB?= =?UTF-8?q?=EF=BC=8C=E5=88=A0=E6=BC=8F=E4=BA=86=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battle/BattleOperatorsImageAnalyzer.cpp | 121 ------------------ 1 file changed, 121 deletions(-) delete mode 100644 src/MaaCore/Vision/Battle/BattleOperatorsImageAnalyzer.cpp diff --git a/src/MaaCore/Vision/Battle/BattleOperatorsImageAnalyzer.cpp b/src/MaaCore/Vision/Battle/BattleOperatorsImageAnalyzer.cpp deleted file mode 100644 index 9a3a310d03..0000000000 --- a/src/MaaCore/Vision/Battle/BattleOperatorsImageAnalyzer.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include "BattleOperatorsImageAnalyzer.h" - -#include -#include -#include - -#include "Utils/NoWarningCV.h" -#include - -#include "Config/Miscellaneous/TilePack.h" -#include "Config/OnnxSessions.h" -#include "Config/TaskData.h" -#include "Utils/Logger.hpp" - -bool asst::BattleOperatorsImageAnalyzer::analyze() -{ - LogTraceFunction; - - - const double x_scale = 640.0 / m_image.cols; - const double y_scale = 640.0 / m_image.rows; - - cv::Mat image; - cv::resize(m_image, image, cv::Size(), x_scale, y_scale, cv::INTER_AREA); - std::vector input = image_to_tensor(image); - - auto memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU); - constexpr int64_t batch_size = 1; - std::array input_shape { batch_size, image.channels(), image.cols, image.rows }; - - Ort::Value input_tensor = Ort::Value::CreateTensor(memory_info, input.data(), input.size(), - input_shape.data(), input_shape.size()); - - auto& session = OnnxSessions::get_instance().get("operators_det"); - - Ort::AllocatorWithDefaultOptions allocator; - std::string input_name = session.GetInputNameAllocated(0, allocator).get(); - std::string output_name = session.GetOutputNameAllocated(0, allocator).get(); - std::vector input_names = { input_name.c_str() }; - std::vector output_names = { output_name.c_str() }; - - Ort::RunOptions run_options; - auto output_tensors = session.Run(run_options, input_names.data(), &input_tensor, input_names.size(), - output_names.data(), output_names.size()); - - const float* raw_output = output_tensors[0].GetTensorData(); - // output_shape is { 1, 5, 8400 } - std::vector output_shape = output_tensors[0].GetTensorTypeAndShapeInfo().GetShape(); - - // yolov8 的 onnx 输出和前面的 v5, v7 等似乎不太一样,目前网上 yolov8 的 demo 较少,文档也没找到 - // 这里的输出解析是我跟着数据推测的: - // center_x0, center_x1, ..... center_x8399 - // center_y0, center_y1, ..... center_y8399 - // w0, w1, ..... w8399 - // h0, h1, ..... h8399 - // conf0, conf1, ..... conf8399 - // 如果后面要做多分类,可能得再看下怎么改(我也不知道shape会变成啥样) - std::vector> output(output_shape[1]); - for (int64_t i = 0; i < output_shape[1]; i++) { - output[i] = std::vector(raw_output + i * output_shape[2], raw_output + (i + 1) * output_shape[2]); - } - - std::vector all_results; - const auto& conf_vec = output.back(); - for (size_t i = 0; i < conf_vec.size(); ++i) { - float score = conf_vec[i]; - constexpr float Threshold = 0.3f; - if (score < Threshold) { - continue; - } - int center_x = static_cast(output[0][i] / x_scale); - int center_y = static_cast(output[1][i] / y_scale); - int w = static_cast(output[2][i] / x_scale); - int h = static_cast(output[3][i] / y_scale); - - int x = center_x - w / 2; - int y = center_y - h / 2; - Rect rect { x, y, w, h }; - all_results.emplace_back(Box { Cls::Operator, rect, score }); - } - - // NMS - constexpr double NmsThreshold = 0.7f; - std::sort(all_results.begin(), all_results.end(), [](const Box& a, const Box& b) { return a.score > b.score; }); - std::vector nms_results; - for (size_t i = 0; i < all_results.size(); ++i) { - const auto& box = all_results[i]; - if (box.score < 0.1f) { - continue; - } - nms_results.emplace_back(box); - for (size_t j = i + 1; j < all_results.size(); ++j) { - auto& box2 = all_results[j]; - if (box2.score < 0.1f) { - continue; - } - int iou_area = (make_rect(box.rect) & make_rect(box2.rect)).area(); - if (iou_area > NmsThreshold * box2.rect.area()) { - box2.score = 0; - } - } - } - -#ifdef ASST_DEBUG - int draw_offset_y = static_cast(m_image.rows * -0.15); - int draw_offset_h = static_cast(m_image.rows * 0.13); - for (const auto& box : nms_results) { - Rect draw_rect = box.rect; - draw_rect.y += draw_offset_y; - draw_rect.height += draw_offset_h; - cv::rectangle(m_image_draw, make_rect(draw_rect), cv::Scalar(0, 0, 255), 5); - cv::putText(m_image_draw, std::to_string(box.score), cv::Point(draw_rect.x, draw_rect.y - 10), - cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0, 0, 255), 2); - m_draw_rect.emplace_back(draw_rect); - } -#endif - - m_results = std::move(nms_results); - - return true; -}