diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp new file mode 100644 index 0000000000..4d5f5fc4ba --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp @@ -0,0 +1,39 @@ +/* Blueberry Points Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/OCR/OCR_NumberReader.h" +#include "PokemonSV_BlueberryPointsReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +int read_BP_from_image(Logger& logger, const ImageViewRGB32& screen){ + // Normalized coordinates for the upper-right balance on a 16:9 quest panel. + // Keep the right edge at 0.954: the old 0.957 edge included the left stroke + // of the B in "BP", making 6750 read as 67501. See the full-frame reference + // UnitTestResources/PokemonSV/BlueberryPoints/6750.png and its README. + const ImageFloatBox BP_NUMBER_BOX(0.866, 0.019, 0.088, 0.041); + // Isolate the near-white digits from the blue panel background. These are + // the existing quest-panel text thresholds, inclusive on all RGB channels. + const uint32_t BP_TEXT_MIN = combine_rgb(198, 198, 198); + const uint32_t BP_TEXT_MAX = combine_rgb(255, 255, 255); + ImageRGB32 number = to_blackwhite_rgb32_range( + extract_box_reference(screen, BP_NUMBER_BOX), true, BP_TEXT_MIN, BP_TEXT_MAX + ); + return OCR::read_number(logger, number); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h new file mode 100644 index 0000000000..d11517de2a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h @@ -0,0 +1,25 @@ +/* Blueberry Points Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BlueberryPointsReader_H +#define PokemonAutomation_PokemonSV_BlueberryPointsReader_H + +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// Read the numeric balance from a captured 16:9 quest panel; -1 on OCR failure. +int read_BP_from_image(Logger& logger, const ImageViewRGB32& screen); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index d966bfe133..e31d2db09e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -26,6 +26,10 @@ #include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h" #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" #include "PokemonSV_Tests.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Inference/PokemonSV_BlueberryPointsReader.h" +#include "Inference/PokemonSV_BlueberryQuestDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -33,7 +37,108 @@ namespace PokemonSV{ +namespace{ + +class Test_BlueberryPoints : public UnitTest{ +public: + Test_BlueberryPoints(const std::string& image, int expected, size_t width, size_t height) + : UnitTest("PokemonSV::BlueberryPoints - " + image + " - " + std::to_string(width)) + , m_image(UNIT_TEST_RESOURCE_PATH() + image) + , m_expected(expected), m_width(width), m_height(height) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + ImageRGB32 reference(m_image); + ImageRGB32 image = reference.scale_to(m_width, m_height); + const int actual = read_BP_from_image(logger, image); + if (actual != m_expected){ + return "Expected " + std::to_string(m_expected) + " BP, read " + std::to_string(actual); + } + return true; + } + +private: + const std::string m_image; + const int m_expected; + const size_t m_width, m_height; +}; + +// One quest slot on a full frame of the quest panel. +struct BlueberryQuestScenario{ + std::string image; + Language language; + BlueberryQuestDetector::QuestPosition position; + std::string expected_slug; + size_t width; + size_t height; +}; + +class Test_BlueberryQuestDetector : public UnitTest{ +public: + Test_BlueberryQuestDetector(BlueberryQuestScenario scenario) + : UnitTest( + "PokemonSV::BlueberryQuestDetector - " + scenario.image + + " - slot " + std::to_string((int)scenario.position + 1) + + " - " + std::to_string(scenario.width) + ) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + ImageRGB32 reference(UNIT_TEST_RESOURCE_PATH() + m_scenario.image); + ImageRGB32 image = reference.scale_to(m_scenario.width, m_scenario.height); + BlueberryQuestDetector detector(logger, COLOR_RED, m_scenario.language, m_scenario.position); + const std::string actual = detector.detect_quest(image); + if (actual != m_scenario.expected_slug){ + return "Expected quest " + m_scenario.expected_slug + ", read \"" + actual + "\""; + } + return true; + } + +private: + const BlueberryQuestScenario m_scenario; +}; + +} + +void add_tests_BlueberryQuestDetector(UnitTestDatabase& database){ + using enum BlueberryQuestDetector::QuestPosition; + // The second frame is the first after rerolling the first quest. Singleplayer + // without a red quest shows 3 quests, so the fourth slot reads as nothing. + const struct{ + std::string image; + std::string slugs[4]; + } panels[] = { + {"PokemonSV/BlueberryPoints/6750.png", {"catch-any", "sneak-up", "auto-10", ""}}, + {"PokemonSV/BlueberryPoints/6740.png", {"photo-swim", "sneak-up", "auto-10", ""}}, + }; + const BlueberryQuestDetector::QuestPosition positions[] = {FIRST, SECOND, THIRD, FOURTH}; + const std::pair sizes[] = {{1920, 1080}, {1280, 720}}; + for (const auto& panel : panels){ + for (size_t slot = 0; slot < 4; slot++){ + for (const auto& [width, height] : sizes){ + database.add(BlueberryQuestScenario{ + .image = panel.image, + .language = Language::ChineseTraditional, + .position = positions[slot], + .expected_slug = panel.slugs[slot], + .width = width, + .height = height, + }); + } + } + } +} + void add_tests(UnitTestDatabase& database){ + // Full-frame references exercise the production crop, filter and OCR together. + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1280, 720); + + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); + add_tests_BlueberryQuestDetector(database); + add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 43740d2c1c..1be2b94795 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -33,6 +33,7 @@ #include "VideoGames/PokemonSV/Programs/PokemonSV_MenuNavigation.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" #include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h" +#include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h" #include "VideoGames/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.h" #include "VideoGames/PokemonSV/Programs/Boxes/PokemonSV_BoxRelease.h" #include "VideoGames/PokemonSV/Inference/Boxes/PokemonSV_BoxEggDetector.h" @@ -159,17 +160,12 @@ int read_BP(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context.wait_for_all_requests(); VideoSnapshot screen = stream.video().snapshot(); - ImageRGB32 BP_value = to_blackwhite_rgb32_range( - extract_box_reference(screen, ImageFloatBox(0.866, 0.019, 0.091, 0.041)), - true, - combine_rgb(198, 198, 198), combine_rgb(255, 255, 255) - ); //Close panel pbf_mash_button(context, BUTTON_B, 800ms); context.wait_for_all_requests(); - return OCR::read_number(stream.logger(), BP_value); + return read_BP_from_image(stream.logger(), screen); } std::vector read_quests( diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 4a370e7d9a..ecfaa79169 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2370,6 +2370,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Inference/PokemonSV_AuctionItemNameReader.h Source/VideoGames/PokemonSV/Inference/PokemonSV_BagDetector.cpp Source/VideoGames/PokemonSV/Inference/PokemonSV_BagDetector.h + Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp + Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.cpp Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestReader.cpp diff --git a/UnitTestResources/PokemonSV/BlueberryPoints/6740.png b/UnitTestResources/PokemonSV/BlueberryPoints/6740.png new file mode 100644 index 0000000000..778116951f Binary files /dev/null and b/UnitTestResources/PokemonSV/BlueberryPoints/6740.png differ diff --git a/UnitTestResources/PokemonSV/BlueberryPoints/6750.png b/UnitTestResources/PokemonSV/BlueberryPoints/6750.png new file mode 100644 index 0000000000..8a5183d4d5 Binary files /dev/null and b/UnitTestResources/PokemonSV/BlueberryPoints/6750.png differ diff --git a/UnitTestResources/PokemonSV/BlueberryPoints/README.md b/UnitTestResources/PokemonSV/BlueberryPoints/README.md new file mode 100644 index 0000000000..84732d36f0 --- /dev/null +++ b/UnitTestResources/PokemonSV/BlueberryPoints/README.md @@ -0,0 +1,41 @@ +# Blueberry Points reader references + +These unmodified 1920×1080 frames were saved with SerialPrograms' Screenshot +button from Pokémon Violet on Switch 1, using the Traditional Chinese UI. + +| Image | Expected BP | Quest in the first slot | +| --- | ---: | --- | +| [6750.png](6750.png) | 6750 | Catch any Pokémon | +| [6740.png](6740.png) | 6740 | Photograph a swimming Pokémon | + +The second frame follows one confirmed 10-BP blue-quest reroll. The balance +decreased by 10 and the first quest changed; the other two quests stayed in +place. No farmer was running during this check. + +`read_BP_from_image()` uses coordinates normalized to the full 16:9 frame: +`(x=0.866, y=0.019, width=0.088, height=0.041)`. The right edge is 0.954. +The previous width, 0.091, ended at 0.957 and included the left stroke of the +adjacent `B` suffix. That stroke was recognized as an extra `1` with PaddleOCR. +The near-white RGB range 198–255 is inherited from the existing reader; this +change does not recalibrate text colors. + +The registered `PokemonSV::BlueberryPoints` tests pass each full frame through +the production crop, color filter and number OCR, at 1920×1080 and 1280×720. +Select these four tests in **Computer → Unit Test Runner**. The runner resolves +these paths under `UNIT_TEST_RESOURCE_PATH()`; keep this `UnitTestResources` +directory alongside the source checkout or application resource directories. +OCR uses the configured backend; the PR validation used PaddleOCR. + +With the old width temporarily restored, all four cases fail: the reader returns +`67501` or `67401`. With width 0.088, all four pass. This checks that the tests +detect the original suffix contamination rather than just recognizing digits. + +Keeping the original frame dimensions is intentional: testing an already-cropped +number would not catch regression of the production crop into the suffix. +These two four-digit balances do not establish accuracy for all balance widths, +UI languages or capture formats. + +The `PokemonSV::BlueberryQuestDetector` tests read the quest slots of the same +frames with Traditional Chinese OCR: catch-any, sneak-up and auto-10 in 6750.png, +and photo-swim, sneak-up and auto-10 in 6740.png. The fourth slot is empty and +must read as nothing.