Skip to content

Commit 55b131d

Browse files
authored
improve reliability of Item printer material detector (#662)
* improve reliability of Item printer material detector. use string OCR directly instead of number OCR, then string OCR. * minor refactor to ItemPrinterMaterialDetector::detect_material_quantity()
1 parent d5a6a40 commit 55b131d

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,28 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
254254
ProControllerContext context(scope, console.pro_controller());
255255
VideoOverlaySet overlays(overlay);
256256

257+
#if 1
258+
ItemPrinterMaterialDetector detector(COLOR_RED, LANGUAGE);
259+
// detector.make_overlays(overlays);
260+
// cout << (int)detector.find_happiny_dust_row_index(console, context) << endl;
261+
262+
ImageRGB32 image(IMAGE_PATH);
263+
// auto image = feed.snapshot();
257264

265+
for (int i = 0; i < 10; i++){
266+
cout << detector.detect_material_name(console, image, context, (int8_t)i) << endl;
267+
cout << detector.detect_material_quantity(console, image, context, (int8_t)i) << endl;
268+
}
269+
#endif
270+
271+
#if 0
258272
auto screenshot = feed.snapshot();
259273

260274

261275
DateReader reader(console);
262276
reader.make_overlays(overlays);
263277
reader.read_date(logger, screenshot);
264-
278+
#endif
265279

266280
#if 0
267281
BinarySliderDetector detector(COLOR_BLUE, {0.836431, 0.097521, 0.069703, 0.796694});

SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -159,43 +159,51 @@ int16_t ItemPrinterMaterialDetector::read_number(
159159
int8_t ItemPrinterMaterialDetector::find_happiny_dust_row_index(
160160
VideoStream& stream, ProControllerContext& context
161161
) const{
162-
int8_t value_68_row_index;
162+
int8_t happiny_dust_row_index = -1;
163163
for (size_t c = 0; c < 30; c++){
164164
context.wait_for_all_requests();
165-
std::vector<int8_t> value_68_row_index_list = find_material_value_row_index(stream, context, 68);
166-
for (size_t i = 0; i < value_68_row_index_list.size(); i++){
167-
value_68_row_index = value_68_row_index_list[i];
168-
if (detect_material_name(stream, context, value_68_row_index) == "happiny-dust"){
169-
// found screen and row number with Happiny dust.
170-
// std::cout << "Happiny dust found. Row number: " << std::to_string(value_68_row_index) << std::endl;
171-
return value_68_row_index;
172-
}
173-
}
165+
VideoSnapshot snapshot = stream.video().snapshot();
166+
int8_t total_rows = 10;
174167

168+
SpinLock lock;
169+
GlobalThreadPools::normal_inference().run_in_parallel(
170+
[&](size_t index){
171+
std::string material_name = detect_material_name(stream, snapshot, context, (int8_t)index);
172+
if ("happiny-dust" == material_name){
173+
WriteSpinLock lg(lock);
174+
happiny_dust_row_index = (int8_t)index;
175+
}
176+
},
177+
0, total_rows, 1
178+
);
179+
180+
if (happiny_dust_row_index != -1){
181+
// found screen and row number with Happiny dust.
182+
// std::cout << "Happiny dust found. Row number: " << std::to_string(value_68_row_index) << std::endl;
183+
return happiny_dust_row_index;
184+
}
185+
175186
// keep searching for Happiny dust
176187
pbf_press_dpad(context, DPAD_RIGHT, 20, 30);
177188
}
178189

179190
OperationFailedException::fire(
180191
ErrorReport::SEND_ERROR_REPORT,
181-
"Failed to find Happiny dust after 10 tries.",
192+
"Failed to find Happiny dust after multiple attempts.",
182193
stream
183194
);
184195

185196
}
186197

187-
// detects the material name at the given row_index
188-
// MaterialNameReader has a very limited dictionary,
189-
// so it can only reliably read the material names with 68% value
190-
// (i.e. Ditto Goo, Happiny Dust, Magby Hair, Beldum Claw)
191198
std::string ItemPrinterMaterialDetector::detect_material_name(
192199
VideoStream& stream,
200+
const ImageViewRGB32& screen,
193201
ProControllerContext& context,
194202
int8_t row_index
195203
) const{
196-
VideoSnapshot snapshot = stream.video().snapshot();
204+
197205
ImageFloatBox material_name_box = m_box_mat_name[row_index];
198-
ImageViewRGB32 material_name_image = extract_box_reference(snapshot, material_name_box);
206+
ImageViewRGB32 material_name_image = extract_box_reference(screen, material_name_box);
199207
const auto ocr_result = MaterialNameReader::instance().read_substring(
200208
stream.logger(), m_language,
201209
material_name_image, OCR::BLACK_OR_WHITE_TEXT_FILTERS()
@@ -250,15 +258,13 @@ std::vector<int8_t> ItemPrinterMaterialDetector::find_material_value_row_index(
250258

251259
}
252260

253-
// detect the quantity of material at the given row number
254261
int16_t ItemPrinterMaterialDetector::detect_material_quantity(
255262
VideoStream& stream,
263+
const ImageViewRGB32& screen,
256264
ProControllerContext& context,
257265
int8_t row_index
258266
) const{
259-
context.wait_for_all_requests();
260-
VideoSnapshot snapshot = stream.video().snapshot();
261-
int16_t value = read_number(stream.logger(), snapshot, m_box_mat_quantity[row_index], row_index);
267+
int16_t value = read_number(stream.logger(), screen, m_box_mat_quantity[row_index], row_index);
262268
return value;
263269
}
264270

SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ class ItemPrinterMaterialDetector{
5858
int16_t material_value
5959
) const;
6060

61+
// detect the quantity of material at the given row number
6162
int16_t detect_material_quantity(
6263
VideoStream& stream,
64+
const ImageViewRGB32& screen,
6365
ProControllerContext& context,
6466
int8_t row_index
6567
) const;
6668

69+
// detects the material name at the given row_index, given the screen
6770
std::string detect_material_name(
6871
VideoStream& stream,
72+
const ImageViewRGB32& screen,
6973
ProControllerContext& context,
7074
int8_t row_index
7175
) const;

SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNG.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,11 @@ uint32_t ItemPrinterRNG::check_num_happiny_dust(
993993
int8_t happiny_dust_row_num = detector.find_happiny_dust_row_index(
994994
env.console, context
995995
);
996+
997+
context.wait_for_all_requests();
998+
VideoSnapshot snapshot = env.console.video().snapshot();
996999
num_happiny_dust = detector.detect_material_quantity(
997-
env.console, context,
1000+
env.console, snapshot, context,
9981001
happiny_dust_row_num
9991002
);
10001003
pbf_mash_button(context, BUTTON_B, 100);

0 commit comments

Comments
 (0)