Skip to content

Commit

Permalink
StationaryOverworldWatcher (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw098 authored Nov 3, 2024
1 parent 4da5a2d commit 6561baf
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,8 @@ file(GLOB MAIN_SOURCES
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.cpp
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.cpp
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.h
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.cpp
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.h
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichHandDetector.cpp
Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/SerialPrograms.pro
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ SOURCES += \
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoHpReader.cpp \
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.cpp \
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.cpp \
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.cpp \
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.cpp \
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichHandDetector.cpp \
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichIngredientDetector.cpp \
Expand Down Expand Up @@ -1761,6 +1762,7 @@ HEADERS += \
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoHpReader.h \
Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h \
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h \
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.h \
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.h \
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichHandDetector.h \
Source/PokemonSV/Inference/Picnics/PokemonSV_SandwichIngredientDetector.h \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Stationary Overworld Detector
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/

#include "Kernels/Waterfill/Kernels_Waterfill_Session.h"
#include "CommonFramework/Globals.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonFramework/ImageMatch/ImageDiff.h"
#include "CommonFramework/ImageMatch/ExactImageMatcher.h"
#include "CommonFramework/ImageTools/BinaryImage_FilterRgb32.h"
#include "CommonFramework/ImageTools/WaterfillUtilities.h"
#include "CommonFramework/ImageMatch/WaterfillTemplateMatcher.h"
#include "PokemonSV_StationaryOverworldWatcher.h"

//#include <iostream>
//using std::cout;
//using std::endl;

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{





StationaryOverworldWatcher::StationaryOverworldWatcher(Color color, ImageFloatBox box, size_t seconds_stationary)
: VisualInferenceCallback("StationaryOverworldWatcher")
, m_color(color)
, m_box(box)
, m_overworld_detector(color)
, m_map_hold_duration(std::chrono::milliseconds(seconds_stationary * 1000))
{}

void StationaryOverworldWatcher::make_overlays(VideoOverlaySet& items) const{
items.add(m_color, m_box);
}

bool StationaryOverworldWatcher::process_frame(const VideoSnapshot& frame){
if (!m_overworld_detector.detect(frame)){
m_snapshot_start.clear();
return false;
}

if (!m_snapshot_start){
m_snapshot_start = frame;
return false;
}

// Check if radar map stays still for `m_map_hold_duration`

// Mismatching image sizes.
ImageViewRGB32 start = extract_box_reference(m_snapshot_start, m_box);
ImageViewRGB32 current = extract_box_reference(frame, m_box);
if (start.width() != current.width() || start.height() != current.height()){
m_snapshot_start = frame;
return false;
}

double rmsd = ImageMatch::pixel_RMSD(start, current);
// cout << "rmsd = " << rmsd << endl;
if (rmsd > 2.0){ // Image of radar map has changed too much.
m_snapshot_start = frame;
return false;
}

// Make sure radar map held for long enough.
return frame.timestamp - m_snapshot_start.timestamp >= m_map_hold_duration;
}



}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Stationary Overworld Detector
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/

#ifndef PokemonAutomation_PokemonSV_StationaryOverworldWatcher_H
#define PokemonAutomation_PokemonSV_StationaryOverworldWatcher_H

#include "Common/Cpp/Color.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/InferenceInfra/VisualInferenceCallback.h"
#include "CommonFramework/Inference/VisualDetector.h"
#include "PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h"


namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{



class StationaryOverworldWatcher : public VisualInferenceCallback{
public:
StationaryOverworldWatcher(Color color = COLOR_RED, ImageFloatBox box = {0.865, 0.82, 0.08, 0.1}, size_t seconds_stationary = 5);

virtual void make_overlays(VideoOverlaySet& items) const override;
// return true if the screen within `m_box` doesn't change for am amount of time as per `seconds_stationary`
// the overworld must be detected throughout this.
virtual bool process_frame(const VideoSnapshot& frame) override;


private:
const Color m_color;
const ImageFloatBox m_box;
const OverworldDetector m_overworld_detector;
const std::chrono::milliseconds m_map_hold_duration;
VideoSnapshot m_snapshot_start;
};


}
}
}
#endif

0 comments on commit 6561baf

Please sign in to comment.