From 5a78440b702b1170a6b9a81f7713d088f2019b7e Mon Sep 17 00:00:00 2001 From: jw098 Date: Sat, 29 Jun 2024 10:50:19 -0700 Subject: [PATCH] change Material detector box member fields into an array of boxes --- .../DevPrograms/TestProgramSwitch.cpp | 4 +- .../PokemonSV_ItemPrinterMaterialDetector.cpp | 205 ++++-------------- .../PokemonSV_ItemPrinterMaterialDetector.h | 49 +---- .../ItemPrinter/PokemonSV_ItemPrinterRNG.cpp | 2 +- 4 files changed, 52 insertions(+), 208 deletions(-) diff --git a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp index 647597ec0..b6d19fb41 100644 --- a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp +++ b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp @@ -268,8 +268,8 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope& #if 0 ItemPrinterMaterialDetector detector(COLOR_RED, Language::English); detector.make_overlays(overlays); - detector.find_happiny_dust_row_num(env.inference_dispatcher(), console, context); - cout << (int)detector.detect_material_quantity(env.inference_dispatcher(), console, context, 2) << endl; + cout << (int)detector.find_happiny_dust_row_index(env.inference_dispatcher(), console, context) << endl; + // cout << (int)detector.detect_material_quantity(env.inference_dispatcher(), console, context, 2) << endl; #endif #if 0 diff --git a/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.cpp b/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.cpp index 964827836..cb34063e8 100644 --- a/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.cpp +++ b/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.cpp @@ -53,69 +53,31 @@ OCR::StringMatchResult MaterialNameReader::read_substring( ItemPrinterMaterialDetector::ItemPrinterMaterialDetector(Color color, Language language) : m_color(color) , m_language(language) - , m_box_1_mat_value(0.39, 0.176, 0.025, 0.050) - , m_box_1_mat_quantity(0.485, 0.176, 0.037, 0.050) - , m_box_1_mat_name(0.090, 0.176, 0.275, 0.050) - , m_box_2_mat_value(0.39, 0.251, 0.025, 0.050) - , m_box_2_mat_quantity(0.485, 0.251, 0.037, 0.050) - , m_box_2_mat_name(0.090, 0.251, 0.275, 0.050) - , m_box_3_mat_value(0.39, 0.328, 0.025, 0.050) - , m_box_3_mat_quantity(0.485, 0.328, 0.037, 0.050) - , m_box_3_mat_name(0.090, 0.328, 0.275, 0.050) - , m_box_4_mat_value(0.39, 0.400, 0.025, 0.050) - , m_box_4_mat_quantity(0.485, 0.400, 0.037, 0.050) - , m_box_4_mat_name(0.090, 0.400, 0.275, 0.050) - , m_box_5_mat_value(0.39, 0.472, 0.025, 0.050) - , m_box_5_mat_quantity(0.485, 0.472, 0.037, 0.050) - , m_box_5_mat_name(0.090, 0.472, 0.275, 0.050) - , m_box_6_mat_value(0.39, 0.546, 0.025, 0.050) - , m_box_6_mat_quantity(0.485, 0.546, 0.037, 0.050) - , m_box_6_mat_name(0.090, 0.546, 0.275, 0.050) - , m_box_7_mat_value(0.39, 0.623, 0.025, 0.050) - , m_box_7_mat_quantity(0.485, 0.623, 0.037, 0.050) - , m_box_7_mat_name(0.090, 0.623, 0.275, 0.050) - , m_box_8_mat_value(0.39, 0.698, 0.025, 0.050) - , m_box_8_mat_quantity(0.485, 0.698, 0.037, 0.050) - , m_box_8_mat_name(0.090, 0.698, 0.275, 0.050) - , m_box_9_mat_value(0.39, 0.771, 0.025, 0.050) - , m_box_9_mat_quantity(0.485, 0.771, 0.037, 0.050) - , m_box_9_mat_name(0.090, 0.771, 0.275, 0.050) - , m_box_10_mat_value(0.39, 0.843, 0.025, 0.050) - , m_box_10_mat_quantity(0.485, 0.843, 0.037, 0.050) - , m_box_10_mat_name(0.090, 0.843, 0.275, 0.050) + , m_box_mat_value(Material_Boxes(ImageFloatBox(0.39, 0.176758, 0.025, 0.050))) + , m_box_mat_quantity(Material_Boxes(ImageFloatBox(0.485, 0.176758, 0.037, 0.050))) + , m_box_mat_name(Material_Boxes(ImageFloatBox(0.090, 0.176758, 0.275, 0.050))) {} +std::array ItemPrinterMaterialDetector::Material_Boxes(ImageFloatBox initial_box){ + std::array material_boxes; + double x = initial_box.x; + double width = initial_box.width; + double height = initial_box.height; + double initial_y = initial_box.y; + double y_spacing = 0.074219; + for (size_t i = 0; i < 10; i++){ + double y = initial_y + i*y_spacing; + material_boxes[i] = ImageFloatBox(x, y, width, height); + } + return material_boxes; +} + void ItemPrinterMaterialDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_box_1_mat_value); - items.add(m_color, m_box_1_mat_quantity); - items.add(m_color, m_box_1_mat_name); - items.add(m_color, m_box_2_mat_value); - items.add(m_color, m_box_2_mat_quantity); - items.add(m_color, m_box_2_mat_name); - items.add(m_color, m_box_3_mat_value); - items.add(m_color, m_box_3_mat_quantity); - items.add(m_color, m_box_3_mat_name); - items.add(m_color, m_box_4_mat_value); - items.add(m_color, m_box_4_mat_quantity); - items.add(m_color, m_box_4_mat_name); - items.add(m_color, m_box_5_mat_value); - items.add(m_color, m_box_5_mat_quantity); - items.add(m_color, m_box_5_mat_name); - items.add(m_color, m_box_6_mat_value); - items.add(m_color, m_box_6_mat_quantity); - items.add(m_color, m_box_6_mat_name); - items.add(m_color, m_box_7_mat_value); - items.add(m_color, m_box_7_mat_quantity); - items.add(m_color, m_box_7_mat_name); - items.add(m_color, m_box_8_mat_value); - items.add(m_color, m_box_8_mat_quantity); - items.add(m_color, m_box_8_mat_name); - items.add(m_color, m_box_9_mat_value); - items.add(m_color, m_box_9_mat_quantity); - items.add(m_color, m_box_9_mat_name); - items.add(m_color, m_box_10_mat_value); - items.add(m_color, m_box_10_mat_quantity); - items.add(m_color, m_box_10_mat_name); + for (size_t i = 0; i < 10; i++){ + items.add(m_color, m_box_mat_value[i]); + items.add(m_color, m_box_mat_quantity[i]); + items.add(m_color, m_box_mat_name[i]); + } } int16_t ItemPrinterMaterialDetector::read_number( @@ -220,20 +182,20 @@ int16_t ItemPrinterMaterialDetector::read_number_black_or_white_text( // return row number where Happiny dust is located on screen // keep pressing DPAD_RIGHT until Happiny dust is on screen // check each row on the screen for Happiny Dust -int8_t ItemPrinterMaterialDetector::find_happiny_dust_row_num( +int8_t ItemPrinterMaterialDetector::find_happiny_dust_row_index( AsyncDispatcher& dispatcher, ConsoleHandle& console, BotBaseContext& context ) const{ - int8_t value_68_row_num; + int8_t value_68_row_index; for (size_t c = 0; c < 10; c++){ context.wait_for_all_requests(); - value_68_row_num = find_material_value_row_num(dispatcher, console, context, 68); - if (value_68_row_num != -1 - && detect_material_name(console, context, value_68_row_num) == "happiny-dust" + value_68_row_index = find_material_value_row_index(dispatcher, console, context, 68); + if (value_68_row_index != -1 + && detect_material_name(console, context, value_68_row_index) == "happiny-dust" ){ // found screen and row number with Happiny dust. - // std::cout << "Happiny dust found. Row number: " << std::to_string(value_68_row_num) << std::endl; - return value_68_row_num; + // std::cout << "Happiny dust found. Row number: " << std::to_string(value_68_row_index) << std::endl; + return value_68_row_index; } // keep searching for Happiny dust pbf_press_dpad(context, DPAD_RIGHT, 20, 30); @@ -247,17 +209,17 @@ int8_t ItemPrinterMaterialDetector::find_happiny_dust_row_num( } -// detects the material name at the given row_num +// detects the material name at the given row_index // MaterialNameReader has a very limited dictionary, // so it can only reliably read the material names with 68% value // (i.e. Ditto Goo, Happiny Dust, Magby Hair, Beldum Claw) std::string ItemPrinterMaterialDetector::detect_material_name( ConsoleHandle& console, BotBaseContext& context, - int8_t row_num + int8_t row_index ) const{ VideoSnapshot snapshot = console.video().snapshot(); - ImageFloatBox material_name_box = get_material_name_box(row_num); + ImageFloatBox material_name_box = m_box_mat_name[row_index]; ImageViewRGB32 material_name_image = extract_box_reference(snapshot, material_name_box); const auto ocr_result = MaterialNameReader::instance().read_substring( console, m_language, @@ -287,21 +249,18 @@ std::string ItemPrinterMaterialDetector::detect_material_name( } // return row number that matches given material_value. else return -1 -int8_t ItemPrinterMaterialDetector::find_material_value_row_num( +int8_t ItemPrinterMaterialDetector::find_material_value_row_index( AsyncDispatcher& dispatcher, ConsoleHandle& console, BotBaseContext& context, int16_t material_value ) const{ + context.wait_for_all_requests(); VideoSnapshot snapshot = console.video().snapshot(); - for (size_t c = 0; c < 10; c++){ - context.wait_for_all_requests(); - int8_t row_num = (int8_t)c+1; - ImageFloatBox material_value_box = get_material_value_box(row_num); - - int16_t value = read_number(console, dispatcher, snapshot, material_value_box); + for (int8_t i = 0; i < 10; i++){ + int16_t value = read_number(console, dispatcher, snapshot, m_box_mat_value[i]); if (value == material_value){ - return row_num; + return i; } } @@ -314,100 +273,14 @@ int16_t ItemPrinterMaterialDetector::detect_material_quantity( AsyncDispatcher& dispatcher, ConsoleHandle& console, BotBaseContext& context, - int8_t row_num + int8_t row_index ) const{ context.wait_for_all_requests(); VideoSnapshot snapshot = console.video().snapshot(); - int16_t value = read_number(console, dispatcher, snapshot, get_material_quantity_box(row_num)); + int16_t value = read_number(console, dispatcher, snapshot, m_box_mat_quantity[row_index]); return value; } -// return image float box for the material quantity at the given row_num -ImageFloatBox ItemPrinterMaterialDetector::get_material_quantity_box(int8_t row_num) const{ - switch(row_num){ - case 1: - return m_box_1_mat_quantity; - case 2: - return m_box_2_mat_quantity; - case 3: - return m_box_3_mat_quantity; - case 4: - return m_box_4_mat_quantity; - case 5: - return m_box_5_mat_quantity; - case 6: - return m_box_6_mat_quantity; - case 7: - return m_box_7_mat_quantity; - case 8: - return m_box_8_mat_quantity; - case 9: - return m_box_9_mat_quantity; - case 10: - return m_box_10_mat_quantity; - default: - return m_box_1_mat_quantity; - } -} - -// return image float box for the material value at the given row_num -ImageFloatBox ItemPrinterMaterialDetector::get_material_value_box(int8_t row_num) const{ - switch(row_num){ - case 1: - return m_box_1_mat_value; - case 2: - return m_box_2_mat_value; - case 3: - return m_box_3_mat_value; - case 4: - return m_box_4_mat_value; - case 5: - return m_box_5_mat_value; - case 6: - return m_box_6_mat_value; - case 7: - return m_box_7_mat_value; - case 8: - return m_box_8_mat_value; - case 9: - return m_box_9_mat_value; - case 10: - return m_box_10_mat_value; - default: - return m_box_1_mat_value; - } -} - -// return image float box for the material name at the given row_num -ImageFloatBox ItemPrinterMaterialDetector::get_material_name_box(int8_t row_num) const{ - switch(row_num){ - case 1: - return m_box_1_mat_name; - case 2: - return m_box_2_mat_name; - case 3: - return m_box_3_mat_name; - case 4: - return m_box_4_mat_name; - case 5: - return m_box_5_mat_name; - case 6: - return m_box_6_mat_name; - case 7: - return m_box_7_mat_name; - case 8: - return m_box_8_mat_name; - case 9: - return m_box_9_mat_name; - case 10: - return m_box_10_mat_name; - default: - return m_box_1_mat_name; - } -} - - - diff --git a/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.h b/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.h index ad3454690..1ff634896 100644 --- a/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.h +++ b/SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.h @@ -12,6 +12,7 @@ #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/Inference/VisualDetector.h" #include "CommonFramework/OCR/OCR_SmallDictionaryMatcher.h" +#include namespace PokemonAutomation{ class Logger; @@ -46,12 +47,14 @@ class ItemPrinterMaterialDetector{ void make_overlays(VideoOverlaySet& items) const; - int8_t find_happiny_dust_row_num( + std::array Material_Boxes(ImageFloatBox initial_box); + + int8_t find_happiny_dust_row_index( AsyncDispatcher& dispatcher, ConsoleHandle& console, BotBaseContext& context ) const; - int8_t find_material_value_row_num( + int8_t find_material_value_row_index( AsyncDispatcher& dispatcher, ConsoleHandle& console, BotBaseContext& context, @@ -62,13 +65,13 @@ class ItemPrinterMaterialDetector{ AsyncDispatcher& dispatcher, ConsoleHandle& console, BotBaseContext& context, - int8_t row_num + int8_t row_index ) const; std::string detect_material_name( ConsoleHandle& console, BotBaseContext& context, - int8_t row_num + int8_t row_index ) const; @@ -84,45 +87,13 @@ class ItemPrinterMaterialDetector{ bool is_white_text ) const; - ImageFloatBox get_material_quantity_box(int8_t row_num) const; - - ImageFloatBox get_material_value_box(int8_t row_num) const; - - ImageFloatBox get_material_name_box(int8_t row_num) const; private: Color m_color; Language m_language; - ImageFloatBox m_box_1_mat_value; - ImageFloatBox m_box_1_mat_quantity; - ImageFloatBox m_box_1_mat_name; - ImageFloatBox m_box_2_mat_value; - ImageFloatBox m_box_2_mat_quantity; - ImageFloatBox m_box_2_mat_name; - ImageFloatBox m_box_3_mat_value; - ImageFloatBox m_box_3_mat_quantity; - ImageFloatBox m_box_3_mat_name; - ImageFloatBox m_box_4_mat_value; - ImageFloatBox m_box_4_mat_quantity; - ImageFloatBox m_box_4_mat_name; - ImageFloatBox m_box_5_mat_value; - ImageFloatBox m_box_5_mat_quantity; - ImageFloatBox m_box_5_mat_name; - ImageFloatBox m_box_6_mat_value; - ImageFloatBox m_box_6_mat_quantity; - ImageFloatBox m_box_6_mat_name; - ImageFloatBox m_box_7_mat_value; - ImageFloatBox m_box_7_mat_quantity; - ImageFloatBox m_box_7_mat_name; - ImageFloatBox m_box_8_mat_value; - ImageFloatBox m_box_8_mat_quantity; - ImageFloatBox m_box_8_mat_name; - ImageFloatBox m_box_9_mat_value; - ImageFloatBox m_box_9_mat_quantity; - ImageFloatBox m_box_9_mat_name; - ImageFloatBox m_box_10_mat_value; - ImageFloatBox m_box_10_mat_quantity; - ImageFloatBox m_box_10_mat_name; + std::array m_box_mat_value; + std::array m_box_mat_quantity; + std::array m_box_mat_name; }; diff --git a/SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNG.cpp b/SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNG.cpp index ddedbd961..6fbf91e82 100644 --- a/SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNG.cpp +++ b/SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNG.cpp @@ -765,7 +765,7 @@ uint32_t ItemPrinterRNG::check_num_happiny_dust( env.log("Detected material selection."); ItemPrinterMaterialDetector detector(COLOR_RED, LANGUAGE); - int8_t happiny_dust_row_num = detector.find_happiny_dust_row_num(env.inference_dispatcher(), env.console, context); + int8_t happiny_dust_row_num = detector.find_happiny_dust_row_index(env.inference_dispatcher(), env.console, context); num_happiny_dust = detector.detect_material_quantity(env.inference_dispatcher(), env.console, context, happiny_dust_row_num); pbf_mash_button(context, BUTTON_B, 100); return num_happiny_dust;