Skip to content

Commit ee5caca

Browse files
committed
Add time tolerance to Rotom menu detection.
1 parent 43a0ef6 commit ee5caca

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_SelectionArrowFinder.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,26 @@ RotomPhoneMenuArrowFinder::RotomPhoneMenuArrowFinder(VideoOverlay& overlay)
197197
}
198198
}
199199

200-
int RotomPhoneMenuArrowFinder::detect(const ImageViewRGB32& screen){
200+
void RotomPhoneMenuArrowFinder::make_overlays(VideoOverlaySet& items) const{
201+
202+
}
203+
bool RotomPhoneMenuArrowFinder::detect(const ImageViewRGB32& screen){
204+
return detect_index(screen) >= 0;
205+
}
206+
207+
int RotomPhoneMenuArrowFinder::detect_index(const ImageViewRGB32& screen){
201208
const double screen_scale = screen.height() / 1080.0;
202209
const size_t min_arrow_area = size_t(1400 * screen_scale * screen_scale);
203210
for (size_t i_row = 0; i_row < 2; i_row++){
204211
for (size_t j_col = 0; j_col < 5; j_col++){
205212
ImageFloatBox box(0.047 + j_col*0.183, 0.175 + 0.333*i_row, 0.059, 0.104);
206213
std::vector<ImagePixelBox> arrows = find_selection_arrows(
207-
extract_box_reference(screen, box), min_arrow_area);
214+
extract_box_reference(screen, box),
215+
min_arrow_area
216+
);
208217
if (arrows.size() > 0){
209-
return (int)(i_row * 5 + j_col);
218+
m_index = (int)(i_row * 5 + j_col);
219+
return m_index;
210220
}
211221
}
212222
}

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_SelectionArrowFinder.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <deque>
1212
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
1313
#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h"
14+
#include "CommonTools/VisualDetector.h"
1415

1516
namespace PokemonAutomation{
1617
namespace NintendoSwitch{
@@ -77,18 +78,33 @@ class StoragePokemonMenuArrowFinder : public SelectionArrowFinder{
7778
};
7879

7980
// The arrow that points to one of the ten apps on Rotom Phone menu
80-
class RotomPhoneMenuArrowFinder{
81+
class RotomPhoneMenuArrowFinder : public StaticScreenDetector{
8182
public:
8283
RotomPhoneMenuArrowFinder(VideoOverlay& overlay);
8384

85+
int current_index() const{
86+
return m_index;
87+
}
88+
89+
virtual void make_overlays(VideoOverlaySet& items) const override;
90+
virtual bool detect(const ImageViewRGB32& screen) override;
91+
8492
// Detect which app is selected by the arrow. Return the index of the app
8593
// The order is: from top to bottom, from left to right.
8694
// If no arrow found, return -1.
87-
int detect(const ImageViewRGB32& screen);
95+
int detect_index(const ImageViewRGB32& screen);
8896

8997
private:
9098
VideoOverlaySet m_overlay_set;
99+
int m_index = -1;
91100
};
101+
class RotomPhoneMenuArrowWatcher : public DetectorToFinder<RotomPhoneMenuArrowFinder>{
102+
public:
103+
RotomPhoneMenuArrowWatcher(VideoOverlay& overlay)
104+
: DetectorToFinder("RotomPhoneMenuArrowWatcher", std::chrono::milliseconds(250), overlay)
105+
{}
106+
};
107+
92108

93109

94110
}

SerialPrograms/Source/PokemonSwSh/Programs/PokemonSwSh_MenuNavigation.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
#include "CommonFramework/Exceptions/OperationFailedException.h"
99
#include "CommonFramework/Tools/ErrorDumper.h"
10-
#include "CommonFramework/VideoPipeline/VideoFeed.h"
10+
//#include "CommonFramework/VideoPipeline/VideoFeed.h"
11+
#include "CommonTools/Async/InferenceRoutines.h"
1112
#include "PokemonSwSh/Inference/PokemonSwSh_SelectionArrowFinder.h"
1213
#include "PokemonSwSh/Programs/PokemonSwSh_BoxHelpers.h"
1314
#include "PokemonSwSh_MenuNavigation.h"
@@ -24,15 +25,18 @@ void navigate_to_menu_app(
2425
EventNotificationOption& notification_option
2526
){
2627
context.wait_for_all_requests();
27-
RotomPhoneMenuArrowFinder menu_arrow_detector(stream.overlay());
28-
auto snapshot = stream.video().snapshot();
29-
const int cur_app_index = menu_arrow_detector.detect(snapshot);
28+
RotomPhoneMenuArrowWatcher menu_arrow(stream.overlay());
29+
wait_until(
30+
stream, context,
31+
5000ms,
32+
{menu_arrow}
33+
);
34+
const int cur_app_index = menu_arrow.current_index();
3035
if (cur_app_index < 0){
3136
OperationFailedException::fire(
3237
ErrorReport::SEND_ERROR_REPORT,
3338
"Cannot detect Rotom phone menu.",
34-
stream,
35-
std::move(snapshot)
39+
stream
3640
);
3741
}
3842
stream.log("Detect menu cursor at " + std::to_string(cur_app_index) + ".");

0 commit comments

Comments
 (0)