From 2aedcd0bd8fc8cdd5664cea5dd67a5d7d0efc4d4 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 18:54:38 -0700 Subject: [PATCH] Fix BP OCR crop including the "BP" suffix The Blueberry Points crop ended at x=0.957, which included the left stroke of the "B" in the "BP" suffix. PaddleOCR read it as an extra digit, so a balance of 6750 came back as 67501. Narrow the crop to end at x=0.954 and move the reader into Inference/PokemonSV_BlueberryPointsReader so it can be tested from a captured frame without depending on the BBQ program code. read_BP() still opens and closes the panel and delegates to the shared reader. Register four PokemonSV::BlueberryPoints unit tests at 1920x1080 and 1280x720 on two full-frame reference screenshots (6750 and 6740 BP, taken one reroll apart). The images go to the CommandLineTests repo under PokemonSV/BlueberryPoints/. With the old crop restored, all four fail. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV_BlueberryPointsReader.cpp | 39 ++++++++++++++++++ .../PokemonSV_BlueberryPointsReader.h | 25 ++++++++++++ .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 40 +++++++++++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 8 +--- SerialPrograms/cmake/SourceFiles.cmake | 2 + 5 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h 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 7788036545..02e94b05c7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -28,6 +28,9 @@ #include "Options/PokemonSV_BBQuests.h" #include "Programs/Farming/PokemonSV_BlueberryQuests.h" #include "PokemonSV_Tests.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Inference/PokemonSV_BlueberryPointsReader.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -116,8 +119,45 @@ class Test_BBQuestsSlugs : public UnitTest{ } +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; +}; + +} + void add_tests(UnitTestDatabase& database){ database.add(); + + // Full-frame references, in the CommandLineTests repo: they 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_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 8bec917a63..c90bae7a04 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -32,6 +32,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" @@ -67,17 +68,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 39c48d531e..251ef0cf6b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2386,6 +2386,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