|
| 1 | +/* Detect Home |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" |
| 8 | +#include "CommonTools/Images/SolidColorTest.h" |
| 9 | +#include "NintendoSwitch_SelectedSettingDetector.h" |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +using std::cout; |
| 13 | +using std::endl; |
| 14 | + |
| 15 | +namespace PokemonAutomation{ |
| 16 | +namespace NintendoSwitch{ |
| 17 | + |
| 18 | +SelectedSettingWatcher::~SelectedSettingWatcher() = default; |
| 19 | + |
| 20 | +SelectedSettingWatcher::SelectedSettingWatcher(ImageFloatBox selected_box, ImageFloatBox not_selected_box1, ImageFloatBox not_selected_box2) |
| 21 | + : VisualInferenceCallback("SelectedSettingWatcher") |
| 22 | + , m_selected_box(selected_box) |
| 23 | + , m_not_selected_box1(not_selected_box1) |
| 24 | + , m_not_selected_box2(not_selected_box2) |
| 25 | +{} |
| 26 | + |
| 27 | +void SelectedSettingWatcher::make_overlays(VideoOverlaySet& items) const{ |
| 28 | + items.add(COLOR_RED, m_selected_box); |
| 29 | + items.add(COLOR_BLUE, m_not_selected_box1); |
| 30 | + items.add(COLOR_BLUE, m_not_selected_box2); |
| 31 | +} |
| 32 | + |
| 33 | +bool is_white_theme(const ImageViewRGB32& screen){ |
| 34 | + ImageFloatBox window_top(0.60, 0.02, 0.35, 0.05); |
| 35 | + ImageStats stats_window_top = image_stats(extract_box_reference(screen, window_top)); |
| 36 | + bool white_theme = stats_window_top.average.sum() > 600; |
| 37 | + return white_theme; |
| 38 | +} |
| 39 | + |
| 40 | +bool SelectedSettingWatcher::process_frame(const ImageViewRGB32& screen, WallClock timestamp){ |
| 41 | + |
| 42 | + ImageStats stats_unselected_box1 = image_stats(extract_box_reference(screen, m_not_selected_box1)); |
| 43 | + double unselected1_average_sum = stats_unselected_box1.average.sum(); |
| 44 | + cout << "unselected_average_sum1: " << std::to_string(unselected1_average_sum) << endl; |
| 45 | + |
| 46 | + ImageStats stats_unselected_box2 = image_stats(extract_box_reference(screen, m_not_selected_box2)); |
| 47 | + double unselected2_average_sum = stats_unselected_box2.average.sum(); |
| 48 | + cout << "unselected_average_sum2: " << std::to_string(unselected2_average_sum) << endl; |
| 49 | + |
| 50 | + double average_sum_unselected_diff = std::abs(unselected1_average_sum - unselected2_average_sum); |
| 51 | + |
| 52 | + ImageStats stats_selected_box = image_stats(extract_box_reference(screen, m_selected_box)); |
| 53 | + double selected_average_sum = stats_selected_box.average.sum(); |
| 54 | + cout << "selected_average_sum: " << std::to_string(selected_average_sum) << endl; |
| 55 | + |
| 56 | + bool is_selected = false; |
| 57 | + if (is_white_theme(screen)){ // light mode |
| 58 | + // unselected should be brighter than selected |
| 59 | + is_selected = selected_average_sum < std::min(unselected1_average_sum, unselected2_average_sum) - average_sum_unselected_diff - 20 ; |
| 60 | + }else{ // dark mode |
| 61 | + // selected should be brighter than unselected |
| 62 | + is_selected = selected_average_sum > std::max(unselected1_average_sum, unselected2_average_sum) + average_sum_unselected_diff + 20; |
| 63 | + } |
| 64 | + |
| 65 | + return is_selected; |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +} |
| 74 | +} |
0 commit comments