123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #include "RSCache.h"
- #include <iostream>
- #include <fstream>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include "vislearning/baselib/Globals.h"
- #include "core/basics/StringTools.h"
- #include "core/basics/ossettings.h"
- #include <unistd.h>
- using namespace OBJREC;
- using namespace NICE;
- using namespace std;
- RSCache::RSCache ( const Config *conf, RegionSegmentationMethod *_rsmethod )
- {
- rsmethod = _rsmethod;
- cachedir = conf->gS ( "cache", "root", "/tmp/" );
- cachedir += "/regionsegmentation";
- forcedwrite = conf->gB ( "RSCache", "forcedwrite", false );
- // Verzeichnis anlegen
- struct stat info;
- if ( stat ( cachedir.c_str(), &info ) < 0 )
- {
- if ( mkdir ( cachedir.c_str(), 0755 ) < 0 )
- {
- cerr << "RSCache:: unable to create directory: " << cachedir << endl;
- exit ( -1 );
- }
- }
- }
- RSCache::~RSCache()
- {
- }
- int RSCache::segRegions ( const NICE::Image & img, NICE::Matrix & mask ) const
- {
- std::string filename = Globals::getCurrentImgFN();
- vector<string> mylistdir;
- // Zerlege Dateipfad (alles zwischen den /)
- StringTools::split ( filename, FILESEP, mylistdir );
- if ( mylistdir.size() < 2 )
- {
- cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
- exit ( -1 );
- }
- std::string name = mylistdir[mylistdir.size()-1];
- vector<string> mylist;
- // Zerlege den Dateinamen in Namen und Endung
- StringTools::split ( name, '.', mylist );
- name = "";
- for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
- if ( k == 0 )
- name = name + mylist[k];
- else
- name = name + "." + mylist[k];
- filename = cachedir + FILESEPSTRING + name;
- std::string imgname = filename+".ppm";
- filename += ".mask";
- int cn = -1;
- struct stat dummy;
- if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
- {
- cn = rsmethod->segRegions ( img, mask );
- ofstream fout ( filename.c_str(), ios::out );
- fout << cn << endl;
- fout << mask;
- fout.close();
- }
- else
- {
- // read from file
- ifstream fin ( filename.c_str() );
- fin >> cn;
- fin >> mask;
- fin.close();
- }
- return cn;
- }
- int RSCache::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const
- {
- std::string filename = Globals::getCurrentImgFN();
- vector<string> mylistdir;
- // Zerlege Dateipfad (alles zwischen den /)
- StringTools::split ( filename, FILESEP, mylistdir );
- if ( mylistdir.size() < 2 )
- {
- cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
- exit ( -1 );
- }
- std::string name = mylistdir[mylistdir.size()-1];
- vector<string> mylist;
- // Zerlege den Dateinamen in Namen und Endung
- StringTools::split ( name, '.', mylist );
- name = "";
- for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
- if ( k == 0 )
- name = name + mylist[k];
- else
- name = name + "." + mylist[k];
- filename = cachedir + FILESEPSTRING + name;
- std::string imgname = filename+".ppm";
- filename += ".mask";
- int cn = -1;
- struct stat dummy;
- if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
- {
- cn = rsmethod->segRegions ( img, mask );
- ofstream fout ( filename.c_str(), ios::out );
- fout << cn << endl;
- fout << mask;
- fout.close();
- }
- else
- {
- // read from file
- ifstream fin ( filename.c_str() );
- fin >> cn;
- fin >> mask;
- fin.close();
- }
- return cn;
- }
- ///////////////////// INTERFACE PERSISTENT /////////////////////
- // interface specific methods for store and restore
- ///////////////////// INTERFACE PERSISTENT /////////////////////
- void RSCache::restore ( std::istream & is, int format )
- {
- //delete everything we knew so far...
- this->clear();
-
-
- if ( is.good() )
- {
-
- std::string tmp;
- is >> tmp; //class name
-
- if ( ! this->isStartTag( tmp, "RSCache" ) )
- {
- std::cerr << " WARNING - attempt to restore RSCache, but start flag " << tmp << " does not match! Aborting... " << std::endl;
- throw;
- }
-
- bool b_endOfBlock ( false ) ;
-
- while ( !b_endOfBlock )
- {
- is >> tmp; // start of block
-
- if ( this->isEndTag( tmp, "RSCache" ) )
- {
- b_endOfBlock = true;
- continue;
- }
-
- tmp = this->removeStartTag ( tmp );
-
- if ( tmp.compare("") == 0 )
- {
- //TODO
- //is >> minimumRegionArea;
- is >> tmp; // end of block
- tmp = this->removeEndTag ( tmp );
- }
- else
- {
- std::cerr << "WARNING -- unexpected RSCache object -- " << tmp << " -- for restoration... aborting" << std::endl;
- throw;
- }
- }
- }
- else
- {
- std::cerr << "RSCache::restore -- InStream not initialized - restoring not possible!" << std::endl;
- throw;
- }
- }
- void RSCache::store ( std::ostream & os, int format ) const
- {
- if (os.good())
- {
- // show starting point
- os << this->createStartTag( "RSCache" ) << std::endl;
-
- //TODO
-
- // done
- os << this->createEndTag( "RSCache" ) << std::endl;
- }
- else
- {
- std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
- }
- }
- void RSCache::clear ()
- {
- //TODO
- }
|