#include "RSCache.h" #include #include #include #include #include "vislearning/baselib/Globals.h" #include "core/basics/StringTools.h" #include "core/basics/ossettings.h" #include 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 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 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 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 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 }