Exemple #1
0
ComplexType Parser::parseComplexType( ParserContext *context, const QDomElement &element )
{
  ComplexType newType( d->mNameSpace );

  newType.setName( element.attribute( QLatin1String("name") ) );

  if (debugParsing())
      qDebug() << "complexType:" << d->mNameSpace << newType.name();

  if ( element.hasAttribute( QLatin1String("mixed") ) )
    newType.setContentModel( XSDType::MIXED );

  QDomElement childElement = element.firstChildElement();

  AttributeGroup::List attributeGroups;

  while ( !childElement.isNull() ) {
    NSManager namespaceManager( context, childElement );
    const QName name( childElement.tagName() );
    if ( name.localName() == QLatin1String("all") ) {
      all( context, childElement, newType );
    } else if ( name.localName() == QLatin1String("sequence") ) {
      parseCompositor( context, childElement, newType );
    } else if ( name.localName() == QLatin1String("choice") ) {
      parseCompositor( context, childElement, newType );
    } else if ( name.localName() == QLatin1String("attribute") ) {
      newType.addAttribute( parseAttribute( context, childElement ) );
    } else if ( name.localName() == QLatin1String("attributeGroup") ) {
      AttributeGroup g = parseAttributeGroup( context, childElement );
      attributeGroups.append( g );
    } else if ( name.localName() == QLatin1String("anyAttribute") ) {
      addAnyAttribute( context, childElement, newType );
    } else if ( name.localName() == QLatin1String("complexContent") ) {
      parseComplexContent( context, childElement, newType );
    } else if ( name.localName() == QLatin1String("simpleContent") ) {
      parseSimpleContent( context, childElement, newType );
    } else if ( name.localName() == QLatin1String("annotation") ) {
      Annotation::List annotations = parseAnnotation( context, childElement );
      newType.setDocumentation( annotations.documentation() );
      newType.setAnnotations( annotations );
    }

    childElement = childElement.nextSiblingElement();
  }

  newType.setAttributeGroups( attributeGroups );

  return newType;
}
Exemple #2
0
ComplexType Parser::parseComplexType( ParserContext *context,
                                      const QDomElement &complexTypeElement,
                                      const QString elementName )
{
  ComplexType newType( d->mNameSpace );

  newType.setName( complexTypeElement.attribute( "name" ) );

  if ( complexTypeElement.hasAttribute( "mixed" ) )
    newType.setContentModel( XSDType::MIXED );

  QDomElement childElement = complexTypeElement.firstChildElement();

  AttributeGroup::List attributeGroups;

  while ( !childElement.isNull() ) {
    QName name = childElement.tagName();
    if ( name.localName() == "all" ) {
      all( context, childElement, newType );
    } else if ( name.localName() == "sequence" ) {
      parseCompositor( context, childElement, newType );
    } else if ( name.localName() == "choice" ) {
      parseCompositor( context, childElement, newType );
    } else if ( name.localName() == "attribute" ) {
      newType.addAttribute( parseAttribute( context, childElement, elementName ) );
    } else if ( name.localName() == "attributeGroup" ) {
      AttributeGroup g = parseAttributeGroup( context, childElement );
      attributeGroups.append( g );
    } else if ( name.localName() == "anyAttribute" ) {
      addAnyAttribute( context, childElement, newType );
    } else if ( name.localName() == "complexContent" ) {
      parseComplexContent( context, childElement, newType );
    } else if ( name.localName() == "simpleContent" ) {
      parseSimpleContent( context, childElement, newType );
    } else if ( name.localName() == "annotation" ) {
      Annotation::List annotations = parseAnnotation( context, childElement );
      newType.setDocumentation( annotations.documentation() );
      newType.setAnnotations( annotations );
    }

    childElement = childElement.nextSiblingElement();
  }

  newType.setAttributeGroups( attributeGroups );

  return newType;
}
Exemple #3
0
void Parser::all( ParserContext *context, const QDomElement &element, ComplexType &ct )
{
  QDomElement childElement = element.firstChildElement();

  while ( !childElement.isNull() ) {
    QName name = childElement.tagName();
    if ( name.localName() == "element" ) {
      ct.addElement( parseElement( context, childElement, ct.nameSpace(),
        childElement ) );
    } else if ( name.localName() == "annotation" ) {
      Annotation::List annotations = parseAnnotation( context, childElement );
      ct.setDocumentation( annotations.documentation() );
      ct.setAnnotations( annotations );
    }

    childElement = childElement.nextSiblingElement();
  }
}
Exemple #4
0
void genHardNegativesOnAnnotations(FeatGen* ldgFeatGen,const path& baseDir, datasets currDataset, bool writeOutWins, bool viz,string modelfile)
{
	vector<path> negativePaths;
	vector<path> negTrainPaths;
	vector<path> negTestPaths;
	FrameId firstTestFrame; // separates training and testing data
	string learnFileStem;
	vector<path> normPosWinPathsTrain; // if any use folder with normalized cropped windows
		vector<path> normPosWinPathsTest; // if any use folder with normalized cropped windows
	getLearnInfos(baseDir, currDataset, negativePaths, negTrainPaths, negTestPaths,normPosWinPathsTrain, normPosWinPathsTest, learnFileStem, firstTestFrame);

	path negTrainFolder = baseDir / "learning" / "train_neg_hard_annot";
	remove_all(negTrainFolder);
	create_directories(negTrainFolder);

	//std::ios_base::openmode mode = ;
	//fs::ofstream trainFile((baseDir / "learning" / learnFileStem).string(), std::ios_base::out | ios_base::app);
	//fs::ofstream trainFilesFile((baseDir / "learning" / learnFileStem).string() + ".files", std::ios_base::out | ios_base::app);

	fs::ofstream trainFileHard((baseDir / "learning" / learnFileStem).string() + "_hard_annot");


	if ( !(modelfile.length() > 0 ) )
	{
		modelfile = (baseDir / "learning" / learnFileStem).string() + ".model";
	}
	path mfp(modelfile);
	if ( !exists(mfp))
	{
		cerr << "Modelfile does not exist: " << modelfile << endl;
		exit(EXIT_FAILURE);
	}
	cout << "genHardNegativesOnAnnotations using model: " << modelfile << endl;
	Detector pd(ldgFeatGen);
	pd.setStartScale(1.0);
	pd.setScaleStep(1.04);
	pd.loadSVMWeights(modelfile);
	float decThresh = 0;

	int nww = ldgFeatGen->getWinW();
	int nwh = ldgFeatGen->getWinH();

	// use caltech annotations
	FrameId startFrame;
	startFrame.set= 0;
	startFrame.video= 0;
	startFrame.image= 0;

	size_t featSize = getFeatLen(ldgFeatGen);

			vector<path> annotFiles;
			annotFiles = findAllAnnotations(baseDir,startFrame);

			if ( annotFiles.size() == 0 )
			{
				cerr << "No annotations found." << endl;
				exit(EXIT_FAILURE);
			}

			for ( vector<path>::iterator it (annotFiles.begin()); it != annotFiles.end(); ++it)
			{
				path vbbFile = *it;
				path txtFolder = vbbFile.parent_path() / vbbFile.stem();

				vector<path> v;
				copy(directory_iterator(txtFolder),directory_iterator(),back_inserter(v));

				sort(v.begin(),v.end());
				int posTrainCounter = 0;
				cout << "0% ";
				cout.flush();
				for ( vector<path>::iterator vit (v.begin()); vit != v.end(); ++vit)
				{
					path txtFile = *vit; // annotation txtFile

					// corresponding image file
					path imgFile = baseDir / "videos" / txtFile.parent_path().parent_path().stem() / txtFile.parent_path().stem() / txtFile.filename().stem();
					bool isTrain = true;

					if (frameLTEQ(firstTestFrame,parseFrameFromPath(imgFile)))
					{
						isTrain = false;
					}

					if ( skipPath(imgFile,startFrame) )
						continue;

					if ( !isTrain ) continue;

					imgFile += findImageExtension(imgFile);

					if ( !exists(imgFile) || !is_regular_file(imgFile))
					{
						cerr << "Could not find corresponding image file " <<imgFile <<endl;
						cerr << "Export all .seq files using provided matlab code." << endl;
						exit(EXIT_FAILURE);
					}

					// parse annotations from txtFile
					fs::ifstream f(txtFile);
					if (!f)
					{
						cerr << "cannot open file " << txtFile << endl;
						exit(EXIT_FAILURE);
					}

					vector<Annotation> annots;

					string buffer;
					while (std::getline(f,buffer))
					{
						Annotation a = parseAnnotation(buffer);
						if ( a.type == "person" )
						{
							//printAnnotation(a);
							annots.push_back(a);
						}

					}

					// extract normalized bb images
					Mat img = imread(imgFile.string(), CV_LOAD_IMAGE_COLOR);

					posTrainCounter++;
					if ( (posTrainCounter % 100) == 0)
					{
						float donePerc = posTrainCounter / (float)v.size();
						cout << floor(donePerc * 100) << "% ";
						cout.flush();
					}


					path nf = imgFile;
					string filePre  = nf.parent_path().parent_path().stem().string() + nf.parent_path().stem().string() +  "_" + nf.filename().stem().string();

					vector< pair < Detection,double* > > detections;
					vector<Detection> alldetections;
					pd.detect(img,detections,alldetections,decThresh,true);

					vector<bool> rectIsUsed;
					rectIsUsed.resize(detections.size());
					for ( size_t i = 0; i < detections.size(); i++)
					{
						pair < Detection,double* > mp = detections[i];
						Detection det = mp.first;
						Rect r = det.r;
						double* f = mp.second;

						// check if rectangle is annotated object
						bool rectIsFree = true;
						for (vector<Annotation>::iterator ait (annots.begin()); ait != annots.end(); ++ait)
						{
							Annotation an = *ait;
							Rect a(an.x,an.y,an.width,an.height);

							// calc intersection area
							Rect inters =  a & r;
							if ( inters.area() > 0 )
							{
								// further analyze intersection

								double ratio1 = (double) inters.area() / (double) r.area();
								double ratio2 = (double) inters.area() / (double) a.area();

								double ratio = min(ratio1,ratio2);


								rectIsFree = !(ratio > 0.5);
								if ( !rectIsFree )
								{
									break;
								}
							}
						}
						rectIsUsed[i] = rectIsFree;

						if ( rectIsFree )
						{
							// save as negative example
							writeFeatToSVMStream(f,trainFileHard,featSize,false);
						}
						else
						{
							// save as positive example
							writeFeatToSVMStream(f,trainFileHard,featSize,true);
						}

						delete[] f;

					}


					if ( viz || writeOutWins )
					{
						Mat vizImg = img.clone();
						if ( alldetections.size() < 100 )
						{
							for ( vector< Detection >::iterator it(alldetections.begin()); it != alldetections.end(); it++)
							{
								Detection det = *it;
								Rect r = det.rNormWin;

								//cout << r.x << " " << r.y << " " << r.width << "x" << r.height << " scale:" << det.scale << " decisionValue:" << det.decisionValue << endl;
								rectangle(vizImg,r,Scalar(255,50,50),1,8);

							}
						}

						for ( size_t i = 0; i < detections.size(); i++)
						{
							Detection det = detections[i].first;
							Rect r = det.r;

							if ( rectIsUsed[i])
							{
								rectangle(vizImg,r,Scalar(0,0,255),1,8);
							}
							else
							{
								rectangle(vizImg,r,Scalar(0,255,255),1,8);
							}

						}

						displayAnnotations(vizImg,annots);

						if ( writeOutWins )
						{
							string fileNameOnly = (filePre +"_hard.png");
							path nwPath = negTrainFolder / fileNameOnly;
							imwrite(nwPath.string(),vizImg);
						}
						if ( viz )
						{
							imshow("negative training image",vizImg);
							waitKey(0);
							destroyWindow("negative training image");
						}
					}


				}


			}
			cout << "100% "<< endl;
	trainFileHard.close();
}
Exemple #5
0
bool Parser::parseSchemaTag( ParserContext *context, const QDomElement &root )
{
  QName name(root.tagName());
  if ( name.localName() != QLatin1String("schema") ) {
    qDebug() << "ERROR localName=" << name.localName();
    return false;
  }

  // Already done by caller when coming from type.cpp, but doesn't hurt to do twice
  context->namespaceManager()->enterChild(root);

  // This method can call itself recursively, so save/restore the member attribute.
  QString oldNamespace = d->mNameSpace;
  if ( root.hasAttribute( QLatin1String("targetNamespace") ) )
    d->mNameSpace = root.attribute( QLatin1String("targetNamespace") );

  if (root.attribute( QLatin1String("elementFormDefault") ) == QLatin1String("qualified"))
      d->mDefaultQualifiedElements = true;

  if (root.attribute( QLatin1String("attributeFormDefault") ) == QLatin1String("qualified"))
      d->mDefaultQualifiedAttributes = true;

 // mTypesTable.setTargetNamespace( mNameSpace );

  QDomElement element = root.firstChildElement();
  while ( !element.isNull() ) {
    NSManager namespaceManager( context, element );
    const QName name( element.tagName() );
    if (debugParsing())
        qDebug() << "Schema: parsing" << name.localName();
    if ( name.localName() == QLatin1String("import") ) {
      parseImport( context, element );
    } else if ( name.localName() == QLatin1String("element") ) {
      addGlobalElement( parseElement( context, element, d->mNameSpace, element ) );
    } else if ( name.localName() == QLatin1String("complexType") ) {
      ComplexType ct = parseComplexType( context, element );
      d->mComplexTypes.append( ct );
    } else if ( name.localName() == QLatin1String("simpleType") ) {
      SimpleType st = parseSimpleType( context, element );
      d->mSimpleTypes.append( st );
    } else if ( name.localName() == QLatin1String("attribute") ) {
      addGlobalAttribute( parseAttribute( context, element, d->mNameSpace ) );
    } else if ( name.localName() == QLatin1String("attributeGroup") ) {
      d->mAttributeGroups.append( parseAttributeGroup( context, element, d->mNameSpace ) );
    } else if ( name.localName() == QLatin1String("group") ) {
      d->mGroups.append( parseGroup( context, element, d->mNameSpace ) );
    } else if ( name.localName() == QLatin1String("annotation") ) {
      d->mAnnotations = parseAnnotation( context, element );
    } else if ( name.localName() == QLatin1String("include") ) {
      parseInclude( context, element );
    } else {
        qWarning() << "Unsupported schema element" << name.localName();
    }

    element = element.nextSiblingElement();
  }

  if (!resolveForwardDeclarations())
      return false;

  d->mNameSpace = oldNamespace;

  return true;
}
Exemple #6
0
bool Parser::parseSchemaTag( ParserContext *context, const QDomElement &root )
{
  QName name = root.tagName();
  if ( name.localName() != QLatin1String("schema") )
    return false;

  NSManager *parentManager = context->namespaceManager();
  NSManager namespaceManager;

  // copy namespaces from wsdl
  if ( parentManager )
    namespaceManager = *parentManager;

  context->setNamespaceManager( &namespaceManager );

  QDomNamedNodeMap attributes = root.attributes();
  for ( int i = 0; i < attributes.count(); ++i ) {
    QDomAttr attribute = attributes.item( i ).toAttr();
    if ( attribute.name().startsWith( QLatin1String("xmlns:") ) ) {
      QString prefix = attribute.name().mid( 6 );
      context->namespaceManager()->setPrefix( prefix, attribute.value() );
    }
  }

  if ( root.hasAttribute( QLatin1String("targetNamespace") ) )
    d->mNameSpace = root.attribute( QLatin1String("targetNamespace") );

 // mTypesTable.setTargetNamespace( mNameSpace );

  QDomElement element = root.firstChildElement();
  while ( !element.isNull() ) {
    QName name = element.tagName();
    if ( name.localName() == QLatin1String("import") ) {
      parseImport( context, element );
    } else if ( name.localName() == QLatin1String("element") ) {
      addGlobalElement( parseElement( context, element, d->mNameSpace, element ) );
    } else if ( name.localName() == QLatin1String("complexType") ) {
      ComplexType ct = parseComplexType( context, element );
      d->mComplexTypes.append( ct );
    } else if ( name.localName() == QLatin1String("simpleType") ) {
      SimpleType st = parseSimpleType( context, element );
      d->mSimpleTypes.append( st );
    } else if ( name.localName() == QLatin1String("attribute") ) {
      addGlobalAttribute( parseAttribute( context, element ) );
    } else if ( name.localName() == QLatin1String("attributeGroup") ) {
      d->mAttributeGroups.append( parseAttributeGroup( context, element ) );
    } else if ( name.localName() == QLatin1String("annotation") ) {
      d->mAnnotations = parseAnnotation( context, element );
    } else if ( name.localName() == QLatin1String("include") ) {
      parseInclude( context, element );
    }

    element = element.nextSiblingElement();
  }

  context->setNamespaceManager( parentManager );
  d->mNamespaces = joinNamespaces( d->mNamespaces, namespaceManager.uris() );
  d->mNamespaces = joinNamespaces( d->mNamespaces, QStringList( d->mNameSpace ) );

  resolveForwardDeclarations();

  return true;
}
Exemple #7
0
static void
parseTitleInfo (xmlDocPtr doc, xmlNodePtr cur, FB2Content *fb)
{

  xmlChar *content;
  cur = cur->children;
  while (cur != NULL) {

    if (!xmlStrcmp(cur->name, (const xmlChar *)"p")) {
      parseP(doc, cur, 1, fb);

    } else if (!xmlStrcmp(cur->name, (const xmlChar *)"empty-line")) {
      bufferAppend("\n", 1, fb);

    } else if (!xmlStrcmp(cur->name, (const xmlChar *)"genre")) {
      content = xmlNodeGetContent(cur->children);

      if(content) {
	/* add genre */
	int i;
	for (i=0; i<BUF_SIZE-1; i++) {
	  if (fb->genres[i] == NULL) {
	    fb->genres[i] = content;
	    fb->genres[i+1] = NULL;
	    fb->num_genres++;
	    break;
	  }
	}
      }

    } else if (!xmlStrcmp(cur->name, (const xmlChar *)"book-title")) {
      content = xmlNodeGetContent(cur->children);
      if (content)
	strncpy(fb->name, content, BUF_SIZE);
      xmlFree(content);

    } else if (!xmlStrcmp(cur->name, (const xmlChar *)"author")) {
      parseAuthor(doc, cur, fb);

    } else if (!xmlStrcmp(cur->name, (const xmlChar *)"annotation")) {
      parseAnnotation(doc, cur, fb);

    } else if (!xmlStrcmp(cur->name, (const xmlChar *)"coverpage")) {
      xmlNodePtr c;
      c = cur->children;
      while (c) {
        if (!xmlStrcmp(c->name, (const xmlChar *)"image")) {
          xmlChar *cover_href;
          cover_href = xmlGetProp(c, (const xmlChar *)"href");
          strncpy(fb->cover_href, cover_href, BUF_SIZE);
          xmlFree (cover_href);
          break;
        }
        c = c->next;
      }
      /*
      content = xmlNodeGetContent(cur->children);
      if (content)
	strncpy(fb->cover_href, content, BUF_SIZE);
      */
      //if (cover_href)
      /*xmlFree(content);*/

    }

    cur = cur->next;
  }
  return;
}