contrib/brl/bbas/bvgl/bvgl_labelme_parser.h
Go to the documentation of this file.
00001 //This is brl/bbas/bvgl/bvgl_labelme_parser.h
00002 #ifndef bvgl_labelme_parser_h_
00003 #define bvgl_labelme_parser_h_
00004 
00005 #include <expatpplib.h>
00006 #include <vcl_string.h>
00007 #include <vcl_sstream.h>
00008 #include <vgl/vgl_polygon.h>
00009 #include <vgl/vgl_point_2d.h>
00010 
00011 //tag macros
00012 #define ANNOTATION "annotation"
00013 #define FILENAME_TAG "filename"
00014 #define FOLDER_TAG "folder"
00015 #define OBJECT_TAG "object"
00016 #define NAME_TAG "name"
00017 #define POLYGON_TAG "polygon"
00018 #define POINT_TAG "pt"
00019 #define X_TAG "x"
00020 #define Y_TAG "y"
00021 
00022 #if 0
00023 <annotation>
00024 filename>andy_eze.jpg</filename>
00025 <folder>test</folder>
00026 <source>
00027 <sourceImage>The MIT-CSAIL database of objects and scenes</sourceImage>
00028 <sourceAnnotation>LabelMe Webtool</sourceAnnotation>
00029 </source>
00030 <object>
00031 <name>mouth</name>
00032 <deleted>0</deleted>
00033 <verified>0</verified>
00034 <date>05-Mar-2012 22:14:49</date>
00035 <id>0</id>
00036 <polygon>
00037 <username>Andy</username>
00038 <pt>
00039 <x>335</x>
00040 <y>183</y>
00041 </pt>
00042 </polygon>
00043 </object>
00044 #endif
00045 
00046 class bvgl_labelme_parser : public expatpp
00047 {
00048  public:
00049   bvgl_labelme_parser() {};
00050   bvgl_labelme_parser(vcl_string& filename);
00051   ~bvgl_labelme_parser(void) {}
00052 
00053   //image filename/path
00054   vcl_string image_name() { return image_name_; }
00055 
00056   //object names (in the same order as polygons)
00057   //vcl_vector<vcl_string> obj_names() { return obj_names_; }
00058   vcl_vector<vcl_string>& obj_names() { return obj_names_; }
00059 
00060   // ACCESSORS for parser info
00061   //vcl_vector<vgl_polygon<double> > polygons() { return polygons_; }
00062   vcl_vector<vgl_polygon<double> >& polygons() { return polygons_; }
00063 
00064  private:
00065   virtual void startElement(const XML_Char* name, const XML_Char** atts);
00066   virtual void endElement(const XML_Char* name);
00067   virtual void charData(const XML_Char* s, int len);
00068 
00069   //lvcs temp values
00070   vcl_vector<vgl_polygon<double> > polygons_;
00071   vcl_vector<vgl_point_2d<double> > pts_;
00072   double x_, y_;
00073 
00074   vcl_vector<vcl_string> obj_names_;
00075   vcl_string image_name_;
00076 
00077   //set active tag for parsing char data from different tags
00078   vcl_string active_tag_;
00079 };
00080 
00081 //string converter
00082 template <typename T>
00083 void convert(const char* t, T& d)
00084 {
00085   vcl_string s = t;
00086   vcl_stringstream strm(s);
00087   strm >> d;
00088 }
00089 
00090 template <typename T>
00091 void convert(vcl_string s, T& d)
00092 {
00093   convert(s.c_str(), d);
00094 }
00095 
00096 #endif