Quellcode durchsuchen

added restore abilities for class names

Alexander Freytag vor 11 Jahren
Ursprung
Commit
d32fcf0332

+ 92 - 26
cbaselib/ClassNames.cpp

@@ -434,41 +434,107 @@ int ClassNames::getBackgroundClass () const
 
 void ClassNames::restore ( istream & is, int format )
 {
-  maxClassNo = -1;
-  while ( ! is.eof() )
+  this->maxClassNo = -1;
+  
+  if ( is.good() )
   {
-    std::string mytext;
-    std::string mycode;
-    int myclassno;
-
-    if ( ! ( is >> mytext ) ) break;
-    if ( mytext == "end" ) break;
-    if ( ! ( is >> mycode ) ) break;
-    if ( ! ( is >> myclassno ) ) break;
-
-    tbl_code_text.insert ( pair<string, string> ( mycode, mytext ) );
-    tbl_text_code.insert ( pair<string, string> ( mytext, mycode ) );
-    tbl_classno_code.insert ( pair<int, string> ( myclassno, mycode ) );
-    tbl_code_classno.insert ( pair<string, int> ( mycode, myclassno ) );
-
-    if ( myclassno > maxClassNo ) maxClassNo = myclassno;
+    
+    std::string tmp;
+    is >> tmp; //class name 
+    
+    bool b_endOfBlock ( false ) ;
+    
+    while ( !b_endOfBlock )
+    {
+      is >> tmp; // start of block 
+      
+      if ( this->isEndTag( tmp, "ClassNames" ) )
+      {
+        b_endOfBlock = true;
+        continue;
+      }
+      
+      tmp = this->removeStartTag ( tmp );    
+      if ( tmp.compare("content") == 0 )
+      {
+        is >> tmp; // size:
+        int mySize ( 0 );
+        is >> mySize;
+        this->tbl_code_text.clear();
+        this->tbl_text_code.clear();
+        this->tbl_classno_code.clear();
+        this->tbl_code_classno.clear();
+        
+        for ( int i = 0; i < mySize; i++ )
+        {
+          std::string mytext;
+          is >> mytext;
+          std::string mycode;
+          is >> mycode;
+          int myclassno;
+          is >> myclassno;
+          
+          this->tbl_code_text.insert    ( std::pair<std::string, std::string> ( mycode, mytext ) );
+          this->tbl_text_code.insert    ( std::pair<std::string, std::string> ( mytext, mycode ) );
+          this->tbl_classno_code.insert ( std::pair<int, std::string> ( myclassno, mycode ) );
+          this->tbl_code_classno.insert ( std::pair<std::string, int> ( mycode, myclassno ) ); 
+          
+          if ( myclassno > this->maxClassNo )
+            this->maxClassNo = myclassno;
+        }
+        
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );        
+      }
+      else
+      {
+      std::cerr << "WARNING -- unexpected ClassNames object -- " << tmp << " -- for restoration... aborting" << std::endl;
+      throw;
+      }
+    }      
   }
+  else
+  {
+    std::cerr << "ClassNames::restore -- InStream not initialized - restoring not possible!" << std::endl;
+    throw;
+  }  
 }
 
 void ClassNames::store ( ostream & os, int format ) const
 {
   assert ( tbl_classno_code.size() == tbl_code_classno.size() );
-  for ( map<int, string>::const_iterator i  = tbl_classno_code.begin() ;
-        i != tbl_classno_code.end();
-        i++ )
+  
+  if (os.good())
   {
-    int myclassno = i->first;
-    std::string mycode = i->second;
-    std::string mytext = text ( myclassno );
-
-    os << mytext << "\t" << mycode << "\t" << myclassno << endl;
+    // show starting point
+    os << this->createStartTag( "ClassNames" ) << std::endl;    
+    
+    os.precision (numeric_limits<double>::digits10 + 1);
+    
+    os << this->createStartTag( "content" ) << std::endl;
+    os << "size: " << this->tbl_classno_code.size() << std::endl;
+    
+    for ( std::map< int, std::string >::const_iterator iIt = this->tbl_classno_code.begin();
+          iIt != this->tbl_classno_code.end();
+          iIt++
+        )
+    {
+      int myclassno = iIt->first;
+      std::string mycode = iIt->second;
+      std::string mytext = text ( myclassno ); 
+      
+      os << mytext << " " << mycode << " " << myclassno << endl;
+    }
+    os << this->createEndTag( "content" ) << std::endl; 
+    
+    // done
+    os << this->createEndTag( "ClassNames" ) << std::endl;      
   }
-  os << "end" << endl;
+  else
+  {
+    std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
+  }  
+
 }
 
 void ClassNames::clear ()

+ 2 - 2
cbaselib/ClassNames.h

@@ -1,8 +1,8 @@
 /**
 * @file ClassNames.h
 * @brief simple interface for class name confusion
-* @author Erik Rodner
-* @date 02/08/2008
+* @author Erik Rodner, Alexander Freytag
+* @date 02/08/2008, latest update 29-01-2014 (dd-mm-yyyy)
 
 */
 #ifndef CLASSNAMESINCLUDE

+ 13 - 0
cbaselib/MultiDataset.cpp

@@ -385,6 +385,19 @@ const ClassNames & MultiDataset::getClassNames ( const std::string & key ) const
 
 }
 
+ClassNames & MultiDataset::getClassNamesNonConst ( const std::string & key )
+{
+  std::map<string, ClassNames>::iterator i = classnames.find(key);
+  if ( i == classnames.end() )
+  {
+    fprintf (stderr, "MultiDataSet::getClassNamesNonConst() FATAL ERROR: dataset <%s> not found !\n", key.c_str() );
+    exit(-1);
+  }
+  return (i->second);
+}
+
+
+
 const LabeledSet *MultiDataset::operator[] ( const std::string & key ) const
 {
   map<string, LabeledSet>::const_iterator i = datasets.find(key);

+ 2 - 0
cbaselib/MultiDataset.h

@@ -54,6 +54,8 @@ protected:
     
     /** access class information, e.g., md.getClassNames("train") */
     const ClassNames & getClassNames ( const std::string & key ) const;
+    
+    ClassNames & getClassNamesNonConst ( const std::string & key );
 
     /** access stored dataset by name, e.g., md["train"], if you have a [train] section
       * in your config file */

+ 1 - 1
features/localfeatures/LocalFeatureColorWeijer.cpp

@@ -41,7 +41,7 @@ LocalFeatureColorWeijer::LocalFeatureColorWeijer( const Config *c )
   
   // If no destination  to the LUT was given, we try to use the file shipped with this library
   // Therefore, we try to catch the NICEHOME and if possible, append the location where the LUT file is stored
-  // NOTE This will only work if NICEHOME is set in your environment (e.g., by exporting it in your bashrc)
+  // NOTE This will only work if NICEHOME is set in your environment (e.g., by exporting it in your bashrc or sourcing the setenv.sh)
   if ( this->tfile.compare("") == 0 )
   {
     char* pNICEHOME;