RSCache.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "RSCache.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include "vislearning/baselib/Globals.h"
  7. #include "core/basics/StringTools.h"
  8. #include "core/basics/ossettings.h"
  9. #include <unistd.h>
  10. using namespace OBJREC;
  11. using namespace NICE;
  12. using namespace std;
  13. RSCache::RSCache ( const Config *conf, RegionSegmentationMethod *_rsmethod )
  14. {
  15. rsmethod = _rsmethod;
  16. cachedir = conf->gS ( "cache", "root", "/tmp/" );
  17. cachedir += "/regionsegmentation";
  18. forcedwrite = conf->gB ( "RSCache", "forcedwrite", false );
  19. // Verzeichnis anlegen
  20. struct stat info;
  21. if ( stat ( cachedir.c_str(), &info ) < 0 )
  22. {
  23. if ( mkdir ( cachedir.c_str(), 0755 ) < 0 )
  24. {
  25. cerr << "RSCache:: unable to create directory: " << cachedir << endl;
  26. exit ( -1 );
  27. }
  28. }
  29. }
  30. RSCache::~RSCache()
  31. {
  32. }
  33. int RSCache::segRegions ( const NICE::Image & img, NICE::Matrix & mask ) const
  34. {
  35. std::string filename = Globals::getCurrentImgFN();
  36. vector<string> mylistdir;
  37. // Zerlege Dateipfad (alles zwischen den /)
  38. StringTools::split ( filename, FILESEP, mylistdir );
  39. if ( mylistdir.size() < 2 )
  40. {
  41. cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
  42. exit ( -1 );
  43. }
  44. std::string name = mylistdir[mylistdir.size()-1];
  45. vector<string> mylist;
  46. // Zerlege den Dateinamen in Namen und Endung
  47. StringTools::split ( name, '.', mylist );
  48. name = "";
  49. for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
  50. if ( k == 0 )
  51. name = name + mylist[k];
  52. else
  53. name = name + "." + mylist[k];
  54. filename = cachedir + FILESEPSTRING + name;
  55. std::string imgname = filename+".ppm";
  56. filename += ".mask";
  57. int cn = -1;
  58. struct stat dummy;
  59. if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
  60. {
  61. cn = rsmethod->segRegions ( img, mask );
  62. ofstream fout ( filename.c_str(), ios::out );
  63. fout << cn << endl;
  64. fout << mask;
  65. fout.close();
  66. }
  67. else
  68. {
  69. // read from file
  70. ifstream fin ( filename.c_str() );
  71. fin >> cn;
  72. fin >> mask;
  73. fin.close();
  74. }
  75. return cn;
  76. }
  77. int RSCache::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const
  78. {
  79. std::string filename = Globals::getCurrentImgFN();
  80. vector<string> mylistdir;
  81. // Zerlege Dateipfad (alles zwischen den /)
  82. StringTools::split ( filename, FILESEP, mylistdir );
  83. if ( mylistdir.size() < 2 )
  84. {
  85. cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
  86. exit ( -1 );
  87. }
  88. std::string name = mylistdir[mylistdir.size()-1];
  89. vector<string> mylist;
  90. // Zerlege den Dateinamen in Namen und Endung
  91. StringTools::split ( name, '.', mylist );
  92. name = "";
  93. for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
  94. if ( k == 0 )
  95. name = name + mylist[k];
  96. else
  97. name = name + "." + mylist[k];
  98. filename = cachedir + FILESEPSTRING + name;
  99. std::string imgname = filename+".ppm";
  100. filename += ".mask";
  101. int cn = -1;
  102. struct stat dummy;
  103. if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
  104. {
  105. cn = rsmethod->segRegions ( img, mask );
  106. ofstream fout ( filename.c_str(), ios::out );
  107. fout << cn << endl;
  108. fout << mask;
  109. fout.close();
  110. }
  111. else
  112. {
  113. // read from file
  114. ifstream fin ( filename.c_str() );
  115. fin >> cn;
  116. fin >> mask;
  117. fin.close();
  118. }
  119. return cn;
  120. }
  121. ///////////////////// INTERFACE PERSISTENT /////////////////////
  122. // interface specific methods for store and restore
  123. ///////////////////// INTERFACE PERSISTENT /////////////////////
  124. void RSCache::restore ( std::istream & is, int format )
  125. {
  126. //delete everything we knew so far...
  127. this->clear();
  128. if ( is.good() )
  129. {
  130. std::string tmp;
  131. is >> tmp; //class name
  132. if ( ! this->isStartTag( tmp, "RSCache" ) )
  133. {
  134. std::cerr << " WARNING - attempt to restore RSCache, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  135. throw;
  136. }
  137. bool b_endOfBlock ( false ) ;
  138. while ( !b_endOfBlock )
  139. {
  140. is >> tmp; // start of block
  141. if ( this->isEndTag( tmp, "RSCache" ) )
  142. {
  143. b_endOfBlock = true;
  144. continue;
  145. }
  146. tmp = this->removeStartTag ( tmp );
  147. if ( tmp.compare("") == 0 )
  148. {
  149. //TODO
  150. //is >> minimumRegionArea;
  151. is >> tmp; // end of block
  152. tmp = this->removeEndTag ( tmp );
  153. }
  154. else
  155. {
  156. std::cerr << "WARNING -- unexpected RSCache object -- " << tmp << " -- for restoration... aborting" << std::endl;
  157. throw;
  158. }
  159. }
  160. }
  161. else
  162. {
  163. std::cerr << "RSCache::restore -- InStream not initialized - restoring not possible!" << std::endl;
  164. throw;
  165. }
  166. }
  167. void RSCache::store ( std::ostream & os, int format ) const
  168. {
  169. if (os.good())
  170. {
  171. // show starting point
  172. os << this->createStartTag( "RSCache" ) << std::endl;
  173. //TODO
  174. // done
  175. os << this->createEndTag( "RSCache" ) << std::endl;
  176. }
  177. else
  178. {
  179. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  180. }
  181. }
  182. void RSCache::clear ()
  183. {
  184. //TODO
  185. }