Esempio n. 1
0
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"));
		}
	}
}
Esempio n. 2
0
 void JSONArray::_parseRapidJSONArray(void *value) {
     rapidjson::Value *rapidjsonValue = (rapidjson::Value *)value;
     
     for (Value::ConstValueIterator currentValue = rapidjsonValue->Begin(); currentValue != rapidjsonValue->End(); ++currentValue)
         
         switch (currentValue->GetType()) {
             case kNullType:
                 break;
             case kFalseType:
             case kTrueType:
                 this->appendValue(shared_ptr<JSONNumber> (new JSONNumber(kTrueType ? true: false)));
                 break;
             case kObjectType: {
                 shared_ptr<JSONObject> obj(new JSONObject());
                 obj->_parseRapidJSONObject((void*)currentValue);
                 this->appendValue(obj);
             }
                 break;
             case kArrayType: {
                 shared_ptr<JSONArray> obj(new JSONArray());
                 obj->_parseRapidJSONArray((void*)currentValue);
                 this->appendValue(obj);
             }
                 break;
             case kStringType:
                 this->appendValue(shared_ptr<JSONString> (new JSONString(currentValue->GetString())));
                 break;
             case kNumberType:
                 if (rapidjsonValue->IsDouble()) {
                     this->appendValue(shared_ptr<JSONNumber> (new JSONNumber(currentValue->GetDouble())));
                 } else if (rapidjsonValue->IsInt() || currentValue->IsInt64()) {
                     this->appendValue(shared_ptr<JSONNumber> (new JSONNumber(currentValue->GetInt())));
                 } else if (currentValue->IsUint() || currentValue->IsUint64()) {
                     this->appendValue(shared_ptr<JSONNumber> (new JSONNumber(currentValue->GetUint())));
                 }
                 
                 break;
         }
 }
/* ****************************************************************************
*
* 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";
}
/* ****************************************************************************
*
* parseContextAttributeCompoundValueStandAlone - 
*/
std::string parseContextAttributeCompoundValueStandAlone
(
  Document&            document,
  ContextAttribute*    caP,
  orion::ValueType     valueType
)
{
  caP->compoundValueP            = new orion::CompoundValueNode();
  caP->compoundValueP->name      = "TOP";
  caP->compoundValueP->container = caP->compoundValueP;
  caP->compoundValueP->valueType = caP->valueType;  // Convert to other type?
  caP->compoundValueP->path      = "/";
  caP->compoundValueP->rootP     = caP->compoundValueP;
  caP->compoundValueP->level     = 0;
  caP->compoundValueP->siblingNo = 0;

  orion::CompoundValueNode*   parent  = caP->compoundValueP;


  //
  // Children of the node
  //
  if (caP->valueType == orion::ValueTypeVector)
  {
    int counter  = 0;

    for (Value::ConstValueIterator iter = document.Begin(); iter != document.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);

      //
      // Start recursive calls if Object or Array
      //
      if ((nodeType == "Object") || (nodeType == "Array"))
      {
        parseContextAttributeCompoundValue(iter, caP, cvnP);
      }
      else if (!caP->typeGiven)
      {
        caP->type = defaultType(caP->valueType);
      }

      ++counter;
    }
  }
  else if (caP->valueType == orion::ValueTypeObject)
  {
    int counter  = 0;

    for (Value::ConstMemberIterator iter = document.MemberBegin(); iter != document.MemberEnd(); ++iter)
    {
      std::string                nodeType = jsonParseTypeNames[iter->value.GetType()];
      orion::CompoundValueNode*  cvnP     = new orion::CompoundValueNode();

      cvnP->name       = iter->name.GetString();
      cvnP->valueType  = stringToCompoundType(nodeType);
      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);
        
      //
      // Start recursive calls if Object or Array
      //
      if ((nodeType == "Object") || (nodeType == "Array"))
      {
        parseContextAttributeCompoundValue(iter, caP, cvnP);
      }
      else if (!caP->typeGiven)
      {
        caP->type = defaultType(caP->valueType);
      }

      ++counter;
    }
  }

  return "OK";
}
Esempio n. 5
0
File: coco.cpp Progetto: CUAir/edges
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;
}