Skip to content

Commit 2231951

Browse files
committed
Improve type detection. Improve reliability of exit sequence. Show type labels on path.
1 parent 1d4af37 commit 2231951

File tree

5 files changed

+76
-28
lines changed

5 files changed

+76
-28
lines changed

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_NumberCodeEntry.h"
119119
#include "PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMenuDetector.h"
120120
#include "PokemonSV/Inference/Picnics/PokemonSV_SandwichHandDetector.h"
121-
#include "PokemonSV/Inference/Picnics/PokemonSV_SandwichPlateDetector.h"
122121
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_PokemonSwapMenu.h"
123122
#include "CommonTools/Images/ImageFilter.h"
124123
#include "NintendoSwitch/Options/NintendoSwitch_ModelType.h"
@@ -138,6 +137,7 @@
138137
#include "PokemonLA/Inference/Map/PokemonLA_SelectedRegionDetector.h"
139138
#include "PokemonHome/Inference/PokemonHome_BallReader.h"
140139
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_PathSide.h"
140+
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_PathMap.h"
141141

142142
#include <QPixmap>
143143
#include <QVideoFrame>
@@ -256,10 +256,29 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
256256
VideoOverlaySet overlays(overlay);
257257

258258

259+
#if 0
260+
auto screenshot = feed.snapshot();
261+
262+
OcrFailureWatchdog watchdog(logger);
263+
MaxLairInternal::BattleMenuReader reader(overlay, Language::ChineseTraditional, watchdog);
264+
reader.read_opponent_in_summary(logger, screenshot);
265+
#endif
266+
259267
#if 1
260-
ImageRGB32 image(IMAGE_PATH);
261-
SandwichPlateDetector middle_plate_detector(logger, COLOR_RED, LANGUAGE, SandwichPlateDetector::Side::MIDDLE);
262-
cout << middle_plate_detector.detect_filling_name(image) << endl;
268+
auto screenshot = feed.snapshot();
269+
270+
std::deque<OverlayBoxScope> hits;
271+
Pokemon::PokemonType type[4];
272+
273+
bool ret = MaxLairInternal::read_type_array(
274+
console,
275+
screenshot,
276+
ImageFloatBox{0.150, 0.020, 0.800, 0.780},
277+
hits,
278+
4, type, nullptr
279+
);
280+
281+
cout << "ret = " << ret << endl;
263282
#endif
264283

265284
#if 0
@@ -652,13 +671,13 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
652671
ItemPrinterMaterialDetector detector(COLOR_RED, Language::English);
653672

654673
std::vector<ImageFloatBox> boxes = {
655-
// {0.485,0.176758,0.037,0.05}, {0.485,0.250977,0.037,0.05}, {0.485,0.325196,0.037,0.05}, {0.485,0.399415,0.037,0.05}, {0.485,0.473634,0.037,0.05}, {0.485,0.547853,0.037,0.05}, {0.485,0.622072,0.037,0.05}, {0.485,0.696291,0.037,0.05}, {0.485,0.77051,0.037,0.05}, {0.485,0.844729,0.037,0.05},
656-
{0.39,0.176758,0.025,0.05}, {0.39,0.250977,0.025,0.05}, {0.39,0.325196,0.025,0.05}, {0.39,0.399415,0.025,0.05}, {0.39,0.473634,0.025,0.05}, {0.39,0.547853,0.025,0.05}, {0.39,0.622072,0.025,0.05}, {0.39,0.696291,0.025,0.05}, {0.39,0.77051,0.025,0.05}, {0.39,0.844729,0.025,0.05},
674+
// {0.485,0.176758,0.037,0.05}, {0.485,0.250977,0.037,0.05}, {0.485,0.325196,0.037,0.05}, {0.485,0.399415,0.037,0.05}, {0.485,0.473634,0.037,0.05}, {0.485,0.547853,0.037,0.05}, {0.485,0.622072,0.037,0.05}, {0.485,0.696291,0.037,0.05}, {0.485,0.77051,0.037,0.05}, {0.485,0.844729,0.037,0.05},
675+
{0.39,0.176758,0.025,0.05}, {0.39,0.250977,0.025,0.05}, {0.39,0.325196,0.025,0.05}, {0.39,0.399415,0.025,0.05}, {0.39,0.473634,0.025,0.05}, {0.39,0.547853,0.025,0.05}, {0.39,0.622072,0.025,0.05}, {0.39,0.696291,0.025,0.05}, {0.39,0.77051,0.025,0.05}, {0.39,0.844729,0.025,0.05},
657676
};
658677
for (ImageFloatBox box : boxes){
659678
detector.read_number(console.logger(), env.inference_dispatcher(), image, box);
660679
}
661-
680+
662681
#endif
663682
#endif
664683

@@ -919,7 +938,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
919938

920939
VideoSnapshot image = feed.snapshot();
921940
DirectionDetector detector;
922-
941+
923942
// ImageRGB32 image("MaterialFarmer-1.png");
924943
// ImageRGB32 image("dark-capture-card_1.png");
925944
// DirectionDetector detector(COLOR_BLUE, ImageFloatBox(0,0,1,1));
@@ -937,7 +956,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
937956
// cout << (int)detector.detect_material_quantity(env.inference_dispatcher(), console, context, 2) << endl;
938957

939958
// test OCR for number 1 -> 999. for black text on light background.
940-
// increasing quantity of materials to sell.
959+
// increasing quantity of materials to sell.
941960
for (int i = 1; i < 1000; i++){
942961
context.wait_for_all_requests();
943962
if (i != (int)detector.detect_material_quantity(env.inference_dispatcher(), console, context, 2)){

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_TypeSymbolFinder.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ size_t distance_sqr(const ImagePixelBox& a, const ImagePixelBox& b){
4949
return dist_x*dist_x + dist_y*dist_y;
5050
}
5151

52+
//bool print = false;
5253

5354
std::pair<double, PokemonType> match_type_symbol(const ImageViewRGB32& image){
5455
size_t width = image.width();
@@ -64,6 +65,9 @@ std::pair<double, PokemonType> match_type_symbol(const ImageViewRGB32& image){
6465
}
6566
ImageStats stats = image_stats(image);
6667
if (stats.stddev.sum() < 50){
68+
// if (print){
69+
// cout << "stats.stddev.sum() = " << stats.stddev.sum() << endl;
70+
// }
6771
return {1.0, PokemonType::NONE};
6872
}
6973

@@ -73,7 +77,7 @@ std::pair<double, PokemonType> match_type_symbol(const ImageViewRGB32& image){
7377
// image.save("test-" + std::to_string(threshold) + "-" + std::to_string(c++) + ".png");
7478

7579
// std::map<double, PokemonType> rank;
76-
double best_score = 0.4;
80+
double best_score = 0.45;
7781
PokemonType best_type = PokemonType::NONE;
7882
for (const auto& item : all_type_sprites()){
7983
// if (threshold != 700 || id != 55){
@@ -83,10 +87,12 @@ std::pair<double, PokemonType> match_type_symbol(const ImageViewRGB32& image){
8387
double expected_aspect_ratio = item.second.aspect_ratio();
8488
double ratio = aspect_ratio / expected_aspect_ratio;
8589
#if 0
86-
cout << item.second.slug()
87-
<< " : expected = " << expected_aspect_ratio
88-
<< ", actual = " << aspect_ratio
89-
<< ", ratio = " << ratio << endl;
90+
if (print){
91+
cout << item.second.slug()
92+
<< " : expected = " << expected_aspect_ratio
93+
<< ", actual = " << aspect_ratio
94+
<< ", ratio = " << ratio << endl;
95+
}
9096
#endif
9197
if (std::abs(ratio - 1) > 0.2){
9298
continue;
@@ -95,7 +101,9 @@ std::pair<double, PokemonType> match_type_symbol(const ImageViewRGB32& image){
95101
double rmsd_alpha = item.second.matcher().diff(image);
96102

97103
// item.second.matcher().m_image.save("sprite.png");
98-
// cout << item.second.slug() << ": " << rmsd_alpha << endl;
104+
// if (print){
105+
// cout << item.second.slug() << ": " << rmsd_alpha << endl;
106+
// }
99107

100108
#if 0
101109
// Handicap fairy due to white and pink being too similar in color and
@@ -135,7 +143,7 @@ void find_type_symbol_candidates(
135143
(size_t)(20. * original_screen.total_pixels() / (1920*1080))
136144
);
137145

138-
// static int index = 0;
146+
static int index = 0;
139147

140148
std::map<size_t, WaterfillObject> objmap;
141149
for (size_t c = 0; c < objects.size(); c++){
@@ -144,9 +152,9 @@ void find_type_symbol_candidates(
144152
}
145153
objmap[c] = objects[c];
146154

147-
// image.copy(
148-
// objects[c].min_x, objects[c].min_y, objects[c].width(), objects[c].height()
149-
// ).save("test-" + std::to_string(index++) + ".png");
155+
#if 0
156+
extract_box_reference(image, ImagePixelBox(objects[c])).save("test-" + std::to_string(index++) + ".png");
157+
#endif
150158
}
151159

152160
// cout << "begin = " << objmap.size() << endl;
@@ -183,7 +191,12 @@ void find_type_symbol_candidates(
183191
// Identify objects.
184192
for (const auto& item : objmap){
185193
ImageViewRGB32 img = extract_box_reference(image, item.second);
194+
195+
// print = index == 137;
196+
// img.save("test-" + std::to_string(index++) + ".png");
197+
186198
std::pair<double, PokemonType> result = match_type_symbol(img);
199+
// cout << "result = " << POKEMON_TYPE_SLUGS().get_string(result.second) << ": " << result.first << endl;
187200
if (result.second != PokemonType::NONE){
188201
const WaterfillObject& obj = item.second;
189202
candidates.emplace(
@@ -211,6 +224,11 @@ std::multimap<double, std::pair<PokemonType, ImagePixelBox>> find_type_symbols(
211224
std::vector<PackedBinaryMatrix> matrices = compress_rgb32_to_binary_range(
212225
image,
213226
{
227+
{0xff808060, 0xffffffff},
228+
{0xffa0a060, 0xffffffff},
229+
{0xff606060, 0xffffffff},
230+
{0xff707070, 0xffffffff},
231+
{0xff808080, 0xffffffff},
214232
{0xff909090, 0xffffffff},
215233
{0xffa0a0a0, 0xffffffff},
216234
{0xffb0b0b0, 0xffffffff},
@@ -224,9 +242,11 @@ std::multimap<double, std::pair<PokemonType, ImagePixelBox>> find_type_symbols(
224242
}
225243
}
226244

245+
// cout << "-------------" << endl;
246+
227247
std::multimap<double, std::pair<PokemonType, ImagePixelBox>> filtered;
228248
for (const auto& candidate : candidates){
229-
// cout << get_type_slug(candidate.second.first) << ": " << candidate.first << endl;
249+
// cout << POKEMON_TYPE_SLUGS().get_string(candidate.second.first) << ": " << candidate.first << endl;
230250
// hits.emplace_back(overlay, translate_to_parent(screen, box, candidate.second.second.box), COLOR_GREEN);
231251

232252
bool is_dupe = false;
@@ -242,14 +262,15 @@ std::multimap<double, std::pair<PokemonType, ImagePixelBox>> find_type_symbols(
242262
}
243263

244264
#if 0
265+
static int c = 0;
245266
for (const auto& item : filtered){
246267
// cout << get_type_slug(item.second.first) << ": " << item.first << " - [" << item.second.second.center_x() << "," << item.second.second.center_y() << "]" << endl;
247268
const ImagePixelBox& box = item.second.second;
248269
ImageViewRGB32 img = image.sub_image(
249270
box.min_x, box.min_y,
250271
box.width(), box.height()
251272
);
252-
// img.save("test-" + std::to_string(c++) + ".png");
273+
img.save("test-" + std::to_string(c++) + ".png");
253274
}
254275
#endif
255276

SerialPrograms/Source/PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_BattleMenu.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,14 @@ std::set<std::string> BattleMenuReader::read_opponent_in_summary(Logger& logger,
333333
}
334334
}
335335

336-
// for (const auto& slug : allowed_slugs){
337-
// cout << "allowed_slugs = " << slug << endl;
338-
// }
339-
// for (const auto& slug : name_slugs){
340-
// cout << "name_slugs = " << slug << endl;
341-
// }
336+
#if 0
337+
for (const auto& slug : allowed_slugs){
338+
cout << "allowed_slugs = " << slug << endl;
339+
}
340+
for (const auto& slug : name_slugs){
341+
cout << "name_slugs = " << slug << endl;
342+
}
343+
#endif
342344

343345
if (common_slugs.size() == 1){
344346
logger.log("Disambiguation succeeded: " + *common_slugs.begin(), COLOR_BLUE);

SerialPrograms/Source/PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_PathMap.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ bool read_type_array(
5555
hits.clear();
5656
size_t c = 0;
5757
for (const auto& item : candidates){
58-
hits.emplace_back(stream.overlay(), translate_to_parent(screen, box, item.second.second), COLOR_GREEN);
58+
hits.emplace_back(
59+
stream.overlay(),
60+
COLOR_GREEN,
61+
translate_to_parent(screen, box, item.second.second),
62+
POKEMON_TYPE_SLUGS().get_string(item.second.first)
63+
);
5964
sorted.emplace(item.second.second.min_x, &item.second);
6065
c++;
6166
if (c >= count){

SerialPrograms/Source/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_CaughtScreen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ StateMachineAction run_caught_screen(
223223
synchronize_caught_screen(console_index, console, context, state_tracker);
224224
pbf_press_dpad(context, DPAD_DOWN, 10, 50);
225225
pbf_press_button(context, BUTTON_B, 10, TICKS_PER_SECOND);
226+
pbf_press_button(context, BUTTON_A, 10, 115);
226227
return mash_A_to_entrance(runtime, console, context, entrance);
227228
}else{
228229
console.log("Taking non-shiny boss and returning to entrance...", COLOR_BLUE);

0 commit comments

Comments
 (0)