ソースを参照

experimental: added polygon annotation support for siftflow dataset

Sven Sickert 10 年 前
コミット
68dc514b0f
3 ファイル変更111 行追加16 行削除
  1. 24 15
      cbaselib/LabeledFileList.cpp
  2. 85 0
      cbaselib/LocalizationResult.cpp
  3. 2 1
      cbaselib/LocalizationResult.h

+ 24 - 15
cbaselib/LabeledFileList.cpp

@@ -90,7 +90,8 @@ LocalizationResult *LabeledFileList::getLocalizationInfo ( const ClassNames & cl
 
     lr = new LocalizationResult ( &classnames, mask, classno );
 
-  } else if ( format == "imagergb" ) {
+  }
+  else if ( format == "imagergb" ) {
     NICE::ColorImage mask;
     try {
       mask.read ( lfile );
@@ -102,27 +103,35 @@ LocalizationResult *LabeledFileList::getLocalizationInfo ( const ClassNames & cl
     }
     lr = new LocalizationResult ( &classnames, mask );
 
-  } else if ( format == "polygon" ) {
-    lr = new LocalizationResult ( &classnames );
+  }
+  else if ( format == "polygon" ) {
+      lr = new LocalizationResult ( &classnames );
 
-    lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON );
+      lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON );
 
       if ( debug_dataset )
-	  fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
-    }
-    else if ( format == "imagelabeler" ) {
+        fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
+  }
+  else if ( format == "polygon_siftflow" ) {
+      lr = new LocalizationResult ( &classnames );
 
-        lr = new LocalizationResult ( &classnames );
-        lr->loadImageInfo(lfile);
+      lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON_SIFTFLOW );
 
-    }
-    else {
-      fthrow(Exception, "Localization format not yet supported !!\n");
-    }
+      if ( debug_dataset )
+        fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
+  }
+  else if ( format == "imagelabeler" ) {
+    lr = new LocalizationResult ( &classnames );
+    lr->loadImageInfo(lfile);
+
+  }
+  else {
+    fthrow(Exception, "Localization format not yet supported !!\n");
+  }
    
-    if ( debug_dataset )
+  if ( debug_dataset )
 	if ( lr != NULL )
-		    fprintf (stderr, "%s (%d objects)\n", lfile.c_str(), (int)lr->size() );
+      fprintf (stderr, "%s (%d objects)\n", lfile.c_str(), (int)lr->size() );
 
   return lr;
 }

+ 85 - 0
cbaselib/LocalizationResult.cpp

@@ -357,6 +357,91 @@ void LocalizationResult::restore (istream & is, int format)
 		}
 
 		//sortEmpricalDepth();
+    } else if ( format == FILEFORMAT_POLYGON_SIFTFLOW ) {
+        // parser for xml annotations of SIFTFlow dataset
+        if ( is.good() )
+        {
+            std::string tmp;
+            is >> tmp;      // annotation tag
+
+            bool b_endOfBlock = false;
+
+            while ( !b_endOfBlock )
+            {
+                is >> tmp;  // get current line
+
+                // reached end of file properly
+                if ( this->isEndTag ( tmp, "annotation") )
+                {
+                    b_endOfBlock = true;
+                    continue;
+                }
+
+                StringTools::normalize_string( tmp );
+                tmp = this->removeStartTag ( tmp );
+
+                // found new single localization result
+                if ( tmp.compare("object") == 0 )
+                {
+                    std::string classname;
+                    is >> classname;
+                    classname = classname.substr( 6, classname.length()-13 );  //remove tags
+                    int classno = cn->classnoFromText(classname);
+
+                    bool foundPolygonBlock = false;
+
+                    while ( !foundPolygonBlock )
+                    {
+                        is >> tmp;
+                        StringTools::normalize_string( tmp );
+                        tmp = this->removeStartTag ( tmp );
+
+                        if ( tmp.compare("polygon") == 0 )
+                            foundPolygonBlock = true;
+                    }
+                    is >> tmp;  // 'username' line
+
+                    NICE::Region newPolygon;
+                    bool endOfPolyBlock = false;
+
+                    while ( !endOfPolyBlock )
+                    {
+                        is >> tmp;      // <pt> or </polygon> ?
+                        if ( this->isEndTag ( tmp, "polygon" ) )
+                        {
+                            endOfPolyBlock = true;
+                            continue;
+                        }
+
+                        int x, y;
+
+                        is >> tmp;      // <x> ... </x>
+                        StringTools::normalize_string( tmp );
+                        tmp = tmp.substr( 3, tmp.length()-7 );  //remove tags
+                        x = atoi ( tmp.c_str() );
+
+                        is >> tmp;      // <y> ... </y>
+                        StringTools::normalize_string( tmp );
+                        tmp = tmp.substr( 3, tmp.length()-7 );  //remove tags
+                        y = atoi ( tmp.c_str() );
+
+                        newPolygon.add( x, y );
+
+                        is >> tmp;      // </pt>
+                    }
+                    if ( classno >= 0 ) {
+                        ClassificationResult *r = new ClassificationResult ( classno, 1.0, cn->getMaxClassno() );
+                        SingleLocalizationResult *sr = new SingleLocalizationResult ( r, newPolygon );
+                        push_back ( sr );
+                    }
+                }
+
+            }
+        }
+        else
+        {
+            fthrow(IOException, "LocalizationResult::restore: InStream not initialized !");
+        }
     }
     else {
 		fthrow(IOException, "LocalizationResult::restore: file format not yet supported !");

+ 2 - 1
cbaselib/LocalizationResult.h

@@ -106,7 +106,8 @@ class LocalizationResult : public std::vector<SingleLocalizationResult *>, publi
     enum {
 		FILEFORMAT_PASCAL2006_RESULT = 0,
 		FILEFORMAT_PASCAL2006_GROUNDTRUTH,
-        FILEFORMAT_POLYGON
+        FILEFORMAT_POLYGON,
+        FILEFORMAT_POLYGON_SIFTFLOW
     };
 
     LocalizationResult ( int xsize = -1, int ysize = -1 );