FCReadCache.cpp 2.2 KB

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