1 //This is brl/bbas/bvgl/bvgl_labelme_parser.h
2 #ifndef bvgl_labelme_parser_h_
3 #define bvgl_labelme_parser_h_
4 
5 // \verbatim
6 //  Modifications
7 //     Yi Dong   OCT-2012   added parser for mindist, maxdist, name, polygon defined in xml
8 //     Yi Dong   NOV-2012   added parser for order, image size, type and ImageCategory(desert or coast, etc) defined in xml
9 //     Yi Dong   JAN-2013   added parser for object orientation and NLCD land classfication
10 //     Yi Dong   JAN-2014   added new tags
11 //     Yi Dong   FEB-2015   added height tag
12 // \endverbatim
13 
14 #include <string>
15 #include <iostream>
16 #include <sstream>
17 #include <expatpplib.h>
18 #ifdef _MSC_VER
19 #  include <vcl_msvc_warnings.h>
20 #endif
21 #include <vgl/vgl_polygon.h>
22 #include <vgl/vgl_point_2d.h>
23 
24 //tag macros
25 #define ANNOTATION "annotation"
26 #define FILENAME_TAG "filename"
27 #define FOLDER_TAG "folder"
28 #define OBJECT_TAG "object"
29 #define NAME_TAG "name"
30 #define POLYGON_TAG "polygon"
31 #define POINT_TAG "pt"
32 #define PIXEL_TAG "pixels"
33 #define SINGLE_PIONT_TAG "point"
34 #define X_TAG "x"
35 #define Y_TAG "y"
36 #define OBJECT_MINDIST_TAG "mindist"
37 #define OBJECT_MAXDIST_TAG "maxdist"
38 #define OBJECT_ORDER_TAG "order"
39 #define TYPE_TAG "type"
40 #define IMG_CAT_TAG "ImageCategory"
41 #define IMG_NJ_TAG "nrows"
42 #define IMG_NI_TAG "ncols"
43 #define NLCD_TAG "class"
44 #define ORIENT_TAG "orientation"
45 #define LAND_TAG "land"
46 #define HEIGHT_TAG "height"
47 #define WEIGHT_TAG "weight"
48 #define REGION_TAG "region"
49 #define FRAME_TAG "frame"
50 #define REFERENCE_TAG "reference"
51 
52 
53 #if 0
54 <annotation>
55 filename>andy_eze.jpg</filename>
56 <folder>test</folder>
57 <source>
58 <sourceImage>The MIT-CSAIL database of objects and scenes</sourceImage>
59 <sourceAnnotation>LabelMe Webtool</sourceAnnotation>
60 </source>
61 <object>
62 <name>mouth</name>
63 <deleted>0</deleted>
64 <verified>0</verified>
65 <date>05-Mar-2012 22:14:49</date>
66 <id>0</id>
67 <polygon>
68 <username>Andy</username>
69 <pt>
70 <x>335</x>
71 <y>183</y>
72 </pt>
73 </polygon>
74 </object>
75 #endif
76 
77 class bvgl_labelme_parser : public expatpp
78 {
79  public:
80   bvgl_labelme_parser() = default;
81   bvgl_labelme_parser(std::string& filename);
82   ~bvgl_labelme_parser(void) override = default;
83 
84   //image filename/path, category, and size
image_name()85   std::string image_name() const { return image_name_; }
image_category()86   std::string image_category() const { return image_category_; }
region()87   std::string region() const { return region_tag_; }
image_ni()88   unsigned image_ni() const { return image_ni_; }
image_nj()89   unsigned image_nj() const { return image_nj_; }
90   //object names (in the same order as polygons)
obj_names()91   std::vector<std::string>& obj_names()            { return obj_names_; }
obj_types()92   std::vector<std::string>& obj_types()            { return obj_types_; }
obj_mindists()93   std::vector<float>& obj_mindists()              { return obj_min_dists_; }
obj_maxdists()94   std::vector<float>& obj_maxdists()              { return obj_max_dists_; }
obj_depth_orders()95   std::vector<int>& obj_depth_orders()            { return obj_depth_orders_; }
obj_heights()96   std::vector<int>& obj_heights()                 { return obj_heights_; }
obj_references()97   std::vector<bool>& obj_references()             { return obj_references_; }
obj_orientations()98   std::vector<std::string>& obj_orientations()     { return obj_orientations_; }
obj_nlcd_ids()99   std::vector<unsigned>& obj_nlcd_ids()           { return obj_nlcd_ids_; }
obj_land_categories()100   std::vector<std::string>& obj_land_categories()  { return obj_land_categories_; }
obj_weights()101   std::vector<float>& obj_weights()               { return obj_weights_; }
obj_frame_ids()102   std::vector<unsigned>& obj_frame_ids()          { return obj_frame_ids_; }
103 
104   // ACCESSORS for parser info
polygons()105   std::vector<vgl_polygon<double> >& polygons() { return polygons_; }
points()106   std::vector<vgl_point_2d<double> >& points()  { return pts_; }
pixels()107   std::vector<vgl_point_2d<double> >& pixels()  { return pixels_; }
108   void trim_string(std::string& s);
109 
110  private:
111   void startElement(const XML_Char* name, const XML_Char** atts) override;
112   void endElement(const XML_Char* name) override;
113   void charData(const XML_Char* s, int len) override;
114 
115   //lvcs temp values
116   std::vector<vgl_polygon<double> > polygons_;
117   std::vector<vgl_point_2d<double> > pts_;
118   std::vector<vgl_point_2d<double> > pixels_;
119   double x_, y_;
120 
121   std::vector<std::string>         obj_names_;
122   std::vector<std::string>         obj_types_;
123   std::vector<float>          obj_min_dists_;
124   std::vector<float>          obj_max_dists_;
125   std::vector<int>         obj_depth_orders_;
126   std::vector<unsigned>        obj_nlcd_ids_;
127   std::vector<std::string>  obj_orientations_;
128   std::vector<std::string>  obj_land_categories_;
129   std::vector<int>         obj_heights_;
130   std::vector<bool>        obj_references_;
131   std::vector<float>       obj_weights_;
132   std::vector<unsigned>    obj_frame_ids_;
133 
134   float min_dist_;
135   float max_dist_;
136   float weight_;
137   int order_;
138   int height_;
139   int reference_;
140   std::string image_category_;
141   std::string image_name_;
142   std::string obj_orient_;
143   std::string region_tag_;
144   std::string temp_str_;
145   unsigned frame_id_;
146   unsigned nlcd_id_;
147   unsigned image_ni_;
148   unsigned image_nj_;
149 
150 
151   //set active tag for parsing char data from different tags
152   std::string active_tag_;
153 };
154 
155 //string converter
156 template <typename T>
convert(const char * t,T & d)157 void convert(const char* t, T& d)
158 {
159   std::string s = t;
160   std::stringstream strm(s);
161   strm >> d;
162 }
163 
164 template <typename T>
convert(std::string s,T & d)165 void convert(std::string s, T& d)
166 {
167   convert(s.c_str(), d);
168 }
169 
170 #endif
171