Skip to content

Commit 2037acb

Browse files
jw098fyex
authored andcommitted
StationaryOverworldWatcher (PokemonAutomation#501)
1 parent dbcc603 commit 2037acb

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,8 @@ file(GLOB MAIN_SOURCES
13321332
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h
13331333
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.cpp
13341334
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h
1335+
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.cpp
1336+
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.h
13351337
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.cpp
13361338
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.h
13371339
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichHandDetector.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ SOURCES += \
666666
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoHpReader.cpp \
667667
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.cpp \
668668
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.cpp \
669+
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.cpp \
669670
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.cpp \
670671
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichHandDetector.cpp \
671672
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichIngredientDetector.cpp \
@@ -1763,6 +1764,7 @@ HEADERS += \
17631764
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoHpReader.h \
17641765
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h \
17651766
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h \
1767+
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.h \
17661768
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.h \
17671769
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichHandDetector.h \
17681770
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichIngredientDetector.h \
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Stationary Overworld Detector
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#include "Kernels/Waterfill/Kernels_Waterfill_Session.h"
8+
#include "CommonFramework/Globals.h"
9+
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
10+
#include "CommonFramework/ImageMatch/ImageDiff.h"
11+
#include "CommonFramework/ImageMatch/ExactImageMatcher.h"
12+
#include "CommonFramework/ImageTools/BinaryImage_FilterRgb32.h"
13+
#include "CommonFramework/ImageTools/WaterfillUtilities.h"
14+
#include "CommonFramework/ImageMatch/WaterfillTemplateMatcher.h"
15+
#include "PokemonSV_StationaryOverworldWatcher.h"
16+
17+
//#include <iostream>
18+
//using std::cout;
19+
//using std::endl;
20+
21+
namespace PokemonAutomation{
22+
namespace NintendoSwitch{
23+
namespace PokemonSV{
24+
25+
26+
27+
28+
29+
StationaryOverworldWatcher::StationaryOverworldWatcher(Color color, ImageFloatBox box, size_t seconds_stationary)
30+
: VisualInferenceCallback("StationaryOverworldWatcher")
31+
, m_color(color)
32+
, m_box(box)
33+
, m_overworld_detector(color)
34+
, m_map_hold_duration(std::chrono::milliseconds(seconds_stationary * 1000))
35+
{}
36+
37+
void StationaryOverworldWatcher::make_overlays(VideoOverlaySet& items) const{
38+
items.add(m_color, m_box);
39+
}
40+
41+
bool StationaryOverworldWatcher::process_frame(const VideoSnapshot& frame){
42+
if (!m_overworld_detector.detect(frame)){
43+
m_snapshot_start.clear();
44+
return false;
45+
}
46+
47+
if (!m_snapshot_start){
48+
m_snapshot_start = frame;
49+
return false;
50+
}
51+
52+
// Check if radar map stays still for `m_map_hold_duration`
53+
54+
// Mismatching image sizes.
55+
ImageViewRGB32 start = extract_box_reference(m_snapshot_start, m_box);
56+
ImageViewRGB32 current = extract_box_reference(frame, m_box);
57+
if (start.width() != current.width() || start.height() != current.height()){
58+
m_snapshot_start = frame;
59+
return false;
60+
}
61+
62+
double rmsd = ImageMatch::pixel_RMSD(start, current);
63+
// cout << "rmsd = " << rmsd << endl;
64+
if (rmsd > 2.0){ // Image of radar map has changed too much.
65+
m_snapshot_start = frame;
66+
return false;
67+
}
68+
69+
// Make sure radar map held for long enough.
70+
return frame.timestamp - m_snapshot_start.timestamp >= m_map_hold_duration;
71+
}
72+
73+
74+
75+
}
76+
}
77+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Stationary Overworld Detector
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonSV_StationaryOverworldWatcher_H
8+
#define PokemonAutomation_PokemonSV_StationaryOverworldWatcher_H
9+
10+
#include "Common/Cpp/Color.h"
11+
#include "CommonFramework/ImageTools/ImageBoxes.h"
12+
#include "CommonFramework/VideoPipeline/VideoFeed.h"
13+
#include "CommonFramework/InferenceInfra/VisualInferenceCallback.h"
14+
#include "CommonFramework/Inference/VisualDetector.h"
15+
#include "PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h"
16+
17+
18+
namespace PokemonAutomation{
19+
namespace NintendoSwitch{
20+
namespace PokemonSV{
21+
22+
23+
24+
class StationaryOverworldWatcher : public VisualInferenceCallback{
25+
public:
26+
StationaryOverworldWatcher(Color color = COLOR_RED, ImageFloatBox box = {0.865, 0.82, 0.08, 0.1}, size_t seconds_stationary = 5);
27+
28+
virtual void make_overlays(VideoOverlaySet& items) const override;
29+
// return true if the screen within `m_box` doesn't change for am amount of time as per `seconds_stationary`
30+
// the overworld must be detected throughout this.
31+
virtual bool process_frame(const VideoSnapshot& frame) override;
32+
33+
34+
private:
35+
const Color m_color;
36+
const ImageFloatBox m_box;
37+
const OverworldDetector m_overworld_detector;
38+
const std::chrono::milliseconds m_map_hold_duration;
39+
VideoSnapshot m_snapshot_start;
40+
};
41+
42+
43+
}
44+
}
45+
}
46+
#endif

0 commit comments

Comments
 (0)