LabeledFileList.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. }
  77. else if ( format == "imagergb" ) {
  78. NICE::ColorImage mask;
  79. try {
  80. mask.read ( lfile );
  81. } catch ( ImageException &e ) {
  82. fprintf ( stderr, "WARNING: unable to open file %s (no localization info provided)\n",
  83. lfile.c_str() );
  84. fprintf ( stderr, "Error: %s\n", e.what() );
  85. return NULL;
  86. }
  87. lr = new LocalizationResult ( &classnames, mask );
  88. }
  89. else if ( format == "polygon" ) {
  90. lr = new LocalizationResult ( &classnames );
  91. lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON );
  92. if ( debug_dataset )
  93. fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
  94. }
  95. else if ( format == "polygon_siftflow" ) {
  96. lr = new LocalizationResult ( &classnames );
  97. lr->read ( lfile, LocalizationResult::FILEFORMAT_POLYGON_SIFTFLOW );
  98. if ( debug_dataset )
  99. fprintf (stderr, "LabeledFileList: object localization %d\n", (int)lr->size() );
  100. }
  101. else if ( format == "imagelabeler" ) {
  102. lr = new LocalizationResult ( &classnames );
  103. lr->loadImageInfo(lfile);
  104. }
  105. else {
  106. fthrow(Exception, "Localization format not yet supported !!\n");
  107. }
  108. if ( debug_dataset )
  109. if ( lr != NULL )
  110. fprintf (stderr, "%s (%d objects)\n", lfile.c_str(), (int)lr->size() );
  111. return lr;
  112. }
  113. void LabeledFileList::getFromPattern (
  114. const std::string & dir,
  115. const Config & datasetconf,
  116. const ClassNames & classnames,
  117. LabeledSet & ls,
  118. bool localizationInfoDisabled ) const
  119. {
  120. std::string filemask;
  121. if ( dir.substr ( dir.length() - 1, 1 ) != "/" )
  122. filemask = dir + "/" + datasetconf.gS ( "main", "pattern" );
  123. else
  124. filemask = dir + datasetconf.gS ( "main", "pattern" );
  125. std::vector<string> files;
  126. int classnameField = datasetconf.gI ( "main", "classname_field", 1 );
  127. std::string fixedClassname = datasetconf.gS ( "main", "fixed_classname", "" );
  128. files.clear();
  129. FileMgt::DirectoryRecursive ( files, dir );
  130. fprintf ( stderr, "LabeledFileList: Files: %d\n", ( int ) files.size() );
  131. sort ( files.begin(), files.end() );
  132. for ( vector<string>::const_iterator i = files.begin();
  133. i != files.end();
  134. i++ )
  135. {
  136. vector<string> submatches;
  137. // refactor-nice.pl: check this substitution
  138. // old: const string & file = *i;
  139. const std::string & file = *i;
  140. if ( debug_dataset )
  141. fprintf ( stderr, "LabeledFileList: next file: %s\n", file.c_str() );
  142. bool match = StringTools::regexMatch ( file, filemask, submatches );
  143. if ( ( fixedClassname == "" ) && ( ( int ) submatches.size() <= classnameField ) ) match = false;
  144. if ( ! match )
  145. {
  146. if ( debug_dataset )
  147. fprintf ( stderr, "LabeledFileList: WARNING: %s does not match filemask: %s!!\n", file.c_str(), filemask.c_str() );
  148. } else {
  149. std::string classcode = ( fixedClassname == "" ) ? submatches[classnameField] : fixedClassname;
  150. if ( classnames.existsClassCode ( classcode ) ) {
  151. int classno = classnames.classno ( classcode );
  152. LocalizationResult *lr = NULL;
  153. if ( ! localizationInfoDisabled )
  154. lr = getLocalizationInfo (
  155. classnames, classno, file, datasetconf );
  156. if ( debug_dataset )
  157. fprintf ( stderr, "LabeledFileList: LabeledSet: add %s (%d)\n", file.c_str(), classno );
  158. if ( lr == NULL )
  159. {
  160. ls.add ( classno, new ImageInfo ( file ) );
  161. } else {
  162. ls.add ( classno, new ImageInfo ( file, lr ) );
  163. if ( debug_dataset )
  164. fprintf ( stderr, "LabeledFileList: LocalizationResult added!\n" );
  165. }
  166. } else {
  167. if ( debug_dataset )
  168. {
  169. for ( vector<string>::iterator i = submatches.begin();
  170. i != submatches.end();
  171. i++ )
  172. {
  173. fprintf ( stderr, "LabeledFileList: submatch: %s\n", i->c_str() );
  174. }
  175. fprintf ( stderr, "LabeledFileList: WARNING: code %s ignored !\n", classcode.c_str() );
  176. }
  177. }
  178. }
  179. if ( debug_dataset )
  180. fprintf ( stderr, "LabeledFileList: filename processed\n" );
  181. }
  182. cerr << "directory " << dir << " loaded..." << endl;
  183. ls.printInformation();
  184. }
  185. void LabeledFileList::getFromList (
  186. const std::string & filelist,
  187. const Config & datasetconf,
  188. const ClassNames & classnames,
  189. LabeledSet & ls,
  190. bool localizationInfoDisabled ) const
  191. {
  192. if ( debug_dataset )
  193. fprintf ( stderr, "Reading file list: %s\n", filelist.c_str() );
  194. ifstream ifs ( filelist.c_str(), ios::in );
  195. if ( ! ifs.good() )
  196. fthrow ( IOException, "File list " << filelist << " not found !" );
  197. std::string fixedClassname = datasetconf.gS ( "main", "fixed_classname", "" );
  198. while ( ! ifs.eof() )
  199. {
  200. std::string classcode;
  201. std::string file;
  202. if ( fixedClassname == "" ) {
  203. if ( ! ( ifs >> classcode ) ) break;
  204. } else {
  205. classcode = fixedClassname;
  206. }
  207. if ( ! ( ifs >> file ) ) break;
  208. file = datasetconf.getAbsoluteFilenameRelativeToThisConfig(file);
  209. if ( classnames.existsClassCode ( classcode ) ) {
  210. int classno = classnames.classno ( classcode );
  211. LocalizationResult *lr = NULL;
  212. if ( ! localizationInfoDisabled )
  213. lr = getLocalizationInfo ( classnames, classno, file, datasetconf );
  214. if ( debug_dataset )
  215. cerr << "Adding file " << file << " with classno " << classno << endl;
  216. if ( lr == NULL )
  217. ls.add ( classno, new ImageInfo ( file ) );
  218. else
  219. ls.add ( classno, new ImageInfo ( file, lr ) );
  220. } else {
  221. if ( debug_dataset )
  222. fprintf ( stderr, "WARNING: code %s ignored !\n", classcode.c_str() );
  223. }
  224. }
  225. if ( debug_dataset )
  226. ls.printInformation();
  227. }
  228. void LabeledFileList::get (
  229. const std::string & dir,
  230. const Config & datasetconf,
  231. const ClassNames & classnames,
  232. LabeledSet & ls,
  233. bool localizationInfoDisabled,
  234. bool debugDataset )
  235. {
  236. std::string pattern = datasetconf.gS("main", "pattern", "");
  237. std::string filelist = datasetconf.gS("main", "filelist", "");
  238. std::string factoryxmlfile = datasetconf.gS("main", "factoryxml", "");
  239. this->debug_dataset = debugDataset;
  240. if ( pattern.size() > 0 )
  241. getFromPattern ( dir, datasetconf, classnames, ls, localizationInfoDisabled );
  242. else if ( filelist.size() > 0 ) {
  243. std::string cfilelist = datasetconf.gS("main", "filelist");
  244. std::string filelist = ( cfilelist.substr(0,1) == "/" ) ? cfilelist : dir + "/" + cfilelist;
  245. getFromList ( filelist, datasetconf, classnames, ls, localizationInfoDisabled );
  246. }
  247. else if( !factoryxmlfile.empty() && m_pLabeledSetFactory != NULL )
  248. {
  249. factoryxmlfile = ( factoryxmlfile.substr(0,1) == "/" ) ? factoryxmlfile : dir + "/" + factoryxmlfile;
  250. m_pLabeledSetFactory->createLabeledSetFromXml( factoryxmlfile, datasetconf,classnames, ls );
  251. }
  252. else {
  253. fprintf (stderr, "LabeledFileList: Unable to obtain labeled file list\n");
  254. exit(-1);
  255. }
  256. }