Skip to content

Commit 716ebcf

Browse files
MistEOCopilot
andauthored
feat: wait_freezes draw (#1177)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 8e103e8 commit 716ebcf

9 files changed

Lines changed: 77 additions & 27 deletions

File tree

source/MaaFramework/Task/Component/ActionHelper.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ActionHelper::ActionHelper(Tasker* tasker)
1313
{
1414
}
1515

16-
bool ActionHelper::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box)
16+
bool ActionHelper::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name)
1717
{
1818
if (param.time <= std::chrono::milliseconds(0)) {
1919
return true;
@@ -65,7 +65,10 @@ bool ActionHelper::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const
6565
return false;
6666
}
6767

68-
TemplateComparator comparator(pre_image, cur_image, { roi }, comp_param);
68+
std::string draw_name = name.empty() ? "wait_freezes" : std::format("{}_wait_freezes", name);
69+
TemplateComparator comparator(pre_image, cur_image, { roi }, comp_param, draw_name);
70+
71+
VisionBase::save_draws(draw_name, comparator.draws());
6972

7073
if (!comparator.best_result()) {
7174
pre_image = cur_image;

source/MaaFramework/Task/Component/ActionHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ActionHelper : public NonCopyable
1313
explicit ActionHelper(Tasker* tasker);
1414

1515
// 等待画面静止
16-
bool wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box);
16+
bool wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name = "");
1717

1818
// 根据 target 计算 ROI 区域
1919
cv::Rect get_target_rect(const MAA_RES_NS::Action::Target& target, const cv::Rect& box = {});

source/MaaFramework/Task/Component/Actuator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ ActionResult Actuator::run(const cv::Rect& reco_hit, MaaRecoId reco_id, const Pi
3636
return {};
3737
}
3838

39-
wait_freezes(pipeline_data.pre_wait_freezes, reco_hit);
39+
wait_freezes(pipeline_data.pre_wait_freezes, reco_hit, pipeline_data.name);
4040
sleep(pipeline_data.pre_delay);
4141

4242
auto& rt_cache = tasker_->runtime_cache();
4343
ActionResult result;
4444

4545
for (uint i = 0; i < pipeline_data.repeat; ++i) {
4646
if (i > 0) {
47-
wait_freezes(pipeline_data.repeat_wait_freezes, reco_hit);
47+
wait_freezes(pipeline_data.repeat_wait_freezes, reco_hit, pipeline_data.name);
4848
sleep(pipeline_data.repeat_delay);
4949
}
5050

@@ -61,7 +61,7 @@ ActionResult Actuator::run(const cv::Rect& reco_hit, MaaRecoId reco_id, const Pi
6161
}
6262
}
6363

64-
wait_freezes(pipeline_data.post_wait_freezes, reco_hit);
64+
wait_freezes(pipeline_data.post_wait_freezes, reco_hit, pipeline_data.name);
6565
sleep(pipeline_data.post_delay);
6666

6767
return result;
@@ -559,14 +559,14 @@ ActionResult Actuator::screencap(const MAA_RES_NS::Action::ScreencapParam& param
559559
};
560560
}
561561

562-
void Actuator::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box)
562+
void Actuator::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name)
563563
{
564564
if (param.time <= std::chrono::milliseconds(0)) {
565565
return;
566566
}
567567

568568
cv::Rect roi = helper_.get_target_rect(param.target, box);
569-
helper_.wait_freezes(param, roi);
569+
helper_.wait_freezes(param, roi, name);
570570
}
571571

572572
ActionResult Actuator::start_app(const MAA_RES_NS::Action::AppParam& param, const std::string& name)

source/MaaFramework/Task/Component/Actuator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Actuator : public NonCopyable
6262
ActionResult do_nothing(const std::string& name);
6363
ActionResult stop_task(const std::string& name);
6464

65-
void wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box);
65+
void wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name);
6666

6767
private:
6868
MAA_CTRL_NS::ControllerAgent* controller();

source/MaaFramework/Task/Component/Recognizer.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -560,24 +560,8 @@ std::vector<cv::Rect> Recognizer::get_rois_from_pretask(const std::string& name,
560560

561561
void Recognizer::save_draws(const std::string& node_name, const RecoResult& result) const
562562
{
563-
const auto& option = MAA_GLOBAL_NS::OptionMgr::get_instance();
564-
565-
if (!option.save_draw()) {
566-
return;
567-
}
568-
569-
auto dir = option.log_dir() / "vision";
570-
571-
std::filesystem::create_directories(dir);
572-
573-
for (const auto& draw : result.draws) {
574-
std::string filename = std::format("{}_{}_{}.jpg", format_now_for_filename(), node_name, result.reco_id);
575-
auto filepath = dir / path(filename);
576-
577-
std::ofstream of(filepath, std::ios::out | std::ios::binary);
578-
of.write(reinterpret_cast<const char*>(draw.data()), draw.size());
579-
LogDebug << "save draw to" << filepath;
580-
}
563+
std::string name = std::format("{}_{}", node_name, result.reco_id);
564+
MAA_VISION_NS::VisionBase::save_draws(name, result.draws);
581565
}
582566

583567
bool Recognizer::debug_mode() const

source/MaaFramework/Vision/TemplateComparator.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void TemplateComparator::analyze()
4242
double score = comp(lhs_roi, rhs_roi, param_.method);
4343
Result res = Result { .box = roi_, .score = score };
4444
add_results({ std::move(res) }, param_.threshold);
45+
46+
if (debug_draw_) {
47+
auto draw = draw_result(roi_, score);
48+
handle_draw(draw);
49+
}
4550
}
4651

4752
cherry_pick();
@@ -101,4 +106,34 @@ bool TemplateComparator::comp_score(double s1, double s2) const
101106
return low_score_better_ ? s1 > s2 : s1 < s2;
102107
}
103108

109+
cv::Mat TemplateComparator::draw_result(const cv::Rect& roi, double score) const
110+
{
111+
int width = image_.cols + rhs_image_.cols;
112+
int height = std::max(image_.rows, rhs_image_.rows);
113+
cv::Mat draw = cv::Mat::zeros(height, width, image_.type());
114+
115+
image_.copyTo(draw(cv::Rect(0, 0, image_.cols, image_.rows)));
116+
rhs_image_.copyTo(draw(cv::Rect(image_.cols, 0, rhs_image_.cols, rhs_image_.rows)));
117+
118+
const cv::Scalar roi_color(0, 255, 0);
119+
const cv::Scalar score_color(0, 0, 255);
120+
121+
cv::putText(draw, name_, cv::Point(5, image_.rows - 5), cv::FONT_HERSHEY_SIMPLEX, 1, roi_color, 2);
122+
123+
cv::rectangle(draw, roi, roi_color, 1);
124+
std::string roi_flag = std::format("ROI: [{}, {}, {}, {}]", roi.x, roi.y, roi.width, roi.height);
125+
cv::putText(draw, roi_flag, cv::Point(roi.x, roi.y - 5), cv::FONT_HERSHEY_PLAIN, 1.2, roi_color, 1);
126+
127+
cv::Rect rhs_roi(roi.x + image_.cols, roi.y, roi.width, roi.height);
128+
cv::rectangle(draw, rhs_roi, roi_color, 1);
129+
cv::putText(draw, roi_flag, cv::Point(rhs_roi.x, rhs_roi.y - 5), cv::FONT_HERSHEY_PLAIN, 1.2, roi_color, 1);
130+
131+
cv::line(draw, roi.tl(), rhs_roi.tl(), roi_color, 1);
132+
133+
std::string score_flag = std::format("Score: {:.3f}", score);
134+
cv::putText(draw, score_flag, cv::Point(roi.x, roi.y + roi.height + 30), cv::FONT_HERSHEY_SIMPLEX, 1, score_color, 2);
135+
136+
return draw;
137+
}
138+
104139
MAA_VISION_NS_END

source/MaaFramework/Vision/TemplateComparator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class TemplateComparator
2626
private:
2727
void analyze();
2828

29+
cv::Mat draw_result(const cv::Rect& roi, double score) const;
30+
2931
void add_results(ResultsVec results, double threshold);
3032
void cherry_pick();
3133

source/MaaFramework/Vision/VisionBase.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include "VisionBase.h"
22

3+
#include <fstream>
4+
35
#include "MaaUtils/NoWarningCV.hpp"
46

57
#include "Global/OptionMgr.h"
68
#include "MaaUtils/Logger.h"
9+
#include "MaaUtils/Time.hpp"
710
#include "VisionUtils.hpp"
811

912
MAA_VISION_NS_BEGIN
@@ -73,4 +76,25 @@ void VisionBase::init_draw()
7376
#endif
7477
}
7578

79+
void VisionBase::save_draws(const std::string& name, const std::vector<ImageEncodedBuffer>& draws)
80+
{
81+
const auto& option = MAA_GLOBAL_NS::OptionMgr::get_instance();
82+
83+
if (!option.save_draw()) {
84+
return;
85+
}
86+
87+
auto dir = option.log_dir() / "vision";
88+
std::filesystem::create_directories(dir);
89+
90+
for (const auto& draw : draws) {
91+
std::string filename = std::format("{}_{}.jpg", format_now_for_filename(), name);
92+
auto filepath = dir / path(filename);
93+
94+
std::ofstream of(filepath, std::ios::out | std::ios::binary);
95+
of.write(reinterpret_cast<const char*>(draw.data()), draw.size());
96+
LogDebug << "save draw to" << filepath;
97+
}
98+
}
99+
76100
MAA_VISION_NS_END

source/MaaFramework/Vision/VisionBase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class VisionBase
4848

4949
std::vector<ImageEncodedBuffer> draws() && { return std::move(draws_); }
5050

51+
static void save_draws(const std::string& name, const std::vector<ImageEncodedBuffer>& draws);
52+
5153
protected:
5254
cv::Mat image_with_roi() const;
5355

0 commit comments

Comments
 (0)