|
@@ -1,4 +1,4 @@
|
|
|
-/**
|
|
|
+/**
|
|
|
* @file LabeledFileList.cpp
|
|
|
* @brief reads images from directory
|
|
|
* @author Erik Rodner
|
|
@@ -22,9 +22,9 @@ using namespace OBJREC;
|
|
|
using namespace std;
|
|
|
using namespace NICE;
|
|
|
|
|
|
-LabeledFileList::LabeledFileList()
|
|
|
+LabeledFileList::LabeledFileList()
|
|
|
{
|
|
|
- debug_dataset = false;
|
|
|
+ debug_dataset = false;
|
|
|
}
|
|
|
|
|
|
LabeledFileList::~LabeledFileList()
|
|
@@ -33,247 +33,247 @@ LabeledFileList::~LabeledFileList()
|
|
|
|
|
|
|
|
|
LocalizationResult *LabeledFileList::getLocalizationInfo ( const ClassNames & classnames,
|
|
|
- int classno,
|
|
|
- const std::string & file,
|
|
|
- const Config & conf ) const
|
|
|
+ int classno,
|
|
|
+ const std::string & file,
|
|
|
+ const Config & conf ) const
|
|
|
{
|
|
|
- /*
|
|
|
- localization_pattern = image
|
|
|
- localization_subst = mask
|
|
|
- localization_format = image
|
|
|
- */
|
|
|
- std::string format = conf.gS("main", "localization_format", "unknown");
|
|
|
- if ( format == "unknown" )
|
|
|
- return NULL;
|
|
|
-
|
|
|
- std::string pattern = conf.gS("main", "localization_pattern" );
|
|
|
- std::string subst = conf.gS("main", "localization_subst" );
|
|
|
-
|
|
|
- std::string lfile = file;
|
|
|
- if ( ! StringTools::regexSubstitute ( lfile, pattern, subst ) )
|
|
|
- {
|
|
|
- fprintf (stderr, "Unable to substitute using pattern #%s# and string #%s#\n",
|
|
|
- pattern.c_str(), lfile.c_str() );
|
|
|
- exit(-1);
|
|
|
+ /*
|
|
|
+ localization_pattern = image
|
|
|
+ localization_subst = mask
|
|
|
+ localization_format = image
|
|
|
+ */
|
|
|
+ std::string format = conf.gS ( "main", "localization_format", "unknown" );
|
|
|
+ if ( format == "unknown" )
|
|
|
+ return NULL;
|
|
|
+
|
|
|
+ std::string pattern = conf.gS ( "main", "localization_pattern" );
|
|
|
+ std::string subst = conf.gS ( "main", "localization_subst" );
|
|
|
+
|
|
|
+ std::string lfile = file;
|
|
|
+ if ( ! StringTools::regexSubstitute ( lfile, pattern, subst ) )
|
|
|
+ {
|
|
|
+ fprintf ( stderr, "Unable to substitute using pattern #%s# and string #%s#\n",
|
|
|
+ pattern.c_str(), lfile.c_str() );
|
|
|
+ exit ( -1 );
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( ! FileMgt::fileExists ( lfile ) ) return NULL;
|
|
|
+ if ( debug_dataset )
|
|
|
+ {
|
|
|
+ fprintf ( stderr, "LabeledFileList: reading localization information %s\n", lfile.c_str() );
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalizationResult *lr = NULL;
|
|
|
+
|
|
|
+ if ( format == "image" )
|
|
|
+ {
|
|
|
+ NICE::Image mask;
|
|
|
+ try {
|
|
|
+ mask.read ( lfile );
|
|
|
+ } catch ( ImageException & ) {
|
|
|
+ fprintf ( stderr, "WARNING: unable to open file %s (no localization info provided)\n",
|
|
|
+ lfile.c_str() );
|
|
|
+ return NULL;
|
|
|
}
|
|
|
|
|
|
- if ( ! FileMgt::fileExists(lfile) ) return NULL;
|
|
|
- if ( debug_dataset )
|
|
|
- {
|
|
|
- fprintf (stderr, "LabeledFileList: reading localization information %s\n", lfile.c_str() );
|
|
|
+ lr = new LocalizationResult ( &classnames, mask, classno );
|
|
|
+
|
|
|
+ } else if ( format == "imagergb" ) {
|
|
|
+ NICE::ColorImage mask;
|
|
|
+ try {
|
|
|
+ mask.read ( lfile );
|
|
|
+ } catch ( ImageException &e ) {
|
|
|
+ fprintf ( stderr, "WARNING: unable to open file %s (no localization info provided)\n",
|
|
|
+ lfile.c_str() );
|
|
|
+ fprintf ( stderr, "Error: %s\n", e.what() );
|
|
|
+ return NULL;
|
|
|
}
|
|
|
+ lr = new LocalizationResult ( &classnames, mask );
|
|
|
|
|
|
- LocalizationResult *lr = NULL;
|
|
|
+ } else if ( format == "polygon" ) {
|
|
|
+ lr = new LocalizationResult ( &classnames );
|
|
|
|
|
|
- if ( format == "image" )
|
|
|
- {
|
|
|
- NICE::Image mask;
|
|
|
- try {
|
|
|
- mask.read(lfile);
|
|
|
- } catch (ImageException &) {
|
|
|
- fprintf (stderr, "WARNING: unable to open file %s (no localization info provided)\n",
|
|
|
- lfile.c_str() );
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
- lr = new LocalizationResult ( &classnames, mask, classno );
|
|
|
-
|
|
|
- } else if ( format == "imagergb" ) {
|
|
|
- NICE::ColorImage mask;
|
|
|
- try {
|
|
|
- mask.read( lfile );
|
|
|
- } catch (ImageException &e) {
|
|
|
- fprintf (stderr, "WARNING: unable to open file %s (no localization info provided)\n",
|
|
|
- lfile.c_str() );
|
|
|
- fprintf (stderr, "Error: %s\n", e.what() );
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- lr = new LocalizationResult ( &classnames, mask );
|
|
|
-
|
|
|
- } else if ( format == "polygon" ) {
|
|
|
- lr = new LocalizationResult ( &classnames );
|
|
|
-
|
|
|
- lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON );
|
|
|
-
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
|
|
|
- } else {
|
|
|
- fthrow(Exception, "Localization format not yet supported !!\n");
|
|
|
- }
|
|
|
-
|
|
|
- if ( debug_dataset )
|
|
|
- if ( lr != NULL )
|
|
|
- fprintf (stderr, "%s (%d objects)\n", lfile.c_str(), (int)lr->size() );
|
|
|
-
|
|
|
- return lr;
|
|
|
+ lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON );
|
|
|
+
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "LabeledFileList: object localization %d\n", ( int ) lr->size() );
|
|
|
+ } else {
|
|
|
+ fthrow ( Exception, "Localization format not yet supported !!\n" );
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( debug_dataset )
|
|
|
+ if ( lr != NULL )
|
|
|
+ fprintf ( stderr, "%s (%d objects)\n", lfile.c_str(), ( int ) lr->size() );
|
|
|
+
|
|
|
+ return lr;
|
|
|
}
|
|
|
|
|
|
void LabeledFileList::getFromPattern (
|
|
|
- const std::string & dir,
|
|
|
- const Config & datasetconf,
|
|
|
- const ClassNames & classnames,
|
|
|
- LabeledSet & ls,
|
|
|
- bool localizationInfoDisabled ) const
|
|
|
+ const std::string & dir,
|
|
|
+ const Config & datasetconf,
|
|
|
+ const ClassNames & classnames,
|
|
|
+ LabeledSet & ls,
|
|
|
+ bool localizationInfoDisabled ) const
|
|
|
{
|
|
|
- std::string filemask;
|
|
|
-
|
|
|
- if ( dir.substr(dir.length()-1,1) != "/" )
|
|
|
- filemask = dir + "/" + datasetconf.gS("main", "pattern");
|
|
|
- else
|
|
|
- filemask = dir + datasetconf.gS("main", "pattern");
|
|
|
-
|
|
|
- std::vector<string> files;
|
|
|
-
|
|
|
- int classnameField = datasetconf.gI("main", "classname_field", 1);
|
|
|
- std::string fixedClassname = datasetconf.gS("main", "fixed_classname", "");
|
|
|
-
|
|
|
- files.clear();
|
|
|
- FileMgt::DirectoryRecursive ( files, dir );
|
|
|
- fprintf (stderr, "LabeledFileList: Files: %d\n", (int)files.size());
|
|
|
-
|
|
|
- sort ( files.begin(), files.end() );
|
|
|
-
|
|
|
- for ( vector<string>::const_iterator i = files.begin();
|
|
|
- i != files.end();
|
|
|
- i++ )
|
|
|
+ std::string filemask;
|
|
|
+
|
|
|
+ if ( dir.substr ( dir.length() - 1, 1 ) != "/" )
|
|
|
+ filemask = dir + "/" + datasetconf.gS ( "main", "pattern" );
|
|
|
+ else
|
|
|
+ filemask = dir + datasetconf.gS ( "main", "pattern" );
|
|
|
+
|
|
|
+ std::vector<string> files;
|
|
|
+
|
|
|
+ int classnameField = datasetconf.gI ( "main", "classname_field", 1 );
|
|
|
+ std::string fixedClassname = datasetconf.gS ( "main", "fixed_classname", "" );
|
|
|
+
|
|
|
+ files.clear();
|
|
|
+ FileMgt::DirectoryRecursive ( files, dir );
|
|
|
+ fprintf ( stderr, "LabeledFileList: Files: %d\n", ( int ) files.size() );
|
|
|
+
|
|
|
+ sort ( files.begin(), files.end() );
|
|
|
+
|
|
|
+ for ( vector<string>::const_iterator i = files.begin();
|
|
|
+ i != files.end();
|
|
|
+ i++ )
|
|
|
+ {
|
|
|
+ vector<string> submatches;
|
|
|
+ // refactor-nice.pl: check this substitution
|
|
|
+ // old: const string & file = *i;
|
|
|
+ const std::string & file = *i;
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "LabeledFileList: next file: %s\n", file.c_str() );
|
|
|
+
|
|
|
+ bool match = StringTools::regexMatch ( file, filemask, submatches );
|
|
|
+
|
|
|
+ if ( ( fixedClassname == "" ) && ( ( int ) submatches.size() <= classnameField ) ) match = false;
|
|
|
+
|
|
|
+ if ( ! match )
|
|
|
{
|
|
|
- vector<string> submatches;
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: const string & file = *i;
|
|
|
- const std::string & file = *i;
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "LabeledFileList: next file: %s\n", file.c_str() );
|
|
|
-
|
|
|
- bool match = StringTools::regexMatch ( file, filemask, submatches );
|
|
|
-
|
|
|
- if ( (fixedClassname == "") && ((int)submatches.size() <= classnameField) ) match = false;
|
|
|
-
|
|
|
- if ( ! match )
|
|
|
- {
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "LabeledFileList: WARNING: %s does not match filemask: %s!!\n", file.c_str(), filemask.c_str() );
|
|
|
- } else {
|
|
|
- std::string classcode = ( fixedClassname == "" ) ? submatches[classnameField] : fixedClassname;
|
|
|
-
|
|
|
- if ( classnames.existsClassCode(classcode) ) {
|
|
|
- int classno = classnames.classno(classcode);
|
|
|
- LocalizationResult *lr = NULL;
|
|
|
-
|
|
|
- if ( ! localizationInfoDisabled )
|
|
|
- lr = getLocalizationInfo (
|
|
|
- classnames, classno, file, datasetconf);
|
|
|
-
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "LabeledFileList: LabeledSet: add %s (%d)\n", file.c_str(), classno );
|
|
|
- if ( lr == NULL )
|
|
|
- {
|
|
|
- ls.add ( classno, new ImageInfo(file) );
|
|
|
- } else {
|
|
|
- ls.add ( classno, new ImageInfo(file, lr) );
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "LabeledFileList: LocalizationResult added!\n");
|
|
|
-
|
|
|
- }
|
|
|
- } else {
|
|
|
- if ( debug_dataset )
|
|
|
- {
|
|
|
- for ( vector<string>::iterator i = submatches.begin();
|
|
|
- i != submatches.end();
|
|
|
- i++ )
|
|
|
- {
|
|
|
- fprintf (stderr, "LabeledFileList: submatch: %s\n", i->c_str() );
|
|
|
- }
|
|
|
- fprintf (stderr, "LabeledFileList: WARNING: code %s ignored !\n", classcode.c_str() );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "LabeledFileList: filename processed\n");
|
|
|
- }
|
|
|
-
|
|
|
- cerr << "directory " << dir << " loaded..." << endl;
|
|
|
- ls.printInformation();
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "LabeledFileList: WARNING: %s does not match filemask: %s!!\n", file.c_str(), filemask.c_str() );
|
|
|
+ } else {
|
|
|
+ std::string classcode = ( fixedClassname == "" ) ? submatches[classnameField] : fixedClassname;
|
|
|
+
|
|
|
+ if ( classnames.existsClassCode ( classcode ) ) {
|
|
|
+ int classno = classnames.classno ( classcode );
|
|
|
+ LocalizationResult *lr = NULL;
|
|
|
+
|
|
|
+ if ( ! localizationInfoDisabled )
|
|
|
+ lr = getLocalizationInfo (
|
|
|
+ classnames, classno, file, datasetconf );
|
|
|
+
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "LabeledFileList: LabeledSet: add %s (%d)\n", file.c_str(), classno );
|
|
|
+ if ( lr == NULL )
|
|
|
+ {
|
|
|
+ ls.add ( classno, new ImageInfo ( file ) );
|
|
|
+ } else {
|
|
|
+ ls.add ( classno, new ImageInfo ( file, lr ) );
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "LabeledFileList: LocalizationResult added!\n" );
|
|
|
+
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ( debug_dataset )
|
|
|
+ {
|
|
|
+ for ( vector<string>::iterator i = submatches.begin();
|
|
|
+ i != submatches.end();
|
|
|
+ i++ )
|
|
|
+ {
|
|
|
+ fprintf ( stderr, "LabeledFileList: submatch: %s\n", i->c_str() );
|
|
|
+ }
|
|
|
+ fprintf ( stderr, "LabeledFileList: WARNING: code %s ignored !\n", classcode.c_str() );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "LabeledFileList: filename processed\n" );
|
|
|
+ }
|
|
|
+
|
|
|
+ cerr << "directory " << dir << " loaded..." << endl;
|
|
|
+ ls.printInformation();
|
|
|
|
|
|
}
|
|
|
|
|
|
void LabeledFileList::getFromList (
|
|
|
- const std::string & filelist,
|
|
|
- const Config & datasetconf,
|
|
|
- const ClassNames & classnames,
|
|
|
- LabeledSet & ls,
|
|
|
- bool localizationInfoDisabled) const
|
|
|
+ const std::string & filelist,
|
|
|
+ const Config & datasetconf,
|
|
|
+ const ClassNames & classnames,
|
|
|
+ LabeledSet & ls,
|
|
|
+ bool localizationInfoDisabled ) const
|
|
|
{
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "Reading file list: %s\n", filelist.c_str() );
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "Reading file list: %s\n", filelist.c_str() );
|
|
|
|
|
|
- ifstream ifs ( filelist.c_str(), ios::in );
|
|
|
- if ( ! ifs.good() )
|
|
|
- fthrow(IOException, "File list " << filelist << " not found !");
|
|
|
-
|
|
|
- std::string fixedClassname = datasetconf.gS("main", "fixed_classname", "");
|
|
|
+ ifstream ifs ( filelist.c_str(), ios::in );
|
|
|
+ if ( ! ifs.good() )
|
|
|
+ fthrow ( IOException, "File list " << filelist << " not found !" );
|
|
|
|
|
|
- while ( ! ifs.eof() )
|
|
|
- {
|
|
|
- std::string classcode;
|
|
|
- std::string file;
|
|
|
-
|
|
|
- if ( fixedClassname == "" ) {
|
|
|
- if ( ! (ifs >> classcode) ) break;
|
|
|
- } else {
|
|
|
- classcode = fixedClassname;
|
|
|
- }
|
|
|
-
|
|
|
- if ( ! (ifs >> file) ) break;
|
|
|
-
|
|
|
- if ( classnames.existsClassCode(classcode) ) {
|
|
|
- int classno = classnames.classno(classcode);
|
|
|
-
|
|
|
- LocalizationResult *lr = NULL;
|
|
|
-
|
|
|
- if ( ! localizationInfoDisabled )
|
|
|
- lr = getLocalizationInfo ( classnames, classno, file, datasetconf);
|
|
|
-
|
|
|
- if ( debug_dataset )
|
|
|
- cerr << "Adding file " << file << " with classno " << classno << endl;
|
|
|
-
|
|
|
- if ( lr == NULL )
|
|
|
- ls.add ( classno, new ImageInfo(file) );
|
|
|
- else
|
|
|
- ls.add ( classno, new ImageInfo(file, lr) );
|
|
|
- } else {
|
|
|
- if ( debug_dataset )
|
|
|
- fprintf (stderr, "WARNING: code %s ignored !\n", classcode.c_str() );
|
|
|
- }
|
|
|
+ std::string fixedClassname = datasetconf.gS ( "main", "fixed_classname", "" );
|
|
|
+
|
|
|
+ while ( ! ifs.eof() )
|
|
|
+ {
|
|
|
+ std::string classcode;
|
|
|
+ std::string file;
|
|
|
|
|
|
+ if ( fixedClassname == "" ) {
|
|
|
+ if ( ! ( ifs >> classcode ) ) break;
|
|
|
+ } else {
|
|
|
+ classcode = fixedClassname;
|
|
|
}
|
|
|
|
|
|
- if ( debug_dataset )
|
|
|
- ls.printInformation();
|
|
|
-}
|
|
|
+ if ( ! ( ifs >> file ) ) break;
|
|
|
|
|
|
+ if ( classnames.existsClassCode ( classcode ) ) {
|
|
|
+ int classno = classnames.classno ( classcode );
|
|
|
|
|
|
-void LabeledFileList::get (
|
|
|
- const std::string & dir,
|
|
|
- const Config & datasetconf,
|
|
|
- const ClassNames & classnames,
|
|
|
- LabeledSet & ls,
|
|
|
- bool localizationInfoDisabled,
|
|
|
- bool debugDataset )
|
|
|
-{
|
|
|
- std::string pattern = datasetconf.gS("main", "pattern", "");
|
|
|
- std::string filelist = datasetconf.gS("main", "filelist", "");
|
|
|
- this->debug_dataset = debugDataset;
|
|
|
+ LocalizationResult *lr = NULL;
|
|
|
|
|
|
- if ( pattern.size() > 0 )
|
|
|
- getFromPattern ( dir, datasetconf, classnames, ls, localizationInfoDisabled );
|
|
|
- else if ( filelist.size() > 0 ) {
|
|
|
+ if ( ! localizationInfoDisabled )
|
|
|
+ lr = getLocalizationInfo ( classnames, classno, file, datasetconf );
|
|
|
|
|
|
- std::string cfilelist = datasetconf.gS("main", "filelist");
|
|
|
- std::string filelist = ( cfilelist.substr(0,1) == "/" ) ? cfilelist : dir + "/" + cfilelist;
|
|
|
+ if ( debug_dataset )
|
|
|
+ cerr << "Adding file " << file << " with classno " << classno << endl;
|
|
|
|
|
|
- getFromList ( filelist, datasetconf, classnames, ls, localizationInfoDisabled );
|
|
|
+ if ( lr == NULL )
|
|
|
+ ls.add ( classno, new ImageInfo ( file ) );
|
|
|
+ else
|
|
|
+ ls.add ( classno, new ImageInfo ( file, lr ) );
|
|
|
} else {
|
|
|
- fprintf (stderr, "LabeledFileList: Unable to obtain labeled file list\n");
|
|
|
- exit(-1);
|
|
|
+ if ( debug_dataset )
|
|
|
+ fprintf ( stderr, "WARNING: code %s ignored !\n", classcode.c_str() );
|
|
|
}
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( debug_dataset )
|
|
|
+ ls.printInformation();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void LabeledFileList::get (
|
|
|
+ const std::string & dir,
|
|
|
+ const Config & datasetconf,
|
|
|
+ const ClassNames & classnames,
|
|
|
+ LabeledSet & ls,
|
|
|
+ bool localizationInfoDisabled,
|
|
|
+ bool debugDataset )
|
|
|
+{
|
|
|
+ std::string pattern = datasetconf.gS ( "main", "pattern", "" );
|
|
|
+ std::string filelist = datasetconf.gS ( "main", "filelist", "" );
|
|
|
+ this->debug_dataset = debugDataset;
|
|
|
+
|
|
|
+ if ( pattern.size() > 0 )
|
|
|
+ getFromPattern ( dir, datasetconf, classnames, ls, localizationInfoDisabled );
|
|
|
+ else if ( filelist.size() > 0 ) {
|
|
|
+
|
|
|
+ std::string cfilelist = datasetconf.gS ( "main", "filelist" );
|
|
|
+ std::string filelist = ( cfilelist.substr ( 0, 1 ) == "/" ) ? cfilelist : dir + "/" + cfilelist;
|
|
|
+
|
|
|
+ getFromList ( filelist, datasetconf, classnames, ls, localizationInfoDisabled );
|
|
|
+ } else {
|
|
|
+ fprintf ( stderr, "LabeledFileList: Unable to obtain labeled file list\n" );
|
|
|
+ exit ( -1 );
|
|
|
+ }
|
|
|
}
|