void iterateArray(Value::ConstValueIterator& itr, Value::ConstValueIterator& itrEnd, StringBuffer *buffer, Writer<StringBuffer> *writer, stringstream& ss) { for (; itr != itrEnd; itr++) { if (itr->IsObject()) { Value::ConstMemberIterator itr_ = itr->MemberBegin(); Value::ConstMemberIterator itrEnd_ = itr->MemberEnd(); iterateObject(itr_, itrEnd_, buffer, writer, ss); } else if (itr->IsArray()) { Value::ConstValueIterator itr_ = itr->Begin(); Value::ConstValueIterator itrEnd_ = itr->End(); iterateArray(itr_, itrEnd_, buffer, writer, ss); } else if (itr->IsBool()) { ss << DELIM << itr->GetBool(); } else if (itr->IsInt()) { ss << DELIM << itr->GetInt(); } else if (itr->IsInt64()) { ss << DELIM << itr->GetInt64(); } else if (itr->IsDouble()) { ss << DELIM << itr->GetDouble(); } else if (itr->IsString()) { ss << DELIM << "\"" << itr->GetString() << "\""; } else { throw runtime_error(string("Case missing from tokenizer")); } } }
/* **************************************************************************** * * parseContextAttributeCompoundValue - */ std::string parseContextAttributeCompoundValue ( const Value::ConstValueIterator& node, ContextAttribute* caP, orion::CompoundValueNode* parent ) { if (node->IsObject()) { int counter = 0; for (Value::ConstMemberIterator iter = node->MemberBegin(); iter != node->MemberEnd(); ++iter) { std::string nodeType = jsonParseTypeNames[iter->value.GetType()]; orion::CompoundValueNode* cvnP = new orion::CompoundValueNode(); cvnP->valueType = stringToCompoundType(nodeType); cvnP->name = iter->name.GetString(); cvnP->container = parent; cvnP->rootP = parent->rootP; cvnP->level = parent->level + 1; cvnP->siblingNo = counter; cvnP->path = parent->path + cvnP->name; if (nodeType == "String") { cvnP->stringValue = iter->value.GetString(); } else if (nodeType == "Number") { cvnP->numberValue = iter->value.GetDouble(); } else if ((nodeType == "True") || (nodeType == "False")) { cvnP->boolValue = (nodeType == "True")? true : false; } else if (nodeType == "Null") { cvnP->valueType = orion::ValueTypeNone; } else if (nodeType == "Object") { cvnP->path += "/"; cvnP->valueType = orion::ValueTypeObject; } else if (nodeType == "Array") { cvnP->path += "/"; cvnP->valueType = orion::ValueTypeVector; } parent->childV.push_back(cvnP); // // Recursive call if Object or Array // if ((nodeType == "Object") || (nodeType == "Array")) { parseContextAttributeCompoundValue(iter, caP, cvnP); } ++counter; } } else if (node->IsArray()) { int counter = 0; for (Value::ConstValueIterator iter = node->Begin(); iter != node->End(); ++iter) { std::string nodeType = jsonParseTypeNames[iter->GetType()]; orion::CompoundValueNode* cvnP = new orion::CompoundValueNode(); char itemNo[4]; snprintf(itemNo, sizeof(itemNo), "%03d", counter); cvnP->valueType = stringToCompoundType(nodeType); cvnP->container = parent; cvnP->rootP = parent->rootP; cvnP->level = parent->level + 1; cvnP->siblingNo = counter; cvnP->path = parent->path + "[" + itemNo + "]"; if (nodeType == "String") { cvnP->stringValue = iter->GetString(); } else if (nodeType == "Number") { cvnP->numberValue = iter->GetDouble(); } else if ((nodeType == "True") || (nodeType == "False")) { cvnP->boolValue = (nodeType == "True")? true : false; } else if (nodeType == "Null") { cvnP->valueType = orion::ValueTypeNone; } else if (nodeType == "Object") { cvnP->path += "/"; cvnP->valueType = orion::ValueTypeObject; } else if (nodeType == "Array") { cvnP->path += "/"; cvnP->valueType = orion::ValueTypeVector; } parent->childV.push_back(cvnP); // // Recursive call if Object or Array // if ((nodeType == "Object") || (nodeType == "Array")) { parseContextAttributeCompoundValue(iter, caP, cvnP); } ++counter; } } return "OK"; }
std::multimap<RarityType,std::string> MtgJsonAllSetsData::getCardPool( const std::string& code ) const { std::multimap<RarityType,std::string> rarityMap; if( mAllSetCodes.count(code) == 0 ) { mLogger->warn( "Unable to find set {}, returning empty card pool", code ); return rarityMap; } // In parse() this was vetted to be safe and yield an Array-type value. const Value& cardsValue = mDoc[code]["cards"]; mLogger->debug( "{:-^40}", "assembling card pool" ); for( Value::ConstValueIterator iter = cardsValue.Begin(); iter != cardsValue.End(); ++iter ) { Value::ConstMemberIterator nameIter = iter->FindMember( "name" ); Value::ConstMemberIterator rarityIter = iter->FindMember( "rarity" ); if( nameIter != iter->MemberEnd() && nameIter->value.IsString() && rarityIter != iter->MemberEnd() && rarityIter->value.IsString() ) { std::string nameStr( nameIter->value.GetString() ); std::string rarityStr( rarityIter->value.GetString() ); mLogger->debug( "json: {} : {}", nameStr, rarityStr ); // Some cards have multiple entries (i.e. split/flip/double-sided), // so make sure they are only represented once. Done by skipping // over cards whose name doesn't match the first entry of the // 'names' array (if it exists). if( iter->HasMember("names") ) { const Value& names = (*iter)["names"]; if( names.IsArray() && !names.Empty() && (nameStr != names[0]) ) { continue; } // Modify the name for split cards. if( MtgJson::isSplitCard( *iter ) ) { nameStr = MtgJson::createSplitCardName( names ); } } // Some cards have variations with multiple entries that should // only be counted once. Done by checking if there are // variations and checking a card's number for a non-digit, // non-'a' ending. if( iter->HasMember("variations") && iter->HasMember("number") ) { const Value& numberValue = (*iter)["number"]; if( numberValue.IsString() ) { const std::string numberStr( numberValue.GetString() ); const char c = numberStr.back(); if( !std::isdigit( c ) && (c != 'a') ) { continue; } } } RarityType rarity = MtgJson::getRarityFromString( rarityStr ); if( rarity != RARITY_UNKNOWN ) rarityMap.insert( std::make_pair( rarity, nameStr ) ); else mLogger->warn( "Unknown rarity type {}, ignoring!", rarityStr ); } else { mLogger->notice( "card entry without name or rarity in set {}", code ); } } return rarityMap; }
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; }