Skip to content

Commit 5c5b753

Browse files
committed
Add proper white dialog detector.
1 parent ef1adba commit 5c5b753

8 files changed

Lines changed: 126 additions & 39 deletions

File tree

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
316316
using namespace OCR;
317317
using namespace NintendoSwitch;
318318
using namespace Pokemon;
319-
// using namespace PokemonSwSh;
319+
using namespace PokemonSwSh;
320320
// using namespace PokemonBDSP;
321321
// using namespace PokemonLA;
322322
// using namespace PokemonSV;
323323
// using namespace PokemonLZA;
324-
using namespace PokemonFRLG;
324+
// using namespace PokemonFRLG;
325325

326326
[[maybe_unused]] Logger& logger = env.logger();
327327
[[maybe_unused]] ConsoleHandle& console = env.consoles[0];
@@ -333,6 +333,14 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
333333
VideoOverlaySet overlays(overlay);
334334

335335

336+
auto snapshot = feed.snapshot();
337+
338+
WhiteDialogBoxDetector detector;
339+
detector.make_overlays(overlays);
340+
cout << detector.detect(snapshot) << endl;
341+
342+
343+
#if 0
336344
RumbleWatcher<ProController> rumble(context, 200);
337345
int ret = wait_until(
338346
console, context,
@@ -344,6 +352,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
344352
}else{
345353
cout << "Did not detect anything." << endl;
346354
}
355+
#endif
347356

348357

349358
#if 0

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_DialogBoxDetector.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ bool BlackDialogBoxDetector::process_frame(const ImageViewRGB32& frame, WallCloc
6565

6666

6767

68+
WhiteDialogBoxDetector::WhiteDialogBoxDetector(Color color)
69+
: m_color(color)
70+
, m_right(0.782, 0.850, 0.030, 0.050)
71+
, m_triangle(color)
72+
{}
73+
74+
void WhiteDialogBoxDetector::make_overlays(VideoOverlaySet& items) const{
75+
items.add(m_color, m_right);
76+
m_triangle.make_overlays(items);
77+
}
78+
bool WhiteDialogBoxDetector::detect(const ImageViewRGB32& screen){
79+
ImageStats right = image_stats(extract_box_reference(screen, m_right));
80+
if (!is_grey(right, 400, 1000)){
81+
return false;
82+
}
83+
84+
if (!m_triangle.detect(screen)){
85+
return false;
86+
}
87+
88+
return true;
89+
}
90+
91+
92+
93+
94+
6895

6996
}
7097
}

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_DialogBoxDetector.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
#define PokemonAutomation_PokemonSwSh_DialogBoxDetector_H
99

1010
#include <atomic>
11+
#include "Common/Cpp/Color.h"
12+
#include "CommonFramework/ImageTools/ImageBoxes.h"
13+
#include "CommonTools/VisualDetector.h"
1114
#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h"
15+
#include "PokemonSwSh_DialogTriangleDetector.h"
1216

1317
namespace PokemonAutomation{
1418
namespace NintendoSwitch{
@@ -25,7 +29,6 @@ class BlackDialogBoxDetector : public VisualInferenceCallback{
2529
}
2630

2731
virtual void make_overlays(VideoOverlaySet& items) const override;
28-
2932
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override;
3033

3134
private:
@@ -35,6 +38,26 @@ class BlackDialogBoxDetector : public VisualInferenceCallback{
3538
};
3639

3740

41+
class WhiteDialogBoxDetector : public StaticScreenDetector{
42+
public:
43+
WhiteDialogBoxDetector(Color color = COLOR_RED);
44+
45+
virtual void make_overlays(VideoOverlaySet& items) const override;
46+
virtual bool detect(const ImageViewRGB32& screen) override;
47+
48+
private:
49+
Color m_color;
50+
ImageFloatBox m_right;
51+
DialogTriangleDetector m_triangle;
52+
};
53+
class WhiteDialogBoxWatcher : public DetectorToFinder<WhiteDialogBoxDetector>{
54+
public:
55+
WhiteDialogBoxWatcher(Color color = COLOR_RED)
56+
: DetectorToFinder("WhiteDialogBoxWatcher", std::chrono::milliseconds(250), color)
57+
{}
58+
};
59+
60+
3861

3962

4063

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_DialogTriangleDetector.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ namespace NintendoSwitch{
2222
namespace PokemonSwSh{
2323

2424

25-
namespace{
26-
// This box covers all possible locations of the black triangle arrow
27-
ImageFloatBox BLACK_TRIANGLE_BOX{0.771, 0.901, 0.031, 0.069};
28-
}
25+
2926

3027
class DialogTriangleMatcher : public ImageMatch::WaterfillTemplateMatcher{
3128
public:
@@ -53,20 +50,58 @@ const DialogTriangleMatcher& DialogTriangleMatcher::instance(){
5350

5451

5552

53+
5654
DialogTriangleDetector::DialogTriangleDetector(
55+
Color color,
56+
ImageFloatBox box
57+
)
58+
: m_color(color)
59+
, m_box(box)
60+
{}
61+
62+
void DialogTriangleDetector::make_overlays(VideoOverlaySet& items) const{
63+
items.add(m_color, m_box);
64+
}
65+
bool DialogTriangleDetector::detect(const ImageViewRGB32& screen){
66+
const std::vector<std::pair<uint32_t, uint32_t>> filters = {
67+
{combine_rgb(0, 0, 0), combine_rgb(50, 50, 50)}
68+
};
69+
70+
const double screen_rel_size = (screen.height() / 1080.0);
71+
const size_t min_size = size_t(screen_rel_size * screen_rel_size * 500.0);
72+
73+
return match_template_by_waterfill(
74+
screen.size(),
75+
extract_box_reference(screen, m_box),
76+
DialogTriangleMatcher::instance(),
77+
filters,
78+
{min_size, SIZE_MAX},
79+
80,
80+
[](Kernels::Waterfill::WaterfillObject& object) -> bool { return true; }
81+
);
82+
}
83+
84+
85+
86+
87+
88+
#if 0
89+
90+
91+
DialogTriangleWatcher::DialogTriangleWatcher(
5792
Logger& logger, VideoOverlay& overlay,
5893
bool stop_on_detected
5994
)
60-
: VisualInferenceCallback("DialogTriangleDetector")
95+
: VisualInferenceCallback("DialogTriangleWatcher")
6196
, m_logger(logger)
6297
, m_stop_on_detected(stop_on_detected)
6398
{}
6499

65100

66-
void DialogTriangleDetector::make_overlays(VideoOverlaySet& items) const{
101+
void DialogTriangleWatcher::make_overlays(VideoOverlaySet& items) const{
67102
items.add(COLOR_RED, BLACK_TRIANGLE_BOX);
68103
}
69-
bool DialogTriangleDetector::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
104+
bool DialogTriangleWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
70105
const std::vector<std::pair<uint32_t, uint32_t>> filters = {
71106
{combine_rgb(0, 0, 0), combine_rgb(50, 50, 50)}
72107
};
@@ -100,7 +135,7 @@ bool DialogTriangleDetector::process_frame(const ImageViewRGB32& frame, WallCloc
100135
return detected && m_stop_on_detected;
101136
}
102137

103-
138+
#endif
104139

105140

106141

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_DialogTriangleDetector.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#ifndef PokemonAutomation_PokemonSwSh_DialogTriangleDetector_H
88
#define PokemonAutomation_PokemonSwSh_DialogTriangleDetector_H
99

10-
#include <atomic>
11-
#include "Common/Cpp/Logging/AbstractLogger.h"
12-
#include "CommonFramework/VideoPipeline/VideoOverlay.h"
10+
#include "Common/Cpp/Color.h"
11+
#include "CommonFramework/ImageTools/ImageBoxes.h"
12+
#include "CommonTools/VisualDetector.h"
1313
#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h"
1414

1515
namespace PokemonAutomation{
@@ -19,28 +19,26 @@ namespace PokemonSwSh{
1919

2020
// Detect the black triangle arrow in the lower right portion of the white dialog box.
2121
// The dialog box shows up when talking to npcs.
22-
class DialogTriangleDetector : public VisualInferenceCallback{
22+
class DialogTriangleDetector : public StaticScreenDetector{
2323
public:
2424
DialogTriangleDetector(
25-
Logger& logger, VideoOverlay& overlay,
26-
bool stop_on_detected
25+
Color color,
26+
ImageFloatBox box = {0.771, 0.901, 0.031, 0.069}
2727
);
2828

29-
bool detected() const{
30-
return m_detected.load(std::memory_order_acquire);
31-
}
32-
3329
virtual void make_overlays(VideoOverlaySet& items) const override;
34-
35-
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override;
30+
virtual bool detect(const ImageViewRGB32& screen) override;
3631

3732
private:
38-
Logger& m_logger;
39-
bool m_stop_on_detected;
40-
41-
std::atomic<bool> m_detected;
33+
Color m_color;
34+
ImageFloatBox m_box;
35+
};
36+
class DialogTriangleWatcher : public DetectorToFinder<DialogTriangleDetector>{
37+
public:
38+
DialogTriangleWatcher(Color color = COLOR_RED)
39+
: DetectorToFinder("DialogTriangleWatcher", std::chrono::milliseconds(250), color)
40+
{}
4241
};
43-
4442

4543

4644

SerialPrograms/Source/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_CaughtScreen.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ StateMachineAction mash_A_to_entrance(
5050

5151
EntranceDetector entrance_detector(entrance);
5252
SelectionArrowFinder prompt(stream.overlay(), {0.362689, 0.282828, 0.625000, 0.580808});
53-
DialogTriangleDetector triangle(stream.logger(), stream.overlay(), true);
54-
BlackDialogBoxDetector dialog(true);
53+
WhiteDialogBoxWatcher white_dialog;
54+
BlackDialogBoxDetector black_dialog(true);
5555
ReceivePokemonWatcher receive(COLOR_RED);
5656

5757
context.wait_for_all_requests();
@@ -61,8 +61,8 @@ StateMachineAction mash_A_to_entrance(
6161
{
6262
entrance_detector,
6363
prompt,
64-
triangle,
65-
dialog,
64+
white_dialog,
65+
black_dialog,
6666
receive,
6767
},
6868
INFERENCE_RATE

SerialPrograms/Source/PokemonSwSh/Programs/DateSpamFarmers/PokemonSwSh_DateSpam-WattTraderFarmer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void WattTraderFarmer::buy_one(SingleSwitchProgramEnvironment& env, ProControlle
103103

104104
bool purchased = false;
105105
while (true){
106-
DialogTriangleDetector dialog(env.logger(), env.console, true);
106+
DialogTriangleWatcher dialog;
107107
BuyCursorDetector item_select(env.console);
108108
BuyQuantityDetector quantity;
109109
context.wait_for_all_requests();

SerialPrograms/Source/Tests/PokemonSwSh_Tests.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ int test_pokemonSwSh_MaxLair_BattleMenuDetector(const ImageViewRGB32& image, boo
5555
}
5656

5757
int test_pokemonSwSh_DialogTriangleDetector(const ImageViewRGB32& image, bool target){
58-
auto& logger = global_logger_command_line();
59-
auto overlay = DummyVideoOverlay();
60-
const bool stop_on_detected = true;
61-
62-
DialogTriangleDetector detector(logger, overlay, stop_on_detected);
63-
58+
DialogTriangleWatcher detector;
6459
bool result = detector.process_frame(image, current_time());
6560
TEST_RESULT_EQUAL(result, target);
6661
return 0;

0 commit comments

Comments
 (0)