From 7fbafa8d28677ea6fec2337b40611e72a8e3bdb1 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 29 Mar 2024 13:35:06 +0800 Subject: [PATCH] refactor: done --- .../Instance/InstanceInternalAPI.hpp | 8 +- source/MaaFramework/Instance/InstanceMgr.cpp | 33 +++-- source/MaaFramework/Instance/InstanceMgr.h | 8 +- source/MaaFramework/Task/Actuator.cpp | 20 +-- source/MaaFramework/Task/CustomAction.cpp | 24 ++-- source/MaaFramework/Task/CustomAction.h | 8 +- source/MaaFramework/Task/Recognizer.cpp | 127 +++++++++--------- source/MaaFramework/Task/Recognizer.h | 5 +- source/MaaFramework/Vision/ColorMatcher.cpp | 6 +- source/MaaFramework/Vision/ColorMatcher.h | 8 +- .../MaaFramework/Vision/CustomRecognizer.cpp | 4 +- source/MaaFramework/Vision/CustomRecognizer.h | 6 +- source/MaaFramework/Vision/FeatureMatcher.cpp | 4 +- source/MaaFramework/Vision/FeatureMatcher.h | 8 +- .../Vision/NeuralNetworkClassifier.cpp | 2 +- .../Vision/NeuralNetworkClassifier.h | 8 +- .../Vision/NeuralNetworkDetector.cpp | 3 +- .../Vision/NeuralNetworkDetector.h | 8 +- source/MaaFramework/Vision/OCRer.cpp | 2 +- source/MaaFramework/Vision/OCRer.h | 8 +- .../Vision/TemplateComparator.cpp | 2 +- .../MaaFramework/Vision/TemplateComparator.h | 8 ++ .../MaaFramework/Vision/TemplateMatcher.cpp | 2 +- source/MaaFramework/Vision/TemplateMatcher.h | 8 +- source/MaaFramework/Vision/VisionBase.h | 8 +- source/MaaFramework/Vision/VisionUtils.hpp | 2 +- 26 files changed, 176 insertions(+), 154 deletions(-) diff --git a/source/MaaFramework/Instance/InstanceInternalAPI.hpp b/source/MaaFramework/Instance/InstanceInternalAPI.hpp index 89b5ad06a..7dafc2835 100644 --- a/source/MaaFramework/Instance/InstanceInternalAPI.hpp +++ b/source/MaaFramework/Instance/InstanceInternalAPI.hpp @@ -6,6 +6,8 @@ #include #include "Conf/Conf.h" +#include "MaaFramework/Task/MaaCustomAction.h" +#include "MaaFramework/Task/MaaCustomRecognizer.h" #include "Utils/NonCopyable.hpp" MAA_RES_NS_BEGIN @@ -24,14 +26,12 @@ struct CustomRecognizerSession { MaaCustomRecognizerHandle recognizer = nullptr; MaaTransparentArg recognizer_arg = nullptr; - InstanceInternalAPI* inst = nullptr; }; struct CustomActionSession { MaaCustomActionHandle action = nullptr; MaaTransparentArg action_arg = nullptr; - InstanceInternalAPI* inst = nullptr; }; struct InstanceInternalAPI : public NonCopyable @@ -41,8 +41,8 @@ struct InstanceInternalAPI : public NonCopyable virtual MAA_CTRL_NS::ControllerAgent* inter_controller() = 0; virtual InstanceStatus* inter_status() = 0; virtual void notify(std::string_view msg, const json::value& details = json::value()) = 0; - virtual CustomRecognizerSession custom_recognizer(const std::string& name) = 0; - virtual CustomActionSession custom_action(const std::string& name) = 0; + virtual CustomRecognizerSession* custom_recognizer_session(const std::string& name) = 0; + virtual CustomActionSession* custom_action_session(const std::string& name) = 0; }; MAA_NS_END diff --git a/source/MaaFramework/Instance/InstanceMgr.cpp b/source/MaaFramework/Instance/InstanceMgr.cpp index 87aa24216..c16a989d9 100644 --- a/source/MaaFramework/Instance/InstanceMgr.cpp +++ b/source/MaaFramework/Instance/InstanceMgr.cpp @@ -140,21 +140,20 @@ bool InstanceMgr::register_custom_recognizer( return false; } - auto recognizer_ptr = - std::make_shared(handle, handle_arg, this); - return custom_recognizers_.insert_or_assign(std::move(name), std::move(recognizer_ptr)).second; + CustomRecognizerSession session { handle, handle_arg }; + return custom_recognizer_sessions_.insert_or_assign(std::move(name), std::move(session)).second; } bool InstanceMgr::unregister_custom_recognizer(std::string name) { LogInfo << VAR(name); - return custom_recognizers_.erase(name) > 0; + return custom_recognizer_sessions_.erase(name) > 0; } void InstanceMgr::clear_custom_recognizer() { LogInfo; - custom_recognizers_.clear(); + custom_recognizer_sessions_.clear(); } bool InstanceMgr::register_custom_action( @@ -167,20 +166,20 @@ bool InstanceMgr::register_custom_action( LogError << "Invalid handle"; return false; } - auto action_ptr = std::make_shared(handle, handle_arg, this); - return custom_actions_.insert_or_assign(std::move(name), std::move(action_ptr)).second; + CustomActionSession session { handle, handle_arg }; + return custom_action_sessions_.insert_or_assign(std::move(name), std::move(session)).second; } bool InstanceMgr::unregister_custom_action(std::string name) { LogInfo << VAR(name); - return custom_actions_.erase(name) > 0; + return custom_action_sessions_.erase(name) > 0; } void InstanceMgr::clear_custom_action() { LogInfo; - custom_actions_.clear(); + custom_action_sessions_.clear(); } MaaStatus InstanceMgr::task_status(MaaTaskId task_id) const @@ -263,24 +262,24 @@ void InstanceMgr::notify(std::string_view msg, const json::value& details) notifier.notify(msg, details); } -MAA_VISION_NS::CustomRecognizerPtr InstanceMgr::custom_recognizer(const std::string& name) +CustomRecognizerSession* InstanceMgr::custom_recognizer_session(const std::string& name) { - auto it = custom_recognizers_.find(name); - if (it == custom_recognizers_.end()) { + auto it = custom_recognizer_sessions_.find(name); + if (it == custom_recognizer_sessions_.end()) { LogError << "Custom recognizer not found:" << name; return nullptr; } - return it->second; + return &it->second; } -MAA_TASK_NS::CustomActionPtr InstanceMgr::custom_action(const std::string& name) +CustomActionSession* InstanceMgr::custom_action_session(const std::string& name) { - auto it = custom_actions_.find(name); - if (it == custom_actions_.end()) { + auto it = custom_action_sessions_.find(name); + if (it == custom_action_sessions_.end()) { LogError << "Custom action not found:" << name; return nullptr; } - return it->second; + return &it->second; } bool InstanceMgr::run_task(TaskId id, TaskPtr task_ptr) diff --git a/source/MaaFramework/Instance/InstanceMgr.h b/source/MaaFramework/Instance/InstanceMgr.h index 6a1969079..e99b393d0 100644 --- a/source/MaaFramework/Instance/InstanceMgr.h +++ b/source/MaaFramework/Instance/InstanceMgr.h @@ -57,8 +57,8 @@ class InstanceMgr virtual MAA_CTRL_NS::ControllerAgent* inter_controller() override; virtual InstanceStatus* inter_status() override; virtual void notify(std::string_view msg, const json::value& details = json::value()) override; - virtual MAA_VISION_NS::CustomRecognizerPtr custom_recognizer(const std::string& name) override; - virtual MAA_TASK_NS::CustomActionPtr custom_action(const std::string& name) override; + virtual CustomRecognizerSession* custom_recognizer_session(const std::string& name) override; + virtual CustomActionSession* custom_action_session(const std::string& name) override; private: using TaskPtr = std::shared_ptr; @@ -73,8 +73,8 @@ class InstanceMgr InstanceStatus status_; bool need_to_stop_ = false; - std::unordered_map custom_recognizers_; - std::unordered_map custom_actions_; + std::unordered_map custom_recognizer_sessions_; + std::unordered_map custom_action_sessions_; std::unique_ptr> task_runner_ = nullptr; MessageNotifier notifier; diff --git a/source/MaaFramework/Task/Actuator.cpp b/source/MaaFramework/Task/Actuator.cpp index e4f505738..198396b40 100644 --- a/source/MaaFramework/Task/Actuator.cpp +++ b/source/MaaFramework/Task/Actuator.cpp @@ -128,15 +128,13 @@ void Actuator::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv: LogFunc << "Wait freezes:" << VAR(param.time) << VAR(param.threshold) << VAR(param.method); cv::Rect target = get_target_rect(param.target, cur_box); + cv::Mat pre_image = controller()->screencap(); - TemplateComparator comp; - comp.set_param({ + TemplateComparatorParam comp_param { .roi = { target }, .threshold = param.threshold, .method = param.method, - }); - - cv::Mat pre_image = controller()->screencap(); + }; auto pre_time = std::chrono::steady_clock::now(); @@ -148,7 +146,9 @@ void Actuator::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv: break; } - auto ret = comp.analyze(pre_image, cur_image); + TemplateComparator comparator(pre_image, cur_image, comp_param); + + auto ret = comparator.filtered_results(); if (ret.empty()) { pre_image = cur_image; pre_time = std::chrono::steady_clock::now(); @@ -199,13 +199,13 @@ bool Actuator::custom_action( LogError << "Inst is null"; return false; } - auto action = inst_->custom_action(param.name); - if (!action) { + auto* session = inst_->custom_action_session(param.name); + if (!session) { LogError << "Custom task not found" << VAR(param.name); return false; } - return action->run(task_name, param, cur_box, cur_rec_detail); + return CustomAction(*session, inst_).run(task_name, param, cur_box, cur_rec_detail); } cv::Rect Actuator::get_target_rect(const MAA_RES_NS::Action::Target target, const cv::Rect& cur_box) @@ -264,4 +264,4 @@ void Actuator::sleep(std::chrono::milliseconds ms) const LogTrace << "end of sleep" << ms << VAR(interval); } -MAA_TASK_NS_END \ No newline at end of file +MAA_TASK_NS_END diff --git a/source/MaaFramework/Task/CustomAction.cpp b/source/MaaFramework/Task/CustomAction.cpp index d69c2de44..3ac8b7777 100644 --- a/source/MaaFramework/Task/CustomAction.cpp +++ b/source/MaaFramework/Task/CustomAction.cpp @@ -8,13 +8,8 @@ MAA_TASK_NS_BEGIN -CustomAction::CustomAction( - MaaCustomActionHandle handle, - MaaTransparentArg handle_arg_, - InstanceInternalAPI* inst) - : action_(handle) - , action_arg_(handle_arg_) - , inst_(inst) +CustomAction::CustomAction(CustomActionSession session, InstanceInternalAPI* inst) + : session_(std::move(session)), inst_(inst) { } @@ -24,10 +19,11 @@ bool CustomAction::run( const cv::Rect& cur_box, const json::value& cur_rec_detail) { - LogFunc << VAR(task_name) << VAR_VOIDP(action_) << VAR(param.custom_param) << VAR(cur_box); + LogFunc << VAR(task_name) << VAR_VOIDP(session_.action) << VAR(param.custom_param) + << VAR(cur_box); - if (!action_ || !action_->run) { - LogError << "Action is null" << VAR_VOIDP(action_) << VAR_VOIDP(action_->run); + if (!session_.action || !session_.action->run) { + LogError << "Action is null" << VAR(task_name); return false; } @@ -39,16 +35,16 @@ bool CustomAction::run( .height = cur_box.height }; std::string cur_rec_detail_string = cur_rec_detail.to_string(); - bool ret = action_->run( + bool ret = session_.action->run( &sync_ctx, task_name.c_str(), custom_param_string.c_str(), &box, cur_rec_detail_string.c_str(), - action_arg_); - LogTrace << VAR_VOIDP(action_) << VAR_VOIDP(action_->run) << VAR(ret); + session_.action_arg); + LogTrace << VAR_VOIDP(session_.action) << VAR_VOIDP(session_.action->run) << VAR(ret); return ret; } -MAA_TASK_NS_END \ No newline at end of file +MAA_TASK_NS_END diff --git a/source/MaaFramework/Task/CustomAction.h b/source/MaaFramework/Task/CustomAction.h index be0a26554..ca9820e9a 100644 --- a/source/MaaFramework/Task/CustomAction.h +++ b/source/MaaFramework/Task/CustomAction.h @@ -11,10 +11,7 @@ MAA_TASK_NS_BEGIN class CustomAction { public: - CustomAction( - MaaCustomActionHandle handle, - MaaTransparentArg handle_arg, - InstanceInternalAPI* inst); + CustomAction(CustomActionSession session, InstanceInternalAPI* inst); bool run(const std::string& task_name, @@ -23,8 +20,7 @@ class CustomAction const json::value& cur_rec_detail); private: - MaaCustomActionHandle action_ = nullptr; - MaaTransparentArg action_arg_ = nullptr; + CustomActionSession session_; InstanceInternalAPI* inst_ = nullptr; }; diff --git a/source/MaaFramework/Task/Recognizer.cpp b/source/MaaFramework/Task/Recognizer.cpp index f3c1cce27..48956c91d 100644 --- a/source/MaaFramework/Task/Recognizer.cpp +++ b/source/MaaFramework/Task/Recognizer.cpp @@ -61,14 +61,14 @@ std::optional break; case Type::NeuralNetworkClassify: - result = classify( + result = nn_classify( image, std::get(task_data.rec_param), task_data.name); break; case Type::NeuralNetworkDetect: - result = detect( + result = nn_detect( image, std::get(task_data.rec_param), task_data.name); @@ -120,11 +120,6 @@ std::optional Recognizer::template_match( return std::nullopt; } - TemplateMatcher matcher; - matcher.set_image(image); - matcher.set_name(name); - matcher.set_param(param); - std::vector> templates; for (const auto& path : param.template_paths) { auto templ = resource()->template_res().image(path); @@ -134,15 +129,19 @@ std::optional Recognizer::template_match( } templates.emplace_back(std::move(templ)); } - matcher.set_templates(std::move(templates)); - auto [results, index] = matcher.analyze(); + TemplateMatcher matcher(image, param, templates, name); + + auto results = std::move(matcher).filtered_results(); + size_t index = matcher.preferred_index(); + auto draws = std::move(matcher).draws(); + if (index >= results.size()) { return std::nullopt; } - const cv::Rect& box = results[index].box; - return Result { .box = box, .detail = std::move(results) }; + + return Result { .box = box, .detail = std::move(results), .draws = std::move(draws) }; } std::optional Recognizer::feature_match( @@ -157,11 +156,6 @@ std::optional Recognizer::feature_match( return std::nullopt; } - FeatureMatcher matcher; - matcher.set_image(image); - matcher.set_name(name); - matcher.set_param(param); - std::vector> templates; for (const auto& path : param.template_paths) { auto templ = resource()->template_res().image(path); @@ -171,15 +165,19 @@ std::optional Recognizer::feature_match( } templates.emplace_back(std::move(templ)); } - matcher.set_templates(std::move(templates)); - auto [results, index] = matcher.analyze(); + FeatureMatcher matcher(image, param, templates, name); + + auto results = std::move(matcher).filtered_results(); + size_t index = matcher.preferred_index(); + auto draws = std::move(matcher).draws(); + if (index >= results.size()) { return std::nullopt; } - const cv::Rect& box = results[index].box; - return Result { .box = box, .detail = std::move(results) }; + + return Result { .box = box, .detail = std::move(results), .draws = std::move(draws) }; } std::optional Recognizer::color_match( @@ -194,18 +192,18 @@ std::optional Recognizer::color_match( return std::nullopt; } - ColorMatcher matcher; - matcher.set_image(image); - matcher.set_name(name); - matcher.set_param(param); + ColorMatcher matcher(image, param, name); + + auto results = std::move(matcher).filtered_results(); + size_t index = matcher.preferred_index(); + auto draws = std::move(matcher).draws(); - auto [results, index] = matcher.analyze(); if (index >= results.size()) { return std::nullopt; } - const cv::Rect& box = results[index].box; - return Result { .box = box, .detail = std::move(results) }; + + return Result { .box = box, .detail = std::move(results), .draws = std::move(draws) }; } std::optional Recognizer::ocr( @@ -220,28 +218,25 @@ std::optional Recognizer::ocr( return std::nullopt; } - OCRer ocrer; - ocrer.set_image(image); - ocrer.set_name(name); - ocrer.set_param(param); - auto det_session = resource()->ocr_res().deter(param.model); auto rec_session = resource()->ocr_res().recer(param.model); auto ocr_session = resource()->ocr_res().ocrer(param.model); - ocrer.set_session(std::move(det_session), std::move(rec_session), std::move(ocr_session)); - ocrer.set_status(status()); + OCRer ocrer(image, param, det_session, rec_session, ocr_session, status(), name); + + auto results = std::move(ocrer).filtered_results(); + size_t index = ocrer.preferred_index(); + auto draws = std::move(ocrer).draws(); - auto [results, index] = ocrer.analyze(); if (index >= results.size()) { return std::nullopt; } - const cv::Rect& box = results[index].box; - return Result { .box = box, .detail = std::move(results) }; + + return Result { .box = box, .detail = std::move(results), .draws = std::move(draws) }; } -std::optional Recognizer::classify( +std::optional Recognizer::nn_classify( const cv::Mat& image, const MAA_VISION_NS::NeuralNetworkClassifierParam& param, const std::string& name) @@ -253,24 +248,23 @@ std::optional Recognizer::classify( return std::nullopt; } - NeuralNetworkClassifier classifier; - classifier.set_image(image); - classifier.set_name(name); - classifier.set_param(param); - auto session = resource()->onnx_res().classifier(param.model); - classifier.set_session(std::move(session)); - auto [results, index] = classifier.analyze(); + NeuralNetworkClassifier classifier(image, param, session, name); + + auto results = std::move(classifier).filtered_results(); + size_t index = classifier.preferred_index(); + auto draws = std::move(classifier).draws(); + if (index >= results.size()) { return std::nullopt; } - const cv::Rect& box = results[index].box; - return Result { .box = box, .detail = std::move(results) }; + + return Result { .box = box, .detail = std::move(results), .draws = std::move(draws) }; } -std::optional Recognizer::detect( +std::optional Recognizer::nn_detect( const cv::Mat& image, const MAA_VISION_NS::NeuralNetworkDetectorParam& param, const std::string& name) @@ -282,21 +276,20 @@ std::optional Recognizer::detect( return std::nullopt; } - NeuralNetworkDetector detector; - detector.set_image(image); - detector.set_name(name); - detector.set_param(param); - auto session = resource()->onnx_res().detector(param.model); - detector.set_session(std::move(session)); - auto [results, index] = detector.analyze(); + NeuralNetworkDetector detector(image, param, session, name); + + auto results = std::move(detector).filtered_results(); + size_t index = detector.preferred_index(); + auto draws = std::move(detector).draws(); + if (index >= results.size()) { return std::nullopt; } - const cv::Rect& box = results[index].box; - return Result { .box = box, .detail = std::move(results) }; + + return Result { .box = box, .detail = std::move(results), .draws = std::move(draws) }; } std::optional Recognizer::custom_recognize( @@ -311,22 +304,22 @@ std::optional Recognizer::custom_recognize( return std::nullopt; } - auto recognizer = inst_->custom_recognizer(param.name); - if (!recognizer) { + auto* session = inst_->custom_recognizer_session(param.name); + if (!session) { LogError << "Custom recognizer not found:" << param.name; return std::nullopt; } - recognizer->set_image(image); - recognizer->set_param(param); - recognizer->set_name(name); - auto result_opt = recognizer->analyze(); - if (!result_opt) { + CustomRecognizer recognizer(image, param, *session, inst_, name); + auto results = std::move(recognizer).result(); + bool ret = recognizer.ret(); + + if (!ret) { return std::nullopt; } - const cv::Rect& box = result_opt->box; - return Result { .box = box, .detail = std::move(*result_opt) }; + const cv::Rect& box = results.box; + return Result { .box = box, .detail = std::move(results) }; } void Recognizer::show_hit_draw( @@ -350,4 +343,4 @@ void Recognizer::show_hit_draw( cv::destroyWindow(kWinName); } -MAA_TASK_NS_END \ No newline at end of file +MAA_TASK_NS_END diff --git a/source/MaaFramework/Task/Recognizer.h b/source/MaaFramework/Task/Recognizer.h index a21160573..e7395ec42 100644 --- a/source/MaaFramework/Task/Recognizer.h +++ b/source/MaaFramework/Task/Recognizer.h @@ -22,6 +22,7 @@ class Recognizer { cv::Rect box {}; json::value detail; + std::vector draws; }; public: @@ -46,11 +47,11 @@ class Recognizer const std::string& name); std::optional ocr(const cv::Mat& image, const MAA_VISION_NS::OCRerParam& param, const std::string& name); - std::optional classify( + std::optional nn_classify( const cv::Mat& image, const MAA_VISION_NS::NeuralNetworkClassifierParam& param, const std::string& name); - std::optional detect( + std::optional nn_detect( const cv::Mat& image, const MAA_VISION_NS::NeuralNetworkDetectorParam& param, const std::string& name); diff --git a/source/MaaFramework/Vision/ColorMatcher.cpp b/source/MaaFramework/Vision/ColorMatcher.cpp index c0f9f7c00..15a632098 100644 --- a/source/MaaFramework/Vision/ColorMatcher.cpp +++ b/source/MaaFramework/Vision/ColorMatcher.cpp @@ -124,10 +124,6 @@ cv::Mat ColorMatcher::draw_result( const cv::Mat& bin, const ResultsVec& results) const { - if (!debug_draw_) { - return; - } - cv::Mat image_draw = draw_roi(roi); const auto color_draw = cv::Scalar(0, 0, 255); @@ -205,4 +201,4 @@ void ColorMatcher::sort_(ResultsVec& results) const } } -MAA_VISION_NS_END +MAA_VISION_NS_END \ No newline at end of file diff --git a/source/MaaFramework/Vision/ColorMatcher.h b/source/MaaFramework/Vision/ColorMatcher.h index 727a9c379..670a8027f 100644 --- a/source/MaaFramework/Vision/ColorMatcher.h +++ b/source/MaaFramework/Vision/ColorMatcher.h @@ -22,9 +22,13 @@ class ColorMatcher : public VisionBase public: ColorMatcher(cv::Mat image, ColorMatcherParam param, std::string name = ""); - const ResultsVec& all_results() const { return all_results_; } + const ResultsVec& all_results() const& { return all_results_; } - const ResultsVec& filtered_results() const { return filtered_results_; } + ResultsVec&& all_results() && { return std::move(all_results_); } + + const ResultsVec& filtered_results() const& { return filtered_results_; } + + ResultsVec filtered_results() && { return std::move(filtered_results_); } private: void analyze(); diff --git a/source/MaaFramework/Vision/CustomRecognizer.cpp b/source/MaaFramework/Vision/CustomRecognizer.cpp index c7979a952..3ff20759a 100644 --- a/source/MaaFramework/Vision/CustomRecognizer.cpp +++ b/source/MaaFramework/Vision/CustomRecognizer.cpp @@ -14,10 +14,12 @@ CustomRecognizer::CustomRecognizer( cv::Mat image, CustomRecognizerParam param, CustomRecognizerSession session, + InstanceInternalAPI* inst, std::string name) : VisionBase(std::move(image), std::move(name)) , param_(std::move(param)) , session_(std::move(session)) + , inst_(inst) { analyze(); } @@ -34,7 +36,7 @@ void CustomRecognizer::analyze() auto start_time = std::chrono::steady_clock::now(); /*in*/ - MAA_TASK_NS::SyncContext sync_ctx(session_.inst); + MAA_TASK_NS::SyncContext sync_ctx(inst_); ImageBuffer image_buffer; image_buffer.set(image_); std::string custom_param_str = param_.custom_param.to_string(); diff --git a/source/MaaFramework/Vision/CustomRecognizer.h b/source/MaaFramework/Vision/CustomRecognizer.h index 423db92f6..e8b50a8a7 100644 --- a/source/MaaFramework/Vision/CustomRecognizer.h +++ b/source/MaaFramework/Vision/CustomRecognizer.h @@ -25,11 +25,14 @@ class CustomRecognizer : public VisionBase cv::Mat image, CustomRecognizerParam param, CustomRecognizerSession session, + InstanceInternalAPI* inst, std::string name = ""); bool ret() const { return ret_; } - const Result& result() const { return result_; } + const Result& result() const& { return result_; } + + Result result() && { return std::move(result_); } private: void analyze(); @@ -37,6 +40,7 @@ class CustomRecognizer : public VisionBase private: const CustomRecognizerParam param_; CustomRecognizerSession session_; + InstanceInternalAPI* inst_ = nullptr; private: bool ret_ = false; diff --git a/source/MaaFramework/Vision/FeatureMatcher.cpp b/source/MaaFramework/Vision/FeatureMatcher.cpp index 32723191f..f14dc7f2d 100644 --- a/source/MaaFramework/Vision/FeatureMatcher.cpp +++ b/source/MaaFramework/Vision/FeatureMatcher.cpp @@ -84,7 +84,7 @@ FeatureMatcher::ResultsVec FeatureMatcher::feature_match( { if (roi_2.empty()) { LogError << name_ << "roi_2 is empty"; - return; + return {}; } auto [keypoints_2, descriptors_2] = detect(image_, create_mask(image_, roi_2)); @@ -333,4 +333,4 @@ void FeatureMatcher::sort_(ResultsVec& results) const } } -MAA_VISION_NS_END +MAA_VISION_NS_END \ No newline at end of file diff --git a/source/MaaFramework/Vision/FeatureMatcher.h b/source/MaaFramework/Vision/FeatureMatcher.h index f47d150e8..18792e638 100644 --- a/source/MaaFramework/Vision/FeatureMatcher.h +++ b/source/MaaFramework/Vision/FeatureMatcher.h @@ -35,9 +35,13 @@ class FeatureMatcher : public VisionBase std::vector> templates, std::string name = ""); - const ResultsVec& all_results() const { return all_results_; } + const ResultsVec& all_results() const& { return all_results_; } - const ResultsVec& filtered_results() const { return filtered_results_; } + ResultsVec&& all_results() && { return std::move(all_results_); } + + const ResultsVec& filtered_results() const& { return filtered_results_; } + + ResultsVec filtered_results() && { return std::move(filtered_results_); } private: void analyze(); diff --git a/source/MaaFramework/Vision/NeuralNetworkClassifier.cpp b/source/MaaFramework/Vision/NeuralNetworkClassifier.cpp index 4a0f0bc9f..05928253b 100644 --- a/source/MaaFramework/Vision/NeuralNetworkClassifier.cpp +++ b/source/MaaFramework/Vision/NeuralNetworkClassifier.cpp @@ -190,4 +190,4 @@ void NeuralNetworkClassifier::sort_(ResultsVec& results) const } } -MAA_VISION_NS_END +MAA_VISION_NS_END \ No newline at end of file diff --git a/source/MaaFramework/Vision/NeuralNetworkClassifier.h b/source/MaaFramework/Vision/NeuralNetworkClassifier.h index 000383a86..63f97dc26 100644 --- a/source/MaaFramework/Vision/NeuralNetworkClassifier.h +++ b/source/MaaFramework/Vision/NeuralNetworkClassifier.h @@ -36,9 +36,13 @@ class NeuralNetworkClassifier : public VisionBase std::shared_ptr session, std::string name = ""); - const ResultsVec& all_results() const { return all_results_; } + const ResultsVec& all_results() const& { return all_results_; } - const ResultsVec& filtered_results() const { return filtered_results_; } + ResultsVec&& all_results() && { return std::move(all_results_); } + + const ResultsVec& filtered_results() const& { return filtered_results_; } + + ResultsVec filtered_results() && { return std::move(filtered_results_); } private: void analyze(); diff --git a/source/MaaFramework/Vision/NeuralNetworkDetector.cpp b/source/MaaFramework/Vision/NeuralNetworkDetector.cpp index 37c5bebb8..e97428a90 100644 --- a/source/MaaFramework/Vision/NeuralNetworkDetector.cpp +++ b/source/MaaFramework/Vision/NeuralNetworkDetector.cpp @@ -241,5 +241,4 @@ void NeuralNetworkDetector::sort_(ResultsVec& results) const } } -MAA_VISION_NS_END - +MAA_VISION_NS_END \ No newline at end of file diff --git a/source/MaaFramework/Vision/NeuralNetworkDetector.h b/source/MaaFramework/Vision/NeuralNetworkDetector.h index fccbfb852..37d030bae 100644 --- a/source/MaaFramework/Vision/NeuralNetworkDetector.h +++ b/source/MaaFramework/Vision/NeuralNetworkDetector.h @@ -33,9 +33,13 @@ class NeuralNetworkDetector : public VisionBase std::shared_ptr session, std::string name = ""); - const ResultsVec& all_results() const { return all_results_; } + const ResultsVec& all_results() const& { return all_results_; } - const ResultsVec& filtered_results() const { return filtered_results_; } + ResultsVec&& all_results() && { return std::move(all_results_); } + + const ResultsVec& filtered_results() const& { return filtered_results_; } + + ResultsVec filtered_results() && { return std::move(filtered_results_); } private: void analyze(); diff --git a/source/MaaFramework/Vision/OCRer.cpp b/source/MaaFramework/Vision/OCRer.cpp index 1d7d29621..944281d00 100644 --- a/source/MaaFramework/Vision/OCRer.cpp +++ b/source/MaaFramework/Vision/OCRer.cpp @@ -278,4 +278,4 @@ void OCRer::sort_(ResultsVec& results) const } } -MAA_VISION_NS_END +MAA_VISION_NS_END \ No newline at end of file diff --git a/source/MaaFramework/Vision/OCRer.h b/source/MaaFramework/Vision/OCRer.h index df62d8d57..955dd1ed0 100644 --- a/source/MaaFramework/Vision/OCRer.h +++ b/source/MaaFramework/Vision/OCRer.h @@ -43,9 +43,13 @@ class OCRer : public VisionBase InstanceStatus* status = nullptr, std::string name = ""); - const ResultsVec& all_results() const { return all_results_; } + const ResultsVec& all_results() const& { return all_results_; } - const ResultsVec& filtered_results() const { return filtered_results_; } + ResultsVec&& all_results() && { return std::move(all_results_); } + + const ResultsVec& filtered_results() const& { return filtered_results_; } + + ResultsVec filtered_results() && { return std::move(filtered_results_); } private: void analyze(); diff --git a/source/MaaFramework/Vision/TemplateComparator.cpp b/source/MaaFramework/Vision/TemplateComparator.cpp index 5232ced5e..f50bec164 100644 --- a/source/MaaFramework/Vision/TemplateComparator.cpp +++ b/source/MaaFramework/Vision/TemplateComparator.cpp @@ -80,4 +80,4 @@ double TemplateComparator::comp(const cv::Mat& lhs, const cv::Mat& rhs, int meth return max_val; } -MAA_VISION_NS_END +MAA_VISION_NS_END \ No newline at end of file diff --git a/source/MaaFramework/Vision/TemplateComparator.h b/source/MaaFramework/Vision/TemplateComparator.h index aef1f18da..1366dd2c9 100644 --- a/source/MaaFramework/Vision/TemplateComparator.h +++ b/source/MaaFramework/Vision/TemplateComparator.h @@ -26,6 +26,14 @@ class TemplateComparator : public VisionBase TemplateComparatorParam param, std::string name = ""); + const ResultsVec& all_results() const& { return all_results_; } + + ResultsVec&& all_results() && { return std::move(all_results_); } + + const ResultsVec& filtered_results() const& { return filtered_results_; } + + ResultsVec filtered_results() && { return std::move(filtered_results_); } + private: void analyze(); ResultsVec compare_all_rois(); diff --git a/source/MaaFramework/Vision/TemplateMatcher.cpp b/source/MaaFramework/Vision/TemplateMatcher.cpp index 06ef2422c..b8310bda8 100644 --- a/source/MaaFramework/Vision/TemplateMatcher.cpp +++ b/source/MaaFramework/Vision/TemplateMatcher.cpp @@ -213,4 +213,4 @@ void TemplateMatcher::sort_(ResultsVec& results) const } } -MAA_VISION_NS_END +MAA_VISION_NS_END \ No newline at end of file diff --git a/source/MaaFramework/Vision/TemplateMatcher.h b/source/MaaFramework/Vision/TemplateMatcher.h index f25713f09..9742d7060 100644 --- a/source/MaaFramework/Vision/TemplateMatcher.h +++ b/source/MaaFramework/Vision/TemplateMatcher.h @@ -29,9 +29,13 @@ class TemplateMatcher : public VisionBase std::vector> templates, std::string name = ""); - const ResultsVec& all_results() const { return all_results_; } + const ResultsVec& all_results() const& { return all_results_; } - const ResultsVec& filtered_results() const { return filtered_results_; } + ResultsVec&& all_results() && { return std::move(all_results_); } + + const ResultsVec& filtered_results() const& { return filtered_results_; } + + ResultsVec filtered_results() && { return std::move(filtered_results_); } private: void analyze(); diff --git a/source/MaaFramework/Vision/VisionBase.h b/source/MaaFramework/Vision/VisionBase.h index 55ebe4cfc..c3819b188 100644 --- a/source/MaaFramework/Vision/VisionBase.h +++ b/source/MaaFramework/Vision/VisionBase.h @@ -13,9 +13,13 @@ class VisionBase public: VisionBase(cv::Mat image, std::string name); - const std::vector& draws() const { return draws_; } + const std::vector& draws() const& { return draws_; } - const std::vector& draw_paths() const { return draw_paths_; } + std::vector draws() && { return std::move(draws_); } + + const std::vector& draw_paths() const& { return draw_paths_; } + + std::vector draw_paths() && { return std::move(draw_paths_); } size_t preferred_index() const { return preferred_index_; } diff --git a/source/MaaFramework/Vision/VisionUtils.hpp b/source/MaaFramework/Vision/VisionUtils.hpp index 0937112ab..3b4893e63 100644 --- a/source/MaaFramework/Vision/VisionUtils.hpp +++ b/source/MaaFramework/Vision/VisionUtils.hpp @@ -267,4 +267,4 @@ inline std::ostream& operator<<(std::ostream& os, const cv::Rect& rect) return os; } -MAA_VISION_NS_END +MAA_VISION_NS_END \ No newline at end of file