Skip to content

Commit

Permalink
Add image dict matching debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Gin committed Sep 15, 2023
1 parent b9fd6d5 commit 9219d38
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 4 deletions.
27 changes: 27 additions & 0 deletions Common/Cpp/Color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Color
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
* A very lightweight color class to avoid pulling in <QColor>.
*
*/

#include "Color.h"
#include <sstream>
#include <iomanip>

namespace PokemonAutomation{

std::string Color::to_string() const{
std::ostringstream os;
os << "[0x" << std::internal << std::setfill('0');
os << std::hex << std::uppercase << std::setw(8) << m_argb << std::dec;
os << " A=" << std::setw(2) << int(alpha());
os << " R=" << std::setw(2) << int(red());
os << " G=" << std::setw(2) << int(green());
os << " B=" << std::setw(2) << int(blue()) << "]";
return os.str();
}

}

4 changes: 4 additions & 0 deletions Common/Cpp/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define PokemonAutomation_Color_H

#include <stdint.h>
#include <string>

namespace PokemonAutomation{

Expand Down Expand Up @@ -45,6 +46,9 @@ class Color{
uint8_t green () const { return (uint8_t)(m_argb >> 8); }
uint8_t blue () const { return (uint8_t)(m_argb >> 0); }

// Example: "[0xFFFDBD00 A=255 R=253 G=189 B=00]"
std::string to_string() const;

private:
uint32_t m_argb;
};
Expand Down
1 change: 1 addition & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ file(GLOB MAIN_SOURCES
../Common/Cpp/AbstractLogger.h
../Common/Cpp/CancellableScope.cpp
../Common/Cpp/CancellableScope.h
../Common/Cpp/Color.cpp
../Common/Cpp/Color.h
../Common/Cpp/Concurrency/AsyncDispatcher.cpp
../Common/Cpp/Concurrency/AsyncDispatcher.h
Expand Down
1 change: 1 addition & 0 deletions SerialPrograms/Scripts/add_new_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Example usage:
python3 add_new_file.py Source/PokemonSV/Inference/Map/PokemonSV_MapFlyMenuDetectorDetector.h
python3 add_new_file.py ../Common/Cpp/Color.cpp
"""

import sys
Expand Down
1 change: 1 addition & 0 deletions SerialPrograms/SerialPrograms.pro
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ SOURCES += \
../ClientSource/Libraries/MessageConverter.cpp \
../Common/CRC32.cpp \
../Common/Cpp/CancellableScope.cpp \
../Common/Cpp/Color.cpp \
../Common/Cpp/Concurrency/AsyncDispatcher.cpp \
../Common/Cpp/Concurrency/FireForgetDispatcher.cpp \
../Common/Cpp/Concurrency/ParallelTaskRunner.cpp \
Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void PreloadSettings::load(const JsonValue& json){
if (debug_obj){
debug_obj->read_boolean(DEBUG.COLOR_CHECK, "COLOR_CHECK");
debug_obj->read_boolean(DEBUG.IMAGE_TEMPLATE_MATCHING, "IMAGE_TEMPLATE_MATCHING");
debug_obj->read_boolean(DEBUG.IMAGE_DICTIONARY_MATCHING, "IMAGE_DICTIONARY_MATCHING");
}
}

Expand Down Expand Up @@ -366,6 +367,7 @@ JsonValue GlobalSettings::to_json() const{
const auto& debug_settings = PreloadSettings::instance().DEBUG;
debug_obj["COLOR_CHECK"] = debug_settings.COLOR_CHECK;
debug_obj["IMAGE_TEMPLATE_MATCHING"] = debug_settings.IMAGE_TEMPLATE_MATCHING;
debug_obj["IMAGE_DICTIONARY_MATCHING"] = debug_settings.IMAGE_DICTIONARY_MATCHING;
obj["DEBUG"] = std::move(debug_obj);

return obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ResolutionOption : public GroupOption{
struct DebugSettings{
bool COLOR_CHECK = false;
bool IMAGE_TEMPLATE_MATCHING = false;
bool IMAGE_DICTIONARY_MATCHING = false;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include <cmath>
#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/GlobalSettingsPanel.h"
#include "CommonFramework/Tools/DebugDumper.h"
#include "ImageCropper.h"
//#include "ImageDiff.h"
#include "CroppedImageDictionaryMatcher.h"

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

Expand Down Expand Up @@ -60,9 +62,17 @@ ImageMatchResult CroppedImageDictionaryMatcher::match(
return results;
}

if (PreloadSettings::debug().IMAGE_DICTIONARY_MATCHING){
std::cout << "CroppedImageDictionaryMatcher: match input image: " << std::endl;
dump_debug_image(global_logger_command_line(), "CommonFramework/CroppedImageDictionaryMatcher", "match_input", image);
}

Color background;
ImageRGB32 processed = process_image(image, background);
// cout << (uint32_t)background << endl;
if (PreloadSettings::debug().IMAGE_DICTIONARY_MATCHING){
std::cout << "CroppedImageDictionaryMatcher: process input image with background " << background.to_string() << std::endl;
dump_debug_image(global_logger_command_line(), "CommonFramework/CroppedImageDictionaryMatcher", "match_input_processed", image);
}

for (const auto& item : m_database){
#if 0
Expand All @@ -75,6 +85,16 @@ ImageMatchResult CroppedImageDictionaryMatcher::match(
results.clear_beyond_spread(alpha_spread);
}

if (PreloadSettings::debug().IMAGE_DICTIONARY_MATCHING){
std::cout << "CroppedImageDictionaryMatcher: results: " << std::endl;
size_t count = 0;
for(const auto& result : results.results){
std::cout << "alpha=" << result.first << ", " << result.second << std::endl;
const auto& image_template = m_database.find(result.second)->second.image_template();
dump_debug_image(global_logger_command_line(), "CommonFramework/CroppedImageDictionaryMatcher", "match_result_" + std::to_string(count) + "_" + result.second, image_template);
++count;
}
}
return results;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::string dump_debug_image(
){
create_debug_folder(path);
std::string full_path = debug_dump_folder_name;
full_path += "/" + path + "/" + now_to_filestring() + "-" + label + ".jpg";
full_path += "/" + path + "/" + now_to_filestring() + "-" + label + ".png";
logger.log("Saving debug image to: " + full_path, COLOR_YELLOW);
image.save(full_path);
return full_path;
Expand Down
2 changes: 1 addition & 1 deletion SerialPrograms/Source/CommonFramework/Tools/DebugDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace PokemonAutomation{
class ImageViewRGB32;
class Logger;

// Dump debug image to ./DebugDumps/`path`/<timestamp>-`label`.jpg
// Dump debug image to ./DebugDumps/`path`/<timestamp>-`label`.png
// Return image path.
std::string dump_debug_image(
Logger& logger,
Expand Down

0 comments on commit 9219d38

Please sign in to comment.