LabeledFileList.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 = true;
  21. }
  22. LabeledFileList::~LabeledFileList()
  23. {
  24. }
  25. /**
  26. * @brief Loads the label information according to a given label file format.
  27. *
  28. * Supported types of label file format (localization_format):
  29. * - "image": <br>usage of a single channel images containing label regions
  30. * - "imagergb": <br>usage of a multi channel color images containing label regions
  31. * - "polygon": <br>obtaining bounding boxes from textural label files (used with e.g. PASCAL dataset)
  32. * - "imagelabeler": <br>obtaining label information (currently only bounding boxes) from the separate label files (XML like) created with the ImageLabeler ( < file name >_labeled.dat ). @see ImageInfo
  33. *
  34. * @param classnames class containing all potential class names (label categories)
  35. * @param conf configuration structure containing a information from a loaded config file; has to tag "localization_format" in section "main" in order to obtain the correct label information from a file.
  36. */
  37. LocalizationResult *LabeledFileList::getLocalizationInfo ( const ClassNames & classnames,
  38. int classno,
  39. const std::string & file,
  40. const Config & conf ) const
  41. {
  42. /*
  43. localization_pattern = image
  44. localization_subst = mask
  45. localization_format = image
  46. */
  47. std::string format = conf.gS ( "main", "localization_format", "unknown" );
  48. if ( format == "unknown" )
  49. return NULL;
  50. std::string pattern = conf.gS ( "main", "localization_pattern" );
  51. std::string subst = conf.gS ( "main", "localization_subst" );
  52. std::string lfile = file;
  53. if ( ! StringTools::regexSubstitute ( lfile, pattern, subst ) )
  54. {
  55. fprintf ( stderr, "Unable to substitute using pattern #%s# and string #%s#\n",
  56. pattern.c_str(), lfile.c_str() );
  57. exit ( -1 );
  58. }
  59. if ( ! FileMgt::fileExists ( lfile ) ) return NULL;
  60. if ( debug_dataset )
  61. {
  62. fprintf ( stderr, "LabeledFileList: reading localization information %s\n", lfile.c_str() );
  63. }
  64. LocalizationResult *lr = NULL;
  65. if ( format == "image" )
  66. {
  67. NICE::Image mask;
  68. try {
  69. mask.read ( lfile );
  70. } catch ( ImageException & ) {
  71. fprintf ( stderr, "WARNING: unable to open file %s (no localization info provided)\n",
  72. lfile.c_str() );
  73. return NULL;
  74. }
  75. lr = new LocalizationResult ( &classnames, mask, classno );
  76. } else if ( format == "imagergb" ) {
  77. NICE::ColorImage mask;
  78. try {
  79. mask.read ( lfile );
  80. } catch ( ImageException &e ) {
  81. fprintf ( stderr, "WARNING: unable to open file %s (no localization info provided)\n",
  82. lfile.c_str() );
  83. fprintf ( stderr, "Error: %s\n", e.what() );
  84. return NULL;
  85. }
  86. lr = new LocalizationResult ( &classnames, mask );
  87. } else if ( format == "polygon" ) {
  88. lr = new LocalizationResult ( &classnames );
  89. lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON );
  90. if ( debug_dataset )
  91. fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
  92. }
  93. else if ( format == "imagelabeler" ) {
  94. lr = new LocalizationResult ( &classnames );
  95. lr->loadImageInfo(lfile);
  96. }
  97. else {
  98. fthrow(Exception, "Localization format not yet supported !!\n");
  99. }
  100. if ( debug_dataset )
  101. if ( lr != NULL )
  102. fprintf (stderr, "%s (%d objects)\n", lfile.c_str(), (int)lr->size() );
  103. return lr;
  104. }
  105. void LabeledFileList::getFromPattern (
  106. const std::string & dir,
  107. const Config & datasetconf,
  108. const ClassNames & classnames,
  109. LabeledSet & ls,
  110. bool localizationInfoDisabled ) const
  111. {
  112. std::string filemask;
  113. if ( dir.substr ( dir.length() - 1, 1 ) != "/" )
  114. filemask = dir + "/" + datasetconf.gS ( "main", "pattern" );
  115. else
  116. filemask = dir + datasetconf.gS ( "main", "pattern" );
  117. std::vector<string> files;
  118. int classnameField = datasetconf.gI ( "main", "classname_field", 1 );
  119. std::string fixedClassname = datasetconf.gS ( "main", "fixed_classname", "" );
  120. files.clear();
  121. FileMgt::DirectoryRecursive ( files, dir );
  122. fprintf ( stderr, "LabeledFileList: Files: %d\n", ( int ) files.size() );
  123. sort ( files.begin(), files.end() );
  124. for ( vector<string>::const_iterator i = files.begin();
  125. i != files.end();
  126. i++ )
  127. {
  128. vector<string> submatches;
  129. // refactor-nice.pl: check this substitution
  130. // old: const string & file = *i;
  131. const std::string & file = *i;
  132. if ( debug_dataset )
  133. fprintf ( stderr, "LabeledFileList: next file: %s\n", file.c_str() );
  134. bool match = StringTools::regexMatch ( file, filemask, submatches );
  135. if ( ( fixedClassname == "" ) && ( ( int ) submatches.size() <= classnameField ) ) match = false;
  136. if ( ! match )
  137. {
  138. if ( debug_dataset )
  139. fprintf ( stderr, "LabeledFileList: WARNING: %s does not match filemask: %s!!\n", file.c_str(), filemask.c_str() );
  140. } else {
  141. std::string classcode = ( fixedClassname == "" ) ? submatches[classnameField] : fixedClassname;
  142. if ( classnames.existsClassCode ( classcode ) ) {
  143. int classno = classnames.classno ( classcode );
  144. LocalizationResult *lr = NULL;
  145. if ( ! localizationInfoDisabled )
  146. lr = getLocalizationInfo (
  147. classnames, classno, file, datasetconf );
  148. if ( debug_dataset )
  149. fprintf ( stderr, "LabeledFileList: LabeledSet: add %s (%d)\n", file.c_str(), classno );
  150. if ( lr == NULL )
  151. {
  152. ls.add ( classno, new ImageInfo ( file ) );
  153. } else {
  154. ls.add ( classno, new ImageInfo ( file, lr ) );
  155. if ( debug_dataset )
  156. fprintf ( stderr, "LabeledFileList: LocalizationResult added!\n" );
  157. }
  158. } else {
  159. if ( debug_dataset )
  160. {
  161. for ( vector<string>::iterator i = submatches.begin();
  162. i != submatches.end();
  163. i++ )
  164. {
  165. fprintf ( stderr, "LabeledFileList: submatch: %s\n", i->c_str() );
  166. }
  167. fprintf ( stderr, "LabeledFileList: WARNING: code %s ignored !\n", classcode.c_str() );
  168. }
  169. }
  170. }
  171. if ( debug_dataset )
  172. fprintf ( stderr, "LabeledFileList: filename processed\n" );
  173. }
  174. cerr << "directory " << dir << " loaded..." << endl;
  175. ls.printInformation();
  176. }
  177. void LabeledFileList::getFromList (
  178. const std::string & filelist,
  179. const Config & datasetconf,
  180. const ClassNames & classnames,
  181. LabeledSet & ls,
  182. bool localizationInfoDisabled ) const
  183. {
  184. if ( debug_dataset )
  185. fprintf ( stderr, "Reading file list: %s\n", filelist.c_str() );
  186. ifstream ifs ( filelist.c_str(), ios::in );
  187. if ( ! ifs.good() )
  188. fthrow ( IOException, "File list " << filelist << " not found !" );
  189. std::string fixedClassname = datasetconf.gS ( "main", "fixed_classname", "" );
  190. while ( ! ifs.eof() )
  191. {
  192. std::string classcode;
  193. std::string file;
  194. if ( fixedClassname == "" ) {
  195. if ( ! ( ifs >> classcode ) ) break;
  196. } else {
  197. classcode = fixedClassname;
  198. }
  199. if ( ! ( ifs >> file ) ) break;
  200. file = datasetconf.getAbsoluteFilenameRelativeToThisConfig(file);
  201. if ( classnames.existsClassCode ( classcode ) ) {
  202. int classno = classnames.classno ( classcode );
  203. LocalizationResult *lr = NULL;
  204. if ( ! localizationInfoDisabled )
  205. lr = getLocalizationInfo ( classnames, classno, file, datasetconf );
  206. if ( debug_dataset )
  207. cerr << "Adding file " << file << " with classno " << classno << endl;
  208. if ( lr == NULL )
  209. ls.add ( classno, new ImageInfo ( file ) );
  210. else
  211. ls.add ( classno, new ImageInfo ( file, lr ) );
  212. } else {
  213. if ( debug_dataset )
  214. fprintf ( stderr, "WARNING: code %s ignored !\n", classcode.c_str() );
  215. }
  216. }
  217. if ( debug_dataset )
  218. ls.printInformation();
  219. }
  220. void LabeledFileList::get (
  221. const std::string & dir,
  222. const Config & datasetconf,
  223. const ClassNames & classnames,
  224. LabeledSet & ls,
  225. bool localizationInfoDisabled,
  226. bool debugDataset )
  227. {
  228. std::string pattern = datasetconf.gS("main", "pattern", "");
  229. std::string filelist = datasetconf.gS("main", "filelist", "");
  230. std::string factoryxmlfile = datasetconf.gS("main", "factoryxml", "");
  231. this->debug_dataset = debugDataset;
  232. if ( pattern.size() > 0 )
  233. getFromPattern ( dir, datasetconf, classnames, ls, localizationInfoDisabled );
  234. else if ( filelist.size() > 0 ) {
  235. std::string cfilelist = datasetconf.gS("main", "filelist");
  236. std::string filelist = ( cfilelist.substr(0,1) == "/" ) ? cfilelist : dir + "/" + cfilelist;
  237. getFromList ( filelist, datasetconf, classnames, ls, localizationInfoDisabled );
  238. }
  239. else if( !factoryxmlfile.empty() && m_pLabeledSetFactory != NULL )
  240. {
  241. factoryxmlfile = ( factoryxmlfile.substr(0,1) == "/" ) ? factoryxmlfile : dir + "/" + factoryxmlfile;
  242. m_pLabeledSetFactory->createLabeledSetFromXml( factoryxmlfile, datasetconf,classnames, ls );
  243. }
  244. else {
  245. fprintf (stderr, "LabeledFileList: Unable to obtain labeled file list\n");
  246. exit(-1);
  247. }
  248. }