LabeledSetFactory.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef LABELEDSETFACTORY_H
  2. #define LABELEDSETFACTORY_H
  3. #include <string>
  4. #include <map>
  5. #include "vislearning/cbaselib/LabeledSet.h"
  6. #include "LabeledSetCreatorInterface.h"
  7. namespace OBJREC
  8. {
  9. /**
  10. * @brief Factory providing xml loading classes according to their document type.
  11. *
  12. * In order to extract ground truth information or - in general - structured data into a MultiDataset, xml files can be used as
  13. * containers of file and label information. This factory is used to choose the right loading function for different xml file formats
  14. * according to the stated document type in the xml file (e.g. < !DOCTYPE DaimlerStereoPedRecXml > ). This enables a flexible program design
  15. * that can handle loading different xml files at runtime.
  16. *
  17. * When initializing this factory, you map a given document type to the appropriate loading class, which implements the interface given by LabeledSetCreatorInterface.
  18. * For example, using the document type "DaimlerStereoPedRecXml" from above, by calling ::addCreator() this string is mapped to the xml load LabeledSetCreatorDaimlerXml,
  19. * which exactly knows how to handle the internal xml structure and create a LabeledSet out of it.
  20. *
  21. * <h2>Example configuration</h2>
  22. * contents of main_prog.config:
  23. * \verbatim
  24. ...
  25. [train]
  26. dataset = <path to train folder containing config for training (train.config)>
  27. ...
  28. \endverbatim
  29. *
  30. * contents of training folder:<br>
  31. * \verbatim
  32. train.config
  33. train_files.xml
  34. ...
  35. \endverbatim
  36. *
  37. * contents of train.config:
  38. * \verbatim
  39. [main]
  40. factoryxml = train_files.xml -> specifying that the factory is used for loading a xml file containing further information
  41. ...
  42. \endverbatim
  43. * contents of train_files.xml:
  44. * \verbatim
  45. < !DOCTYPE DaimlerStereoPedRecXml > -> Doctype tells the factory which implementation of the LabeledSetCreatorInterface interface should be used for handling this particulary xml file
  46. < itemlist >
  47. < item filename="07m_04s_237599u.pgm" objectid="17028"/>
  48. < item filename="15m_39s_595649u.pgm" objectid="39616"/>
  49. ....
  50. < / itemlist >
  51. \endverbatim
  52. * To handle this example train_files.xml, suppose a class LabeledSetCreatorDaimlerXml was implemented (implementing interface LabeledSetCreatorInterface) .
  53. * You have to add this class to the factory in advance of using the factory:
  54. * \code
  55. * NICE::Config confSimple ( "main_prog.config" );
  56. *
  57. * LabeledSetCreatorDaimlerXml *daimlerXml = new LabeledSetCreatorDaimlerXml();
  58. * LabeledSetFactory *factory = new LabeledSetFactory();
  59. * factory->addCreator("DaimlerStereoPedRecXml", daimlerXml); //the daimlerXml tells the factory that it is associated with the doctype "DaimlerStereoPedRecXml"
  60. *
  61. * MultiDataset md( &confSimple, factory);
  62. *
  63. * const LabeledSet *pTrainFiles = md["train"];
  64. * ...
  65. * \endcode
  66. *
  67. * \date 2012/05/18
  68. * \author Johannes Ruehle
  69. */
  70. class LabeledSetFactory
  71. {
  72. public:
  73. LabeledSetFactory();
  74. /**
  75. * @brief Fill a LabeledSet with data loaded from a xml file using the right loading object specified by the xml document type.
  76. *
  77. * Reads the document type of a xml file containing training / test data and calls the appropriate xml extraction class accordingly.
  78. * As a result, the xml loaded extract label information and stores them into a LabeledSet
  79. *
  80. * Note: An appropriate class derived from LabeledSetCreatorInterface had to be added in advance, by LabeledSetFactory::addCreator().
  81. *
  82. * @param sXmlFilename name of the config file (xml) containing a specific structure stated by the document type
  83. * according to which the appropriate LabeledSetCreatorInferface implementation is called for xml loading
  84. * @param p_conf Global config structure
  85. * @param p_classnames Structure containing all classnames of the ground truth
  86. * @param p_LabelSet labeled set of data to be created. All loaded data is appended to this structure.
  87. */
  88. void createLabeledSetFromXml(std::string sXmlFilename,
  89. const NICE::Config & p_conf,
  90. const ClassNames & p_classnames,
  91. LabeledSet &p_LabelSet);
  92. /**
  93. * @brief Create mapping from a xml document type to the appropriate loading function for a xml file.
  94. *
  95. * @param sCreatorName xml document type stating a specific xml format and its contents
  96. * @param pCreator object reference that knows how to load / parse the specific xml file and create a LabeledData set of it.
  97. */
  98. void addCreator(std::string sCreatorName, LabeledSetCreatorInterface *pCreator);
  99. protected:
  100. /// Mapping from xml document type to loader implementation.
  101. std::map<std::string, LabeledSetCreatorInterface*> m_MapLSCreators;
  102. };
  103. } //namespace
  104. #endif // LABELEDSETFACTORY_H