std::vector<SlotType> MtgJsonAllSetsData::getBoosterSlots( const std::string& code ) const { std::vector<SlotType> boosterSlots; if( mBoosterSetCodes.count(code) == 0 ) { mLogger->warn( "No booster member in set {}, returning empty booster slots", code ); return boosterSlots; } // In parse() this was vetted to be safe and yield an Array-type value. const Value& boosterValue = mDoc[code]["booster"]; mLogger->debug( "{:-^40}", "assembling booster slots" ); for( Value::ConstValueIterator iter = boosterValue.Begin(); iter != boosterValue.End(); ++iter ) { if( iter->IsArray() ) { // create a set of any strings in the array std::set<std::string> slotArraySet; for( unsigned i = 0; i < iter->Size(); ++i ) { const Value& val = (*iter)[i]; if( val.IsString() ) { slotArraySet.insert( val.GetString() ); mLogger->debug( "booster slot array[{}]: {}", i, val.GetString() ); } else { mLogger->warn( "Non-string in booster slot array, ignoring!" ); } } const std::set<std::string> rareMythicRareSlot { "rare", "mythic rare" }; if( slotArraySet == rareMythicRareSlot ) boosterSlots.push_back( SLOT_RARE_OR_MYTHIC_RARE ); else mLogger->warn( "Unrecognized booster slot array, ignoring!" ); } else if( iter->IsString() ) { std::string slotStr( iter->GetString() ); mLogger->debug( "booster slot string: {}", slotStr ); if( slotStr == "common" ) boosterSlots.push_back( SLOT_COMMON ); else if( slotStr == "uncommon" ) boosterSlots.push_back( SLOT_UNCOMMON ); else if( slotStr == "rare" ) boosterSlots.push_back( SLOT_RARE ); else if( slotStr == "timeshifted purple" ) boosterSlots.push_back( SLOT_TIMESHIFTED_PURPLE ); else if( slotStr == "land" ) { /* do nothing */ } else if( slotStr == "marketing" ) { /* do nothing */ } else mLogger->warn( "Unrecognized booster slot type {}, ignoring!", slotStr ); } else { mLogger->warn( "Non-string booster slot type, ignoring!" ); } } return boosterSlots; }
list loadCOCO( const std::string & name, int fold ) { using namespace rapidjson; // Load the annotations char buf[1024]; sprintf( buf, COCO_ANNOT.c_str(), name.c_str() ); // Read the json file Document doc; std::ifstream t(buf); std::string json_str = std::string(std::istreambuf_iterator<char>(t),std::istreambuf_iterator<char>()); doc.Parse( (char*)json_str.c_str() ); // Go through all instance labels std::unordered_map< uint64_t, std::vector<int> > categories; std::unordered_map< uint64_t, std::vector<float> > areas; std::unordered_map< uint64_t, std::vector<Polygons> > segments; const Value & instances = doc["instances"]; for ( Value::ConstValueIterator i = instances.Begin(); i != instances.End(); i++ ) { // Get the image id Value::ConstMemberIterator cmi_image_id = i->FindMember("image_id"); eassert( cmi_image_id != i->MemberEnd() ); const int image_id = cmi_image_id->value.GetInt(); // Get the category id Value::ConstMemberIterator cmi_category_id = i->FindMember("category_id"); eassert( cmi_category_id != i->MemberEnd() ); const int category_id = cmi_category_id->value.GetInt(); // Get the category id Value::ConstMemberIterator cmi_area = i->FindMember("area"); eassert( cmi_area != i->MemberEnd() ); const float area = cmi_area->value.GetDouble(); // Read the polygon Value::ConstMemberIterator cmi_segmentation = i->FindMember("segmentation"); eassert( cmi_segmentation != i->MemberEnd() ); const Value & segmentations = cmi_segmentation->value; // For now just use the first segmentation for each object Polygons polygons; for( Value::ConstValueIterator segmentation = segmentations.Begin(); segmentation!=segmentations.End(); segmentation++ ){ Polygon polygon = RMatrixXf( segmentation->Size() / 2, 2 ); float * ppolygon = polygon.data(); for ( Value::ConstValueIterator j = segmentation->Begin(); j != segmentation->End(); j++ ) *(ppolygon++) = j->GetDouble(); polygons.push_back( polygon ); } if( !ONLY_CONNECTED || polygons.size() == 1 ) { categories[ image_id ].push_back( category_id ); segments[ image_id ].push_back( polygons ); areas[ image_id ].push_back( area ); } } // Load all images Value::ConstValueIterator B = doc["images"].Begin(), E = doc["images"].End(); const int N = E-B; Value::ConstValueIterator i0 = B+(fold*N/N_FOLDS), i1 = B+((fold+1)*N/N_FOLDS); std::vector< std::shared_ptr<Image8u> > images( i1 - i0 ); #pragma omp parallel for for ( int k=0; k<i1-i0; k++ ) { Value::ConstValueIterator i = i0+k; // Get the file name and path Value::ConstMemberIterator cmi_file_name = i->FindMember("file_name"); eassert( cmi_file_name != i->MemberEnd() ); const std::string file_name = cmi_file_name->value.GetString(); Value::ConstMemberIterator cmi_file_path = i->FindMember("file_path"); eassert( cmi_file_path != i->MemberEnd() ); const std::string file_path = cmi_file_path->value.GetString(); // Add the image entry images[i-i0] = imreadShared( coco_dir+"/"+file_path+"/"+file_name ); } // Create the python struct with the result list r; for ( Value::ConstValueIterator i = i0; i != i1; i++ ) { // Get the image id Value::ConstMemberIterator cmi_image_id = i->FindMember("id"); eassert( cmi_image_id != i->MemberEnd() ); const int image_id = cmi_image_id->value.GetInt(); // Add the image entry const int N = categories[ image_id ].size(); if( N > 0 ){ dict entry; entry["id"] = image_id; entry["image"] = images[i - i0]; entry["categories"] = categories[ image_id ]; entry["areas"] = areas[ image_id ]; entry["segmentation"] = segments[ image_id ]; r.append( entry ); } // else // printf("Image '%d' doesn't have any annotations!\n", image_id ); } return r; }