LabeledFileList.cpp 11 KB

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