FCReadCache.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @file FCReadCache.cpp
  3. * @author Erik Rodner
  4. * @date 11/15/2007
  5. */
  6. #include <iostream>
  7. #include "vislearning/features/simplefeatures/FCReadCache.h"
  8. #include "vislearning/baselib/Globals.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. // refactor-nice.pl: check this substitution
  12. // old: using namespace ice;
  13. using namespace NICE;
  14. FCReadCache::FCReadCache( const Config * conf ) : FeatureFactory ( conf )
  15. {
  16. cachedir = conf->gS("cache", "root");
  17. cachemode = Globals::getCacheMode ( conf->gS("cache", "mode", "cat") );
  18. extension = conf->gS("cache", "feature_file_ext", "feature" );
  19. cache_debug = conf->gB("cache", "cache_debug", false );
  20. string feature_format_s = conf->gS("cache", "feature_format", "std");
  21. if ( feature_format_s == "std" )
  22. feature_format = FEATURE_FORMAT_STANDARD;
  23. else if ( feature_format_s == "simple" )
  24. feature_format = FEATURE_FORMAT_SIMPLE;
  25. else {
  26. fprintf (stderr, "FCReadCache: feature format %s unknown", feature_format_s.c_str() );
  27. exit(-1);
  28. }
  29. }
  30. FCReadCache::~FCReadCache()
  31. {
  32. }
  33. int FCReadCache::readSimpleText ( ifstream & ifs, NICE::Vector & vec )
  34. {
  35. vec.resize(128);
  36. unsigned int count = 0;
  37. while ( ifs.good() )
  38. {
  39. double value;
  40. if ( ! ( ifs >> value ) ) break;
  41. if ( count >= vec.size() )
  42. vec.resize( 2*vec.size() );
  43. vec[count] = value;
  44. count++;
  45. }
  46. vec.resize(count);
  47. return vec.size();
  48. }
  49. int FCReadCache::convert ( const NICE::Image & img, NICE::Vector & vec )
  50. {
  51. std::string filename = Globals::getCacheFilename ( cachedir, cachemode );
  52. std::string filename_feat = filename + "." + extension;
  53. if ( cache_debug )
  54. fprintf (stderr, "FCReadCache: reading feature from file %s ...\n", filename_feat.c_str() );
  55. ifstream ifs ( filename_feat.c_str(), ios::in );
  56. if ( ! ifs.good() )
  57. {
  58. fprintf (stderr, "FCReadCache: unable to read file %s\n",
  59. filename_feat.c_str());
  60. exit(-1);
  61. }
  62. if ( feature_format == FEATURE_FORMAT_SIMPLE )
  63. readSimpleText ( ifs, vec );
  64. else
  65. ifs >> vec;
  66. ifs.close();
  67. if ( cache_debug )
  68. fprintf (stderr, "FCReadCache: feature successfully read\n" );
  69. return 0;
  70. }