-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pokemon Moves reader * remove unused headers * update cmakelists
- Loading branch information
Showing
4 changed files
with
159 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
SerialPrograms/Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
SerialPrograms/Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |