LabeledFileList.cpp 9.7 KB

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