LabeledFileList.cpp 7.5 KB

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