contrib/brl/bbas/bvgl/bvgl_labelme_parser.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Parses the configuration file for bwm tool.
00004 //
00005 #include "bvgl_labelme_parser.h"
00006 
00007 #include <vcl_sstream.h>
00008 #include <vcl_cstring.h>
00009 #include <vcl_iostream.h>
00010 #include <vcl_cstdio.h>
00011 
00012 
00013 //Constructor from file
00014 bvgl_labelme_parser::bvgl_labelme_parser(vcl_string& filename)
00015 {
00016   vcl_FILE* xmlFile = vcl_fopen(filename.c_str(), "r");
00017   if (!xmlFile) {
00018     vcl_cerr << filename << " error on opening\n";
00019     throw -1;
00020   }
00021   if (!this->parseFile(xmlFile)) {
00022     vcl_cerr << XML_ErrorString(this->XML_GetErrorCode()) << " at line "
00023              << this->XML_GetCurrentLineNumber() << '\n';
00024     throw -1;
00025   }
00026 }
00027 
00028 //-----------------------------------------------------------------------------
00029 //: Start Element needs to parse the following tags
00030 //#define ANNOTATION "annotation"
00031 //#define FILENAME_TAG "filename"
00032 //#define FOLDER_TAG "folder"
00033 //#define OBJECT_TAG "object"
00034 //#define NAME_TAG "name"
00035 //#define POLYTON_TAG "polygon"
00036 //#define POINT_TAG "pt"
00037 //#define X_TAG "x"
00038 //#define Y_TAG "y"
00039 void
00040 bvgl_labelme_parser::startElement(const char* name, const char** atts)
00041 {
00042   //set active tag for charData
00043   active_tag_ = vcl_string(name); 
00044 
00045   //parse object/polygon, start with a fresh set of points
00046   if(vcl_strcmp(name, POLYGON_TAG)==0) 
00047     pts_.clear();
00048 }
00049 
00050 //Creates and pushes polygon, creates/pushes point
00051 void bvgl_labelme_parser::endElement(const XML_Char* name)
00052 {
00053   //Finish up polygon
00054   if(vcl_strcmp(name, POLYGON_TAG)==0) {
00055     vgl_polygon<double> poly(pts_); 
00056     polygons_.push_back(poly);
00057   }
00058 
00059   //finish up a point
00060   if(vcl_strcmp(name, Y_TAG)==0) {
00061     vgl_point_2d<double> pt(x_, y_);
00062     pts_.push_back(pt);
00063   }
00064 }
00065 
00066 
00067 //Grabs data from points
00068 void bvgl_labelme_parser::charData(const XML_Char* s, int len)
00069 {
00070   if(active_tag_ == X_TAG || active_tag_ == Y_TAG) {
00071     int val; 
00072     convert(vcl_string(s,len), val);
00073     if(active_tag_ == X_TAG)
00074       x_ = (double) val;
00075     if(active_tag_ == Y_TAG) 
00076       y_ = (double) val;
00077   }
00078 
00079   if(active_tag_ == FILENAME_TAG)
00080     image_name_ = vcl_string(s, len);
00081 
00082   if(active_tag_ == NAME_TAG) {
00083     vcl_string name = vcl_string(s,len);
00084     obj_names_.push_back(name);
00085   }
00086 }
00087