LabeledFileList.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * @file LabeledFileList.cpp
  3. * @brief reads images from directory
  4. * @author Erik Rodner
  5. * @date 17.09.2007
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <iostream>
  11. #include <sstream>
  12. #include "core/basics/StringTools.h"
  13. #include "core/basics/FileMgt.h"
  14. #include "vislearning/cbaselib/LabeledFileList.h"
  15. using namespace OBJREC;
  16. using namespace std;
  17. using namespace NICE;
  18. LabeledFileList::LabeledFileList()
  19. {
  20. debug_dataset = false;
  21. }
  22. LabeledFileList::~LabeledFileList()
  23. {
  24. }
  25. LocalizationResult *LabeledFileList::getLocalizationInfo ( const ClassNames & classnames,
  26. int classno,
  27. const std::string & file,
  28. const Config & conf ) const
  29. {
  30. /*
  31. localization_pattern = image
  32. localization_subst = mask
  33. localization_format = image
  34. */
  35. std::string format = conf.gS("main", "localization_format", "unknown");
  36. if ( format == "unknown" )
  37. return NULL;
  38. std::string pattern = conf.gS("main", "localization_pattern" );
  39. std::string subst = conf.gS("main", "localization_subst" );
  40. std::string lfile = file;
  41. if ( ! StringTools::regexSubstitute ( lfile, pattern, subst ) )
  42. {
  43. fprintf (stderr, "Unable to substitute using pattern #%s# and string #%s#\n",
  44. pattern.c_str(), lfile.c_str() );
  45. exit(-1);
  46. }
  47. if ( ! FileMgt::fileExists(lfile) ) return NULL;
  48. if ( debug_dataset )
  49. {
  50. fprintf (stderr, "LabeledFileList: reading localization information %s\n", lfile.c_str() );
  51. }
  52. LocalizationResult *lr = NULL;
  53. if ( format == "image" )
  54. {
  55. NICE::Image mask;
  56. try {
  57. mask.read(lfile);
  58. } catch (ImageException &) {
  59. fprintf (stderr, "WARNING: unable to open file %s (no localization info provided)\n",
  60. lfile.c_str() );
  61. return NULL;
  62. }
  63. lr = new LocalizationResult ( &classnames, mask, classno );
  64. } else if ( format == "imagergb" ) {
  65. NICE::ColorImage mask;
  66. try {
  67. mask.read( lfile );
  68. } catch (ImageException &e) {
  69. fprintf (stderr, "WARNING: unable to open file %s (no localization info provided)\n",
  70. lfile.c_str() );
  71. fprintf (stderr, "Error: %s\n", e.what() );
  72. return NULL;
  73. }
  74. lr = new LocalizationResult ( &classnames, mask );
  75. } else if ( format == "polygon" ) {
  76. lr = new LocalizationResult ( &classnames );
  77. lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON );
  78. if ( debug_dataset )
  79. fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
  80. } else {
  81. fthrow(Exception, "Localization format not yet supported !!\n");
  82. }
  83. if ( debug_dataset )
  84. if ( lr != NULL )
  85. fprintf (stderr, "%s (%d objects)\n", lfile.c_str(), (int)lr->size() );
  86. return lr;
  87. }
  88. void LabeledFileList::getFromPattern (
  89. const std::string & dir,
  90. const Config & datasetconf,
  91. const ClassNames & classnames,
  92. LabeledSet & ls,
  93. bool localizationInfoDisabled ) const
  94. {
  95. std::string filemask;
  96. if ( dir.substr(dir.length()-1,1) != "/" )
  97. filemask = dir + "/" + datasetconf.gS("main", "pattern");
  98. else
  99. filemask = dir + datasetconf.gS("main", "pattern");
  100. std::vector<string> files;
  101. int classnameField = datasetconf.gI("main", "classname_field", 1);
  102. std::string fixedClassname = datasetconf.gS("main", "fixed_classname", "");
  103. files.clear();
  104. FileMgt::DirectoryRecursive ( files, dir );
  105. fprintf (stderr, "LabeledFileList: Files: %d\n", (int)files.size());
  106. sort ( files.begin(), files.end() );
  107. for ( vector<string>::const_iterator i = files.begin();
  108. i != files.end();
  109. i++ )
  110. {
  111. vector<string> submatches;
  112. // refactor-nice.pl: check this substitution
  113. // old: const string & file = *i;
  114. const std::string & file = *i;
  115. if ( debug_dataset )
  116. fprintf (stderr, "LabeledFileList: next file: %s\n", file.c_str() );
  117. bool match = StringTools::regexMatch ( file, filemask, submatches );
  118. if ( (fixedClassname == "") && ((int)submatches.size() <= classnameField) ) match = false;
  119. if ( ! match )
  120. {
  121. if ( debug_dataset )
  122. fprintf (stderr, "LabeledFileList: WARNING: %s does not match filemask: %s!!\n", file.c_str(), filemask.c_str() );
  123. } else {
  124. std::string classcode = ( fixedClassname == "" ) ? submatches[classnameField] : fixedClassname;
  125. if ( classnames.existsClassCode(classcode) ) {
  126. int classno = classnames.classno(classcode);
  127. LocalizationResult *lr = NULL;
  128. if ( ! localizationInfoDisabled )
  129. lr = getLocalizationInfo (
  130. classnames, classno, file, datasetconf);
  131. if ( debug_dataset )
  132. fprintf (stderr, "LabeledFileList: LabeledSet: add %s (%d)\n", file.c_str(), classno );
  133. if ( lr == NULL )
  134. {
  135. ls.add ( classno, new ImageInfo(file) );
  136. } else {
  137. ls.add ( classno, new ImageInfo(file, lr) );
  138. if ( debug_dataset )
  139. fprintf (stderr, "LabeledFileList: LocalizationResult added!\n");
  140. }
  141. } else {
  142. if ( debug_dataset )
  143. {
  144. for ( vector<string>::iterator i = submatches.begin();
  145. i != submatches.end();
  146. i++ )
  147. {
  148. fprintf (stderr, "LabeledFileList: submatch: %s\n", i->c_str() );
  149. }
  150. fprintf (stderr, "LabeledFileList: WARNING: code %s ignored !\n", classcode.c_str() );
  151. }
  152. }
  153. }
  154. if ( debug_dataset )
  155. fprintf (stderr, "LabeledFileList: filename processed\n");
  156. }
  157. cerr << "directory " << dir << " loaded..." << endl;
  158. ls.printInformation();
  159. }
  160. void LabeledFileList::getFromList (
  161. const std::string & filelist,
  162. const Config & datasetconf,
  163. const ClassNames & classnames,
  164. LabeledSet & ls,
  165. bool localizationInfoDisabled) const
  166. {
  167. if ( debug_dataset )
  168. fprintf (stderr, "Reading file list: %s\n", filelist.c_str() );
  169. ifstream ifs ( filelist.c_str(), ios::in );
  170. if ( ! ifs.good() )
  171. fthrow(IOException, "File list " << filelist << " not found !");
  172. std::string fixedClassname = datasetconf.gS("main", "fixed_classname", "");
  173. while ( ! ifs.eof() )
  174. {
  175. std::string classcode;
  176. std::string file;
  177. if ( fixedClassname == "" ) {
  178. if ( ! (ifs >> classcode) ) break;
  179. } else {
  180. classcode = fixedClassname;
  181. }
  182. if ( ! (ifs >> file) ) break;
  183. if ( classnames.existsClassCode(classcode) ) {
  184. int classno = classnames.classno(classcode);
  185. LocalizationResult *lr = NULL;
  186. if ( ! localizationInfoDisabled )
  187. lr = getLocalizationInfo ( classnames, classno, file, datasetconf);
  188. if ( debug_dataset )
  189. cerr << "Adding file " << file << " with classno " << classno << endl;
  190. if ( lr == NULL )
  191. ls.add ( classno, new ImageInfo(file) );
  192. else
  193. ls.add ( classno, new ImageInfo(file, lr) );
  194. } else {
  195. if ( debug_dataset )
  196. fprintf (stderr, "WARNING: code %s ignored !\n", classcode.c_str() );
  197. }
  198. }
  199. if ( debug_dataset )
  200. ls.printInformation();
  201. }
  202. void LabeledFileList::get (
  203. const std::string & dir,
  204. const Config & datasetconf,
  205. const ClassNames & classnames,
  206. LabeledSet & ls,
  207. bool localizationInfoDisabled,
  208. bool debugDataset )
  209. {
  210. std::string pattern = datasetconf.gS("main", "pattern", "");
  211. std::string filelist = datasetconf.gS("main", "filelist", "");
  212. this->debug_dataset = debugDataset;
  213. if ( pattern.size() > 0 )
  214. getFromPattern ( dir, datasetconf, classnames, ls, localizationInfoDisabled );
  215. else if ( filelist.size() > 0 ) {
  216. std::string cfilelist = datasetconf.gS("main", "filelist");
  217. std::string filelist = ( cfilelist.substr(0,1) == "/" ) ? cfilelist : dir + "/" + cfilelist;
  218. getFromList ( filelist, datasetconf, classnames, ls, localizationInfoDisabled );
  219. } else {
  220. fprintf (stderr, "LabeledFileList: Unable to obtain labeled file list\n");
  221. exit(-1);
  222. }
  223. }