Skip to content

Commit

Permalink
Pokemon Moves reader (#474)
Browse files Browse the repository at this point in the history
* Pokemon Moves reader

* remove unused headers

* update cmakelists
  • Loading branch information
jw098 authored Sep 8, 2024
1 parent 67cacd1 commit f5185ea
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 20 deletions.
12 changes: 2 additions & 10 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1348,12 +1348,10 @@ file(GLOB MAIN_SOURCES
Source/PokemonSV/Inference/PokemonSV_ESPEmotionDetector.h
Source/PokemonSV/Inference/PokemonSV_MainMenuDetector.cpp
Source/PokemonSV/Inference/PokemonSV_MainMenuDetector.h
# Source/PokemonSV/Inference/PokemonSV_MenuOptionReader.cpp
# Source/PokemonSV/Inference/PokemonSV_MenuOptionReader.h
Source/PokemonSV/Inference/PokemonSV_MoneyReader.cpp
Source/PokemonSV/Inference/PokemonSV_MoneyReader.h
# Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.cpp
# Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.h
Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.cpp
Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.h
Source/PokemonSV/Inference/PokemonSV_PokePortalDetector.cpp
Source/PokemonSV/Inference/PokemonSV_PokePortalDetector.h
Source/PokemonSV/Inference/PokemonSV_PokemonSummaryReader.cpp
Expand Down Expand Up @@ -1424,12 +1422,6 @@ file(GLOB MAIN_SOURCES
Source/PokemonSV/PokemonSV_Panels.h
Source/PokemonSV/PokemonSV_Settings.cpp
Source/PokemonSV/PokemonSV_Settings.h
# Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp
# Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.h
# Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOption.cpp
# Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOption.h
# Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOptionDatabase.cpp
# Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOptionDatabase.h
Source/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.cpp
Source/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.h
Source/PokemonSV/Programs/Battles/PokemonSV_Battles.cpp
Expand Down
12 changes: 2 additions & 10 deletions SerialPrograms/SerialPrograms.pro
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,8 @@ SOURCES += \
Source/PokemonSV/Inference/PokemonSV_ClothingTopDetector.cpp \
Source/PokemonSV/Inference/PokemonSV_ESPEmotionDetector.cpp \
Source/PokemonSV/Inference/PokemonSV_MainMenuDetector.cpp \
#Source/PokemonSV/Inference/PokemonSV_MenuOptionReader.cpp \
Source/PokemonSV/Inference/PokemonSV_MoneyReader.cpp \
#Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.cpp \
Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.cpp \
Source/PokemonSV/Inference/PokemonSV_PokePortalDetector.cpp \
Source/PokemonSV/Inference/PokemonSV_PokemonSummaryReader.cpp \
Source/PokemonSV/Inference/PokemonSV_StatHexagonReader.cpp \
Expand Down Expand Up @@ -711,9 +710,6 @@ SOURCES += \
Source/PokemonSV/Options/PokemonSV_TournamentPrizeTable.cpp \
Source/PokemonSV/PokemonSV_Panels.cpp \
Source/PokemonSV/PokemonSV_Settings.cpp \
#Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp \
#Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOption.cpp \
#Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOptionDatabase.cpp \
Source/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.cpp \
Source/PokemonSV/Programs/Battles/PokemonSV_Battles.cpp \
Source/PokemonSV/Programs/Battles/PokemonSV_SinglesBattler.cpp \
Expand Down Expand Up @@ -1756,9 +1752,8 @@ HEADERS += \
Source/PokemonSV/Inference/PokemonSV_ClothingTopDetector.h \
Source/PokemonSV/Inference/PokemonSV_ESPEmotionDetector.h \
Source/PokemonSV/Inference/PokemonSV_MainMenuDetector.h \
#Source/PokemonSV/Inference/PokemonSV_MenuOptionReader.h \
Source/PokemonSV/Inference/PokemonSV_MoneyReader.h \
#Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.h \
Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.h \
Source/PokemonSV/Inference/PokemonSV_PokePortalDetector.h \
Source/PokemonSV/Inference/PokemonSV_PokemonSummaryReader.h \
Source/PokemonSV/Inference/PokemonSV_StatHexagonReader.h \
Expand Down Expand Up @@ -1795,9 +1790,6 @@ HEADERS += \
Source/PokemonSV/Options/PokemonSV_TournamentPrizeTable.h \
Source/PokemonSV/PokemonSV_Panels.h \
Source/PokemonSV/PokemonSV_Settings.h \
#Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.h \
#Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOption.h \
#Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOptionDatabase.h \
Source/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.h \
Source/PokemonSV/Programs/Battles/PokemonSV_Battles.h \
Source/PokemonSV/Programs/Battles/PokemonSV_SinglesBattler.h \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* Pokemon Moves Reader
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/

#include <algorithm>
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
#include "PokemonSV_PokemonMovesReader.h"

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

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{



PokemonMovesOCR& PokemonMovesOCR::instance(){
static PokemonMovesOCR reader;
return reader;
}

PokemonMovesOCR::PokemonMovesOCR()
: SmallDictionaryMatcher("PokemonSV/PokemonMovesOCR.json")
{}

OCR::StringMatchResult PokemonMovesOCR::read_substring(
Logger& logger,
Language language,
const ImageViewRGB32& image,
const std::vector<OCR::TextColorRange>& text_color_ranges,
double min_text_ratio, double max_text_ratio
) const{
return match_substring_from_image_multifiltered(
&logger, language, image, text_color_ranges,
MAX_LOG10P, MAX_LOG10P_SPREAD, min_text_ratio, max_text_ratio
);
}

PokemonMovesReader::PokemonMovesReader(Language language)
: m_language(language)
{
for (size_t c = 0; c < 4; c++){
m_boxes_rearrange[c] = ImageFloatBox(0.345, 0.245 + c * 0.074, 0.250, 0.065);
}
}

void PokemonMovesReader::make_overlays(VideoOverlaySet& items) const{
for (size_t c = 0; c < 4; c++){
items.add(COLOR_GREEN, m_boxes_rearrange[c]);
}
}

std::string PokemonMovesReader::read_move(Logger& logger, const ImageViewRGB32& screen, int8_t index) const{
ImageViewRGB32 cropped = extract_box_reference(screen, m_boxes_rearrange[index]);
const auto ocr_result = PokemonMovesOCR::instance().read_substring(
logger, m_language,
cropped, OCR::BLACK_OR_WHITE_TEXT_FILTERS()
);

std::multimap<double, OCR::StringMatchData> results;
if (!ocr_result.results.empty()){
for (const auto& result : ocr_result.results){
results.emplace(result.first, result.second);
}
}

if (results.empty()){
return "";
}

if (results.size() > 1){
throw OperationFailedException(
ErrorReport::SEND_ERROR_REPORT,
logger,
"MenuOption::read_option(): Unable to read item. Ambiguous or multiple results."
);
}

return results.begin()->second.token;
}



}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* Pokemon Moves Reader
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/

#ifndef PokemonAutomation_PokemonSV_PokemonMovesReader_H
#define PokemonAutomation_PokemonSV_PokemonMovesReader_H

#include <map>
#include <array>
#include "CommonFramework/Language.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonFramework/OCR/OCR_SmallDictionaryMatcher.h"

namespace PokemonAutomation{
class AsyncDispatcher;
class ConsoleHandle;
class BotBaseContext;
namespace NintendoSwitch{
namespace PokemonSV{

class PokemonMovesOCR : public OCR::SmallDictionaryMatcher{
static constexpr double MAX_LOG10P = -1.30;
static constexpr double MAX_LOG10P_SPREAD = 0.50;

public:
PokemonMovesOCR();

static PokemonMovesOCR& instance();

OCR::StringMatchResult read_substring(
Logger& logger,
Language language,
const ImageViewRGB32& image,
const std::vector<OCR::TextColorRange>& text_color_ranges,
double min_text_ratio = 0.01, double max_text_ratio = 0.50
) const;


};

class PokemonMovesReader{

public:
PokemonMovesReader(Language language);

void make_overlays(VideoOverlaySet& items) const;

std::string read_move(Logger& logger, const ImageViewRGB32& screen, int8_t index) const;

private:
Language m_language;
std::array<ImageFloatBox, 4> m_boxes_rearrange;
};



}
}
}
#endif

0 comments on commit f5185ea

Please sign in to comment.