|
8 | 8 | #include "Common/Cpp/Exceptions.h" |
9 | 9 | #include "Kernels/Waterfill/Kernels_Waterfill_Types.h" |
10 | 10 | #include "CommonFramework/GlobalSettingsPanel.h" |
| 11 | +#include "CommonFramework/Tools/GlobalThreadPools.h" |
11 | 12 | #include "CommonTools/ImageMatch/WaterfillTemplateMatcher.h" |
12 | 13 | #include "CommonTools/Images/WaterfillUtilities.h" |
| 14 | +#include "CommonTools/Images/SolidColorTest.h" |
| 15 | +#include "CommonTools/OCR/OCR_NumberReader.h" |
| 16 | +#include "CommonTools/OCR/OCR_RawOCR.h" |
13 | 17 | #include "PokemonPokopia_PCDetection.h" |
14 | 18 |
|
15 | 19 | namespace PokemonAutomation{ |
@@ -38,8 +42,6 @@ class InfoIconMatcher : public ImageMatch::WaterfillTemplateMatcher{ |
38 | 42 | } |
39 | 43 | }; |
40 | 44 |
|
41 | | - |
42 | | - |
43 | 45 | InfoIconDetector::InfoIconDetector( |
44 | 46 | Color color, |
45 | 47 | VideoOverlay* overlay, |
@@ -209,6 +211,219 @@ bool StampDetector::detect(const ImageViewRGB32& screen){ |
209 | 211 | return found; |
210 | 212 | } |
211 | 213 |
|
| 214 | +class RecipeIconMatcher : public ImageMatch::WaterfillTemplateMatcher{ |
| 215 | +public: |
| 216 | + // Match the yellow negative space for the recipe icon |
| 217 | + RecipeIconMatcher(const char* path) |
| 218 | + : WaterfillTemplateMatcher( |
| 219 | + path, |
| 220 | + Color(210, 180, 0), Color(255, 250, 160), 2100 |
| 221 | + ) |
| 222 | + { |
| 223 | + m_aspect_ratio_lower = 0.9; |
| 224 | + m_aspect_ratio_upper = 1.1; |
| 225 | + m_area_ratio_lower = 0.85; |
| 226 | + m_area_ratio_upper = 1.1; |
| 227 | + } |
| 228 | + |
| 229 | + static const RecipeIconMatcher& single_matcher(){ |
| 230 | + static RecipeIconMatcher matcher("PokemonPokopia/Recipe.png"); |
| 231 | + return matcher; |
| 232 | + } |
| 233 | + static const RecipeIconMatcher& double_matcher(){ |
| 234 | + static RecipeIconMatcher matcher("PokemonPokopia/DoubleRecipe.png"); |
| 235 | + return matcher; |
| 236 | + } |
| 237 | + static const RecipeIconMatcher& triple_matcher(){ |
| 238 | + static RecipeIconMatcher matcher("PokemonPokopia/TripleRecipe.png"); |
| 239 | + return matcher; |
| 240 | + } |
| 241 | + static const RecipeIconMatcher& quad_matcher(){ |
| 242 | + static RecipeIconMatcher matcher("PokemonPokopia/QuadRecipe.png"); |
| 243 | + return matcher; |
| 244 | + } |
| 245 | +}; |
| 246 | + |
| 247 | +RecipeIconDetector::RecipeIconDetector( |
| 248 | + Color color, |
| 249 | + VideoOverlay* overlay, |
| 250 | + const ImageFloatBox& box |
| 251 | +) |
| 252 | + : m_color(color) |
| 253 | + , m_overlay(overlay) |
| 254 | + , m_arrow_box(box) |
| 255 | +{} |
| 256 | +void RecipeIconDetector::make_overlays(VideoOverlaySet& items) const{ |
| 257 | + items.add(m_color, m_arrow_box); |
| 258 | +} |
| 259 | +bool RecipeIconDetector::detect(const ImageViewRGB32& screen){ |
| 260 | + double screen_rel_size = (screen.height() / 1080.0); |
| 261 | + double screen_rel_size_2 = screen_rel_size * screen_rel_size; |
| 262 | + |
| 263 | + double min_area_1080p = 500; |
| 264 | + double rmsd_threshold = 95; |
| 265 | + size_t min_area = size_t(screen_rel_size_2 * min_area_1080p); |
| 266 | + |
| 267 | + const std::vector<std::pair<uint32_t, uint32_t>> FILTERS = { |
| 268 | + {0xffd2b400, 0xfffffaa0} // RGB(210, 180, 0), RGB(255, 250, 160) |
| 269 | + }; |
| 270 | + |
| 271 | + const std::vector<std::pair<const RecipeIconMatcher&, RecipeType>> matchers = { |
| 272 | + {RecipeIconMatcher::single_matcher(), RecipeType::SINGLE}, |
| 273 | + {RecipeIconMatcher::double_matcher(), RecipeType::DOUBLE}, |
| 274 | + {RecipeIconMatcher::triple_matcher(), RecipeType::TRIPLE}, |
| 275 | + {RecipeIconMatcher::quad_matcher(), RecipeType::QUAD}, |
| 276 | + }; |
| 277 | + |
| 278 | + bool found = false; |
| 279 | + m_recipe_type = RecipeType::NOT_RECIPE; |
| 280 | + |
| 281 | + for (const auto& matcher_info : matchers) { |
| 282 | + found = match_template_by_waterfill( |
| 283 | + screen.size(), |
| 284 | + extract_box_reference(screen, m_arrow_box), |
| 285 | + matcher_info.first, |
| 286 | + FILTERS, |
| 287 | + {min_area, SIZE_MAX}, |
| 288 | + rmsd_threshold, |
| 289 | + [&](Kernels::Waterfill::WaterfillObject& object) -> bool { |
| 290 | + m_last_detected = translate_to_parent(screen, m_arrow_box, object); |
| 291 | + return true; |
| 292 | + } |
| 293 | + ); |
| 294 | + if (found) { |
| 295 | + m_recipe_type = matcher_info.second; |
| 296 | + break; |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + if (m_overlay){ |
| 301 | + if (found){ |
| 302 | + m_last_detected_box.emplace(*m_overlay, m_last_detected, COLOR_GREEN); |
| 303 | + }else{ |
| 304 | + m_last_detected_box.reset(); |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + return found; |
| 309 | +} |
| 310 | + |
| 311 | +CoinCountDetector::CoinCountDetector(Logger& logger) |
| 312 | +: m_logger(logger) |
| 313 | +// location in the shop menu |
| 314 | +, m_coin_count_box{0.900000, 0.053000, 0.075000, 0.035000} |
| 315 | +{} |
| 316 | + |
| 317 | +void CoinCountDetector::make_overlays(VideoOverlaySet& items) const{ |
| 318 | + items.add(COLOR_WHITE, m_coin_count_box); |
| 319 | +} |
| 320 | + |
| 321 | +bool CoinCountDetector::detect(const ImageViewRGB32& screen){ |
| 322 | + const ImageViewRGB32 coin_image_crop = extract_box_reference(screen, m_coin_count_box); |
| 323 | + |
| 324 | + const bool text_inside_range = true; |
| 325 | + const bool prioritize_numeric_only_results = true; |
| 326 | + const size_t width_max = SIZE_MAX; |
| 327 | + // The coin crop includes the "," in coin numbers like "1,000". |
| 328 | + // We have to use `min_digit_area` to filter out "," when doing OCR. |
| 329 | + // The min digit area computation is that any dot with size smaller than coin_image_crop.height()/5 is filtered out when OCR. |
| 330 | + const size_t min_digit_area = coin_image_crop.height()*coin_image_crop.height() / 25; |
| 331 | + m_coin_count = 0; |
| 332 | + const std::vector<std::pair<uint32_t, uint32_t>> filters = { |
| 333 | + {0xff808080, 0xffffffff}, |
| 334 | + {0xffa0a0a0, 0xffffffff}, |
| 335 | + {0xffc0c0c0, 0xffffffff}, |
| 336 | + {0xffe0e0e0, 0xffffffff}, |
| 337 | + {0xfff0f0f0, 0xffffffff}, |
| 338 | + }; |
| 339 | + int number = OCR::read_number_waterfill_multifilter( |
| 340 | + m_logger, |
| 341 | + GlobalThreadPools::computation_realtime(), |
| 342 | + coin_image_crop, filters, |
| 343 | + text_inside_range, prioritize_numeric_only_results, width_max, min_digit_area |
| 344 | + ); |
| 345 | + if (number <= 0 || number > 999999){ |
| 346 | + return false; |
| 347 | + } |
| 348 | + m_coin_count = static_cast<uint32_t>(number); |
| 349 | + return true; |
| 350 | +} |
| 351 | + |
| 352 | +CoinCountWatcher::CoinCountWatcher(Logger& logger) |
| 353 | +: CoinCountDetector(logger), VisualInferenceCallback("CoinCountWatcher") |
| 354 | +{} |
| 355 | + |
| 356 | +void CoinCountWatcher::make_overlays(VideoOverlaySet& items) const{ |
| 357 | + CoinCountDetector::make_overlays(items); |
| 358 | +} |
| 359 | + |
| 360 | +bool CoinCountWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){ |
| 361 | + return detect(frame); |
| 362 | +} |
| 363 | + |
| 364 | +class CoinIconMatcher : public ImageMatch::WaterfillTemplateMatcher{ |
| 365 | +public: |
| 366 | + CoinIconMatcher() |
| 367 | + : WaterfillTemplateMatcher( |
| 368 | + "PokemonPokopia/Coin.png", |
| 369 | + Color(210, 150, 30), Color(255, 240, 140), 900 |
| 370 | + ) |
| 371 | + { |
| 372 | + m_aspect_ratio_lower = 0.9; |
| 373 | + m_aspect_ratio_upper = 1.1; |
| 374 | + m_area_ratio_lower = 0.85; |
| 375 | + m_area_ratio_upper = 1.1; |
| 376 | + } |
| 377 | +}; |
| 378 | + |
| 379 | +CoinIconDetector::CoinIconDetector( |
| 380 | + Color color, |
| 381 | + VideoOverlay* overlay, |
| 382 | + const ImageFloatBox& box |
| 383 | +) |
| 384 | + : m_color(color) |
| 385 | + , m_overlay(overlay) |
| 386 | + , m_arrow_box(box) |
| 387 | +{} |
| 388 | +void CoinIconDetector::make_overlays(VideoOverlaySet& items) const{ |
| 389 | + items.add(m_color, m_arrow_box); |
| 390 | +} |
| 391 | +bool CoinIconDetector::detect(const ImageViewRGB32& screen){ |
| 392 | + double screen_rel_size = (screen.height() / 1080.0); |
| 393 | + double screen_rel_size_2 = screen_rel_size * screen_rel_size; |
| 394 | + |
| 395 | + double min_area_1080p = 200; |
| 396 | + double rmsd_threshold = 100; |
| 397 | + size_t min_area = size_t(screen_rel_size_2 * min_area_1080p); |
| 398 | + |
| 399 | + const std::vector<std::pair<uint32_t, uint32_t>> FILTERS = { |
| 400 | + {0xffd2961e, 0xfffff08c} // RGB(210, 150, 30), RGB(255, 240, 140) |
| 401 | + }; |
| 402 | + |
| 403 | + bool found = match_template_by_waterfill( |
| 404 | + screen.size(), |
| 405 | + extract_box_reference(screen, m_arrow_box), |
| 406 | + CoinIconMatcher(), |
| 407 | + FILTERS, |
| 408 | + {min_area, SIZE_MAX}, |
| 409 | + rmsd_threshold, |
| 410 | + [&](Kernels::Waterfill::WaterfillObject& object) -> bool { |
| 411 | + m_last_detected = translate_to_parent(screen, m_arrow_box, object); |
| 412 | + return true; |
| 413 | + } |
| 414 | + ); |
| 415 | + |
| 416 | + if (m_overlay){ |
| 417 | + if (found){ |
| 418 | + m_last_detected_box.emplace(*m_overlay, m_last_detected, COLOR_GREEN); |
| 419 | + }else{ |
| 420 | + m_last_detected_box.reset(); |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + return found; |
| 425 | +} |
| 426 | + |
212 | 427 |
|
213 | 428 |
|
214 | 429 | } |
|
0 commit comments