-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathTestMap.cpp
More file actions
329 lines (274 loc) · 20.9 KB
/
TestMap.cpp
File metadata and controls
329 lines (274 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Test Map
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/Containers/AlignedVector.tpp"
#include "CommonFramework_Tests.h"
#include "Kernels_Tests.h"
#include "NintendoSwitch_Tests.h"
#include "PokemonFRLG_Tests.h"
#include "PokemonHome_Tests.h"
#include "PokemonLA_Tests.h"
#include "PokemonLZA_Tests.h"
#include "PokemonSwSh_Tests.h"
#include "PokemonSV_Tests.h"
#include "TestMap.h"
#include "TestUtils.h"
#include "CommonFramework/AudioPipeline/AudioTemplate.h"
#include <QFileInfo>
#include <iostream>
#include <algorithm>
#include <string>
using std::cout;
using std::cerr;
using std::endl;
using namespace std::placeholders; // for _1, _2, _3... with std::bind()
namespace PokemonAutomation{
// For each test object, TestFunction will do the work of skipping non-test
// files, read data from test file names or gold files and possibly more.
// To reuse the code, we design the following helper functions.
using ImageFilenameFunction = std::function<int(const ImageViewRGB32& image, const std::string& filename_base)>;
using ImageBoolDetectorFunction = std::function<int(const ImageViewRGB32& image, bool target)>;
using ImageFloatDetectorFunction = std::function<int(const ImageViewRGB32& image, float target, float threshold)>;
using ImageIntDetectorFunction = std::function<int(const ImageViewRGB32& image, int target)>;
using ImageWordsDetectorFunction = std::function<int(const ImageViewRGB32& image, const std::vector<std::string>& words)>;
using ImageVoidDetectorFunction = std::function<int(const ImageViewRGB32& image)>;
using SoundBoolDetectorFunction = std::function<int(const std::vector<AudioSpectrum>& spectrums, bool target)>;
// Basic check on whether an image can be loaded.
// Also strip the image format suffix (.png and so on)
// Helper for testing code that reads an image and uses filename to get the target outcome for the code.
// test_func: reads an image and the image filename base and returns an int code.
int image_filename_detector_helper(ImageFilenameFunction test_func, const std::string& test_path){
ImageRGB32 image;
std::string basename;
try{
image = ImageRGB32(test_path);
QFileInfo file_info(QString::fromStdString(test_path));
std::string full_name = file_info.fileName().toStdString();
// Find the basename (full name without file extension)
// QFileInfo has a function basename() to get basename, but we shouldn't use it
// because for a filename like Free_0.421_0.024_0.163_0.174.png where floating point values are
// put into the filename, QFileInfo::basename() will return a basename of "Free_0". It finds the
// first "." as the separation for extension, not the last.
size_t dot_loc = full_name.find_last_of(".");
basename = full_name.substr(0, dot_loc);
cout << "Parse file path: file name: " << full_name << ", base name: " << basename << endl;
}catch (FileException&){
cout << "Skip " << test_path << " as it cannot be read as image" << endl;
return -1;
}
return test_func(image, basename);
}
// Helper for testing detector code that reads an image and returns true or false.
// The target test result (whether this test file should be detected as true or false)
// is stored as part of the filename. For example, IngoBattleDayTime_True.png.
int image_bool_detector_helper(ImageBoolDetectorFunction test_func, const std::string& test_path){
auto parse_filename_and_run_test = [&](const ImageViewRGB32& image, const std::string& filename_base){
const auto name_base = QString::fromStdString(filename_base);
bool target_bool = false;
if (name_base.endsWith("_True")){
target_bool = true;
}else if (name_base.endsWith("_False")){
target_bool = false;
}else{
cerr << "Error: image test file " << test_path << " has incorrect target detection result (_True/_False) set in the filename." << endl;
return 1;
}
return test_func(image, target_bool);
};
return image_filename_detector_helper(parse_filename_and_run_test, test_path);
}
// Helper for testing detector code that reads an image and returns some custom data that can be described
// by words included in the test filename.
// The helper will split the filename by "_" into words and send it in the same order to the test function.
int image_words_detector_helper(ImageWordsDetectorFunction test_func, const std::string& test_path){
auto parse_filename_and_run_test = [&](const ImageViewRGB32& image, const std::string& filename_base){
return test_func(image, parse_words(filename_base));
};
return image_filename_detector_helper(parse_filename_and_run_test, test_path);
}
// Helper for testing detector code that reads an image and returns a non-negative float that can be described
// in the filename for example <name_base>_0.4.png.
int image_non_negative_float_detector_helper(ImageFloatDetectorFunction test_func, const std::string& test_path){
auto parse_filename_and_run_test = [&](const ImageViewRGB32& image, const std::vector<std::string>& words) -> int{
if (words.size() < 2){
cerr << "Error: image test file " << test_path << " does not have two non-negative floats (e.g image_0.4_0.001.png) set in the filename." << endl;
return 1;
}
float target_number = 0.0f, threshold = 0.0f;
if (parse_float(words[words.size()-2], target_number) == false || parse_float(words[words.size()-1], threshold) == false){
cerr << "Error: image test file " << test_path << " does not have two non-negative floats (e.g image_0.4_0.001.png) set in the filename." << endl;
return 1;
}
return test_func(image, target_number, threshold);
};
return image_words_detector_helper(parse_filename_and_run_test, test_path);
}
int image_int_detector_helper(ImageIntDetectorFunction test_func, const std::string& test_path){
auto parse_filename_and_run_test = [&](const ImageViewRGB32& image, const std::vector<std::string>& words) -> int{
if (words.size() == 0){
cerr << "Error: image test file " << test_path << " does not have an int (e.g image_5.png) set in the filename." << endl;
return 1;
}
int target_number = 0;
if (parse_int(words[words.size()-1], target_number) == false){
cerr << "Error: image test file " << test_path << " does not have an int (e.g image_5.png) set in the filename." << endl;
return 1;
}
return test_func(image, target_number);
};
return image_words_detector_helper(parse_filename_and_run_test, test_path);
}
// Helper for testing sdetector code that reads an image and returns nothing.
// This is used for developing visual inference code where the developer writes custom
// debugging output. So no need to get target values from the test framework.
int image_void_detector_helper(ImageVoidDetectorFunction test_func, const std::string& test_path){
auto run_test = [&](const ImageViewRGB32& image, const std::string&) -> int{
return test_func(image);
};
return image_filename_detector_helper(run_test, test_path);
}
// Basic check on whether an image can be loaded.
// Also strip the image format suffix (.png and so on)
int sound_bool_detector_helper(SoundBoolDetectorFunction test_func, const std::string& test_path){
QFileInfo file_info(QString::fromStdString(test_path));
std::string filename = file_info.fileName().toStdString();
// Search for the target test result from test filename.
const size_t target_pos = filename.rfind('.');
if (target_pos == std::string::npos){
cerr << "Error: image test file " << test_path << " has no \".\" in the filename." << endl;
return 1;
}
// cout << "Test file: " << test_path << endl;
const auto filename_base = filename.substr(0, target_pos);
const auto name_base = QString::fromStdString(filename_base);
bool target_bool = false;
if (name_base.endsWith("_True")){
target_bool = true;
}else if (name_base.endsWith("_False")){
target_bool = false;
}else{
cerr << "Error: audio test file " << test_path << " has incorrect target detection result (_True/_False) set in the filename." << endl;
return 1;
}
// XXX for now we assume the audio in the command line test is always 48000.
// in future we can read sample rate from filename
size_t sample_rate = 48000;
AudioTemplate audio_stream = loadAudioTemplate(test_path, sample_rate);
std::vector<AudioSpectrum> spectrums;
for (size_t i = 0; i < audio_stream.numWindows(); i++){
// AudioSpectrum(size_t s, size_t rate, std::shared_ptr<const AlignedVector<float>> m);
AlignedVector<float> freq_mag(audio_stream.numFrequencies());
memcpy(freq_mag.data(), audio_stream.getWindow(i), sizeof(float) * audio_stream.numFrequencies());
spectrums.emplace_back(0, sample_rate, std::make_shared<const AlignedVector<float>>(std::move(freq_mag)));
}
// Need to reverse spectrums, because audio detector interface accepts sepctrum vector in the order of
// from newest (largest timestamp) to oldest (smallest timestamp) in the vector.
std::reverse(spectrums.begin(), spectrums.end());
return test_func(spectrums, target_bool);
}
const std::map<std::string, TestFunction> TEST_MAP = {
{"Kernels_ImageScaleBrightness", std::bind(image_void_detector_helper, test_kernels_ImageScaleBrightness, _1)},
{"Kernels_BinaryMatrix", std::bind(image_void_detector_helper, test_kernels_BinaryMatrix, _1)},
{"Kernels_FilterRGB32Range", std::bind(image_void_detector_helper, test_kernels_FilterRGB32Range, _1)},
{"Kernels_FilterRGB32Euclidean", std::bind(image_void_detector_helper, test_kernels_FilterRGB32Euclidean, _1)},
{"Kernels_ToBlackWhiteRGB32Range", std::bind(image_void_detector_helper, test_kernels_ToBlackWhiteRGB32Range, _1)},
{"Kernels_FilterByMask", std::bind(image_void_detector_helper, test_kernels_FilterByMask, _1)},
{"Kernels_CompressRGB32ToBinaryEuclidean", std::bind(image_void_detector_helper, test_kernels_CompressRGB32ToBinaryEuclidean, _1)},
{"Kernels_Waterfill", std::bind(image_void_detector_helper, test_kernels_Waterfill, _1)},
{"CommonFramework_BlackBorderDetector", std::bind(image_bool_detector_helper, test_CommonFramework_BlackBorderDetector, _1)},
{"NintendoSwitch_CheckOnlineDetector", std::bind(image_bool_detector_helper, test_NintendoSwitch_CheckOnlineDetector, _1)},
{"NintendoSwitch_FailedToConnectDetector", std::bind(image_bool_detector_helper, test_NintendoSwitch_FailedToConnectDetector, _1)},
{"NintendoSwitch_UpdatePopupDetector", std::bind(image_bool_detector_helper, test_NintendoSwitch_UpdatePopupDetector, _1)},
{"PokemonSwSh_YCommMenuDetector", std::bind(image_bool_detector_helper, test_pokemonSwSh_YCommMenuDetector, _1)},
{"PokemonSwSh_MaxLair_BattleMenuDetector", std::bind(image_bool_detector_helper, test_pokemonSwSh_MaxLair_BattleMenuDetector, _1)},
{"PokemonSwSh_DialogTriangleDetector", std::bind(image_bool_detector_helper, test_pokemonSwSh_DialogTriangleDetector, _1)},
{"PokemonSwSh_RetrieveEggArrowFinder", std::bind(image_bool_detector_helper, test_pokemonSwSh_RetrieveEggArrowFinder, _1)},
{"PokemonSwSh_YCommIconDetector", std::bind(image_bool_detector_helper, test_pokemonSwSh_YCommIconDetector, _1)},
{"PokemonSwSh_RotomPhoneMenuArrowFinder", std::bind(image_int_detector_helper, test_pokemonSwSh_RotomPhoneMenuArrowFinder, _1)},
{"PokemonSwSh_StoragePokemonMenuArrowFinder", std::bind(image_bool_detector_helper, test_pokemonSwSh_StoragePokemonMenuArrowFinder, _1)},
{"PokemonSwSh_CheckNurseryArrowFinder", std::bind(image_bool_detector_helper, test_pokemonSwSh_CheckNurseryArrowFinder, _1)},
{"PokemonSwSh_BlackDialogBoxDetector", std::bind(image_bool_detector_helper, test_pokemonSwSh_BlackDialogBoxDetector, _1)},
{"PokemonSwSh_BoxShinySymbolDetector", std::bind(image_bool_detector_helper, test_pokemonSwSh_BoxShinySymbolDetector, _1)},
{"PokemonSwSh_BoxGenderDetector", std::bind(image_int_detector_helper, test_pokemonSwSh_BoxGenderDetector, _1)},
{"PokemonSwSh_SelectionArrowFinder", std::bind(image_int_detector_helper, test_pokemonSwSh_SelectionArrowFinder, _1)},
{"PokemonLA_BattleMenuDetector", std::bind(image_bool_detector_helper, test_pokemonLA_BattleMenuDetector, _1)},
{"PokemonLA_BattlePokemonSwitchDetector", std::bind(image_bool_detector_helper, test_pokemonLA_BattlePokemonSwitchDetector, _1)},
{"PokemonLA_TransparentDialogueDetector", std::bind(image_bool_detector_helper, test_pokemonLA_TransparentDialogueDetector, _1)},
{"PokemonLA_EventDialogDetector", std::bind(image_bool_detector_helper, test_pokemonLA_EventDialogDetector, _1)},
{"PokemonLA_DialogueYellowArrowDetector", std::bind(image_bool_detector_helper, test_pokemonLA_DialogueYellowArrowDetector, _1)},
{"PokemonLA_BlackOutDetector", std::bind(image_bool_detector_helper, test_pokemonLA_BlackOutDetector, _1)},
{"PokemonLA_BattleStartDetector", std::bind(image_bool_detector_helper, test_pokemonLA_BattleStartDetector, _1)},
{"PokemonLA_BerryTreeDetector", std::bind(image_void_detector_helper, test_pokemonLA_BerryTreeDetector, _1)},
{"PokemonLA_MMOQuestionMarkDetector", std::bind(image_words_detector_helper, test_pokemonLA_MMOQuestionMarkDetector, _1)},
{"PokemonLA_StatusInfoScreenDetector", std::bind(image_words_detector_helper, test_pokemonLA_StatusInfoScreenDetector, _1)},
{"PokemonLA_WildPokemonFocusDetector", std::bind(image_words_detector_helper, test_pokemonLA_WildPokemonFocusDetector, _1)},
{"PokemonLA_BattleSpriteWatcher", std::bind(image_words_detector_helper, test_pokemonLA_BattleSpriteWatcher, _1)},
{"PokemonLA_SaveScreenDetector", std::bind(image_words_detector_helper, test_pokemonLA_SaveScreenDetector, _1)},
{"PokemonLA_MapMarkerLocator", std::bind(image_non_negative_float_detector_helper, test_pokemonLA_MapMarkerLocator, _1)},
{"PokemonLA_MapZoomLevelReader", std::bind(image_int_detector_helper, test_pokemonLA_MapZoomLevelReader, _1)},
{"PokemonLA_BattleSpriteArrowDetector", std::bind(image_int_detector_helper, test_pokemonLA_BattleSpriteArrowDetector, _1)},
{"PokemonLA_MapMissionTabReader", std::bind(image_bool_detector_helper, test_pokemonLA_MapMissionTabReader, _1)},
{"PokemonLA_ShinySoundDetector", std::bind(sound_bool_detector_helper, test_pokemonLA_shinySoundDetector, _1)},
{"PokemonLA_MMOSpriteMatcher", test_pokemonLA_MMOSpriteMatcher},
{"PokemonLA_MapWeatherAndTimeReader", std::bind(image_words_detector_helper, test_pokemonLA_MapWeatherAndTimeReader, _1)},
{"PokemonLA_FlagTrackerPerformance", std::bind(image_int_detector_helper, test_pokemonLA_FlagTracker_performance, _1)},
{"PokemonHome_BoxView", std::bind(image_words_detector_helper, test_pokemonHome_BoxView, _1)},
{"PokemonHome_SummaryScreen", std::bind(image_words_detector_helper, test_pokemonHome_SummaryScreen, _1)},
{"PokemonSV_MapDetector", std::bind(image_words_detector_helper, test_pokemonSV_MapDetector, _1)},
{"PokemonSV_PicnicDetector", std::bind(image_bool_detector_helper, test_pokemonSV_PicnicDetector, _1)},
{"PokemonSV_TeraCardFinder", std::bind(image_bool_detector_helper, test_pokemonSV_TeraCardFinder, _1)},
{"PokemonSV_TerastallizingDetector", std::bind(image_bool_detector_helper, test_pokemonSV_TerastallizingDetector, _1)},
{"PokemonSV_TeraSilhouetteReader", std::bind(image_words_detector_helper, test_pokemonSV_TeraSilhouetteReader, _1)},
{"PokemonSV_TeraTypeReader", std::bind(image_words_detector_helper, test_pokemonSV_TeraTypeReader, _1)},
{"PokemonSV_SandwichRecipeDetector", std::bind(image_words_detector_helper, test_pokemonSV_SandwichRecipeDetector, _1)},
{"PokemonSV_SandwichHandDetector", std::bind(image_words_detector_helper, test_pokemonSV_SandwichHandDetector, _1)},
{"PokemonSV_BoxPokemonInfoDetector", std::bind(image_words_detector_helper, test_pokemonSV_BoxPokemonInfoDetector, _1)},
{"PokemonSV_SomethingInBoxSlotDetector", std::bind(image_bool_detector_helper, test_pokemonSV_SomethingInBoxSlotDetector, _1)},
{"PokemonSV_BoxEggDetector", std::bind(image_bool_detector_helper, test_pokemonSV_BoxEggDetector, _1)},
{"PokemonSV_BoxPartyEggDetector", std::bind(image_int_detector_helper, test_pokemonSV_BoxPartyEggDetector, _1)},
{"PokemonSV_OverworldDetector", std::bind(image_bool_detector_helper, test_pokemonSV_OverworldDetector, _1)},
{"PokemonSV_BoxBottomButtonDetector", std::bind(image_words_detector_helper, test_pokemonSV_BoxBottomButtonDetector, _1)},
{"PokemonSV_SandwichIngredientsDetector", std::bind(image_words_detector_helper, test_pokemonSV_SandwichIngredientsDetector, _1)},
{"PokemonSV_SandwichIngredientReader", test_pokemonSV_SandwichIngredientReader},
{"PokemonSV_AdvanceDialogDetector", std::bind(image_bool_detector_helper, test_pokemonSV_AdvanceDialogDetector, _1)},
{"PokemonSV_SwapMenuDetector", std::bind(image_bool_detector_helper, test_pokemonSV_SwapMenuDetector, _1)},
{"PokemonSV_DialogBoxDetector", std::bind(image_bool_detector_helper, test_pokemonSV_DialogBoxDetector, _1)},
{"PokemonSV_FastTravelDetector", std::bind(image_bool_detector_helper, test_pokemonSV_FastTravelDetector, _1)},
{"PokemonSV_MapPokeCenterIconDetector", std::bind(image_int_detector_helper, test_pokemonSV_MapPokeCenterIconDetector, _1)},
{"PokemonSV_ESPPressedEmotionDetector", std::bind(image_bool_detector_helper, test_pokemonSV_ESPPressedEmotionDetector, _1)},
{"PokemonSV_MapFlyMenuDetector", std::bind(image_bool_detector_helper, test_pokemonSV_MapFlyMenuDetector, _1)},
{"PokemonSV_SandwichPlateDetector", std::bind(image_words_detector_helper, test_pokemonSV_SandwichPlateDetector, _1)},
{"PokemonSV_RecentlyBattledDetector", std::bind(image_bool_detector_helper, test_pokemonSV_RecentlyBattledDetector, _1)},
{"PokemonLZA_FlatWhiteDialogDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_FlatWhiteDialogDetector, _1)},
{"PokemonLZA_BlueDialogDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_BlueDialogDetector, _1)},
{"PokemonLZA_TransparentBattleDialogDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_TransparentBattleDialogDetector, _1)},
{"PokemonLZA_ButtonDetector", std::bind(image_words_detector_helper, test_pokemonLZA_ButtonDetector, _1)},
{"PokemonLZA_MainMenuDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_MainMenuDetector, _1)},
{"PokemonLZA_AlertEyeDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_AlertEyeDetector, _1)},
{"PokemonLZA_BoxCellInfoDetector", std::bind(image_words_detector_helper, test_pokemonLZA_BoxCellInfoDetector, _1)},
{"PokemonLZA_SelectionArrowDetector", std::bind(image_words_detector_helper, test_pokemonLZA_SelectionArrowDetector, _1)},
{"PokemonLZA_MapIconDetector", test_pokemonLZA_MapIconDetector},
{"PokemonLZA_OverworldPartySelectionDetector", std::bind(image_words_detector_helper, test_pokemonLZA_OverworldPartySelectionDetector, _1)},
{"PokemonLZA_DirectionArrowDetector", std::bind(image_int_detector_helper, test_pokemonLZA_DirectionArrowDetector, _1)},
{"PokemonLZA_MapDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_MapDetector, _1)},
{"PokemonLZA_HyperspaceCalorieDetector", std::bind(image_int_detector_helper, test_pokemonLZA_HyperspaceCalorieDetector, _1)},
{"PokemonLZA_FlavorPowerScreenDetector", test_pokemonLZA_FlavorPowerScreenDetector},
{"PokemonLZA_DonutBerriesReader", test_pokemonLZA_DonutBerriesReader},
{"PokemonFRLG_AdvanceWhiteDialogDetector", std::bind(image_bool_detector_helper, test_pokemonFRLG_AdvanceWhiteDialogDetector, _1)},
{"PokemonFRLG_ShinySymbolDetector", std::bind(image_bool_detector_helper, test_pokemonFRLG_ShinySymbolDetector, _1)},
{"PokemonFRLG_SelectionDialogDetector", std::bind(image_bool_detector_helper, test_pokemonFRLG_SelectionDialogDetector, _1)},
{"PokemonFRLG_AdvanceBattleDialogDetector", std::bind(image_bool_detector_helper, test_pokemonFRLG_AdvanceBattleDialogDetector, _1)},
{"PokemonFRLG_BattleMenuDetector", std::bind(image_bool_detector_helper, test_pokemonFRLG_BattleMenuDetector, _1)},
{"PokemonFRLG_PrizeSelectDetector", std::bind(image_bool_detector_helper, test_pokemonFRLG_PrizeSelectDetector, _1)},
};
TestFunction find_test_function(const std::string& test_space, const std::string& test_name){
const auto it = TEST_MAP.find(test_space + "_" + test_name);
if (it == TEST_MAP.end()){
cerr << "Warning: no test object named " << test_space << "_" << test_name << " found in the code." << endl;
return nullptr;
}
return it->second;
}
}