LFWriteCache.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @file LFWriteCache.cpp
  3. * @brief read local features from file
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 02/14/2008
  6. */
  7. #include "core/vector/VectorT.h"
  8. #include "core/vector/MatrixT.h"
  9. #include "core/image/ImageT.h"
  10. // STL includes
  11. #include <iostream>
  12. #include <fstream>
  13. //
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. // nice-core includes
  17. #include <core/basics/StringTools.h>
  18. // nice-vislearning includes
  19. #include <vislearning/baselib/Globals.h>
  20. //
  21. #include "vislearning/features/localfeatures/GenericLFSelection.h"
  22. #include "vislearning/features/localfeatures/LFWriteCache.h"
  23. using namespace OBJREC;
  24. using namespace std;
  25. using namespace NICE;
  26. ///////////////////// ///////////////////// /////////////////////
  27. // PROTECTED METHODS
  28. ///////////////////// ///////////////////// ////////////////
  29. void LFWriteCache::setDescFormat ( const std::string & _descFormat_s )
  30. {
  31. if ( _descFormat_s == "binary_double" )
  32. this->descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
  33. else if ( _descFormat_s == "binary_uchar" )
  34. this->descFormat = VVector::FILEFORMAT_BINARY_CHAR;
  35. else if ( _descFormat_s == "text_line" )
  36. this->descFormat = VVector::FILEFORMAT_LINE;
  37. else if ( ( _descFormat_s == "text_nice" ) || ( _descFormat_s == "text_ice" ) )
  38. this->descFormat = VVector::FILEFORMAT_NICE;
  39. else
  40. fthrow ( Exception, "Format " << _descFormat_s << " is unknown." );
  41. }
  42. ///////////////////// ///////////////////// /////////////////////
  43. // CONSTRUCTORS / DESTRUCTORS
  44. ///////////////////// ///////////////////// /////////////////
  45. LFWriteCache::LFWriteCache ()
  46. {
  47. this->cachedir = "";
  48. this->cachemode = Globals::getCacheMode ( "cat" );
  49. std::string descFormat_s = "binary_double";
  50. this->setDescFormat ( descFormat_s );
  51. }
  52. LFWriteCache::LFWriteCache ( const Config *_conf,
  53. const std::string & _confSection
  54. )
  55. {
  56. this->initFromConfig( _conf, _confSection );
  57. }
  58. LFWriteCache::LFWriteCache ( const Config *conf,
  59. LocalFeatureRepresentation *_lfrep,
  60. const std::string & _section ) : lfrep ( _lfrep )
  61. {
  62. this->cachedir = conf->gS ( _section, "root" );
  63. this->cachemode = Globals::getCacheMode ( conf->gS ( _section, "mode", "cat" ) );
  64. std::string descFormat_s = conf->gS ( _section, "descriptor_format", "binary_double" );
  65. this->setDescFormat ( descFormat_s );
  66. }
  67. LFWriteCache::~LFWriteCache()
  68. {
  69. if ( this->lfrep != NULL )
  70. {
  71. delete this->lfrep;
  72. this->lfrep = NULL;
  73. }
  74. }
  75. void LFWriteCache::initFromConfig(const NICE::Config* _conf, const std::string& _confSection)
  76. {
  77. //srand(time(NULL));
  78. this->cachedir = _conf->gS ( _confSection, "root" );
  79. this->cachemode = Globals::getCacheMode ( _conf->gS ( _confSection, "mode", "cat" ) );
  80. std::string descFormat_s = _conf->gS ( _confSection, "descriptor_format", "binary_double" );
  81. this->setDescFormat ( descFormat_s );
  82. if ( this->lfrep != NULL )
  83. {
  84. delete this->lfrep;
  85. this->lfrep = NULL;
  86. }
  87. this->lfrep = GenericLFSelection::selectLocalFeatureRep ( _conf, _confSection );
  88. }
  89. ///////////////////// ///////////////////// /////////////////////
  90. // FEATURE STUFF
  91. ///////////////////// ///////////////////// /////////////////
  92. int LFWriteCache::getDescSize () const
  93. {
  94. return lfrep->getDescSize();
  95. }
  96. int LFWriteCache::extractFeatures ( const NICE::Image & img,
  97. VVector & features,
  98. VVector & positions ) const
  99. {
  100. std::string filename = Globals::getCacheFilename ( cachedir, cachemode );
  101. int ret = lfrep->extractFeatures ( img, features, positions );
  102. std::string filename_desc = filename + ".desc";
  103. std::string filename_pos = filename + ".key";
  104. struct stat dummy;
  105. if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  106. {
  107. fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
  108. ( int ) positions.size() );
  109. if ( features.size() > 0 )
  110. fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
  111. features.save ( filename_desc, descFormat );
  112. positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  113. } else {
  114. fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  115. }
  116. return ret;
  117. }
  118. int LFWriteCache::extractFeatures ( const NICE::ColorImage & img,
  119. VVector & features,
  120. VVector & positions ) const
  121. {
  122. std::string filename = Globals::getCacheFilename ( cachedir, cachemode );
  123. int ret = lfrep->extractFeatures ( img, features, positions );
  124. std::string filename_desc = filename + ".desc";
  125. std::string filename_pos = filename + ".key";
  126. struct stat dummy;
  127. if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  128. {
  129. fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
  130. ( int ) positions.size() );
  131. if ( features.size() > 0 )
  132. fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
  133. features.save ( filename_desc, descFormat );
  134. positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  135. }
  136. else
  137. {
  138. fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  139. }
  140. return ret;
  141. }
  142. void LFWriteCache::visualize ( NICE::Image & img,
  143. const NICE::Vector & feature ) const
  144. {
  145. this->lfrep->visualize ( img, feature );
  146. }
  147. void LFWriteCache::visualizeFeatures ( NICE::Image & mark,
  148. const VVector & positions,
  149. size_t color ) const
  150. {
  151. this->lfrep->visualizeFeatures ( mark, positions, color );
  152. }
  153. ///////////////////// INTERFACE PERSISTENT /////////////////////
  154. // interface specific methods for store and restore
  155. ///////////////////// INTERFACE PERSISTENT /////////////////////
  156. void LFWriteCache::restore ( std::istream & is, int format )
  157. {
  158. //delete everything we knew so far...
  159. this->clear();
  160. if ( is.good() )
  161. {
  162. std::string tmp;
  163. is >> tmp; //class name
  164. if ( ! this->isStartTag( tmp, "LFWriteCache" ) )
  165. {
  166. std::cerr << " WARNING - attempt to restore LFWriteCache, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  167. throw;
  168. }
  169. bool b_endOfBlock ( false ) ;
  170. while ( !b_endOfBlock )
  171. {
  172. is >> tmp; // start of block
  173. if ( this->isEndTag( tmp, "LFWriteCache" ) )
  174. {
  175. b_endOfBlock = true;
  176. continue;
  177. }
  178. tmp = this->removeStartTag ( tmp );
  179. if ( tmp.compare("lfrep") == 0 )
  180. {
  181. //TODO generic restore function
  182. this->lfrep->restore ( is );
  183. is >> tmp; // end of block
  184. tmp = this->removeEndTag ( tmp );
  185. }
  186. else if ( tmp.compare("cachedir") == 0 )
  187. {
  188. is >> this->cachedir;
  189. is >> tmp; // end of block
  190. tmp = this->removeEndTag ( tmp );
  191. }
  192. else if ( tmp.compare("descFormat") == 0 )
  193. {
  194. is >> this->descFormat;
  195. is >> tmp; // end of block
  196. tmp = this->removeEndTag ( tmp );
  197. }
  198. else if ( tmp.compare("cachemode") == 0 )
  199. {
  200. is >> this->cachemode;
  201. is >> tmp; // end of block
  202. tmp = this->removeEndTag ( tmp );
  203. }
  204. else
  205. {
  206. std::cerr << "WARNING -- unexpected LFWriteCache object -- " << tmp << " -- for restoration... aborting" << std::endl;
  207. throw;
  208. }
  209. }
  210. }
  211. else
  212. {
  213. std::cerr << "LFWriteCache::restore -- InStream not initialized - restoring not possible!" << std::endl;
  214. throw;
  215. }
  216. }
  217. void LFWriteCache::store ( std::ostream & os, int format ) const
  218. {
  219. if (os.good())
  220. {
  221. // show starting point
  222. os << this->createStartTag( "LFWriteCache" ) << std::endl;
  223. os << this->createStartTag( "lfrep" ) << std::endl;
  224. this->lfrep->store ( os );
  225. os << this->createEndTag( "lfrep" ) << std::endl;
  226. os << this->createStartTag( "cachedir" ) << std::endl;
  227. os << this->cachedir << std::endl;
  228. os << this->createEndTag( "cachedir" ) << std::endl;
  229. os << this->createStartTag( "descFormat" ) << std::endl;
  230. os << this->descFormat << std::endl;
  231. os << this->createEndTag( "descFormat" ) << std::endl;
  232. os << this->createStartTag( "cachemode" ) << std::endl;
  233. os << this->cachemode << std::endl;
  234. os << this->createEndTag( "cachemode" ) << std::endl;
  235. // done
  236. os << this->createEndTag( "LFWriteCache" ) << std::endl;
  237. }
  238. else
  239. {
  240. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  241. }
  242. }
  243. void LFWriteCache::clear ()
  244. {
  245. if ( this->lfrep != NULL )
  246. {
  247. delete this->lfrep;
  248. this->lfrep = NULL;
  249. }
  250. }