1 //:
2 // \file
3 // \brief Parses the configuration file for bwm tool.
4 //
5 #include <sstream>
6 #include <cstring>
7 #include <iostream>
8 #include <cstdio>
9 #include "bvgl_labelme_parser.h"
10
11 #ifdef _MSC_VER
12 # include "vcl_msvc_warnings.h"
13 #endif
14
15
16 //Constructor from file
bvgl_labelme_parser(std::string & filename)17 bvgl_labelme_parser::bvgl_labelme_parser(std::string& filename)
18 {
19 std::FILE* xmlFile = std::fopen(filename.c_str(), "r");
20 if (!xmlFile) {
21 std::cerr << filename << " error on opening\n";
22 throw -1;
23 }
24 if (!this->parseFile(xmlFile)) {
25 std::cerr << XML_ErrorString(this->XML_GetErrorCode()) << " at line "
26 << this->XML_GetCurrentLineNumber() << '\n';
27 throw -1;
28 }
29 }
30
31 //-----------------------------------------------------------------------------
32 //: Start Element needs to parse the following tags
33 //#define ANNOTATION "annotation"
34 //#define FILENAME_TAG "filename"
35 //#define FOLDER_TAG "folder"
36 //#define OBJECT_TAG "object"
37 //#define NAME_TAG "name"
38 //#define POLYTON_TAG "polygon"
39 //#define OBJECT_MINDIST_TAG "mindist"
40 //#define OBJECT_MAXDIST_TAG "maxdist"
41 //#define OBJECT_ORDER_TAG "order"
42 //#define TYPE_TAG "type"
43 //#define POINT_TAG "pt"
44 //#define X_TAG "x"
45 //#define Y_TAG "y"
46 //#define LAND_TAG "land"
47 //#define WEIGHT_TAG "weight"
48 void
startElement(const char * name,const char **)49 bvgl_labelme_parser::startElement(const char* name, const char** /*atts*/)
50 {
51 //set active tag for charData
52 active_tag_ = std::string(name);
53
54 //parse object/polygon, start with a fresh set of points
55 if (std::strcmp(name, POLYGON_TAG)==0)
56 pts_.clear();
57
58 if (std::strcmp(name, OBJECT_TAG)==0) {
59 min_dist_ = -1;
60 max_dist_ = -1;
61 order_ = -1;
62 weight_ = 0;
63 frame_id_ = 0;
64 }
65 }
66
67 //Creates and pushes polygon, creates/pushes point
endElement(const XML_Char * name)68 void bvgl_labelme_parser::endElement(const XML_Char* name)
69 {
70 if (std::strcmp(name, FILENAME_TAG)==0)
71 image_name_ = temp_str_;
72
73 if (std::strcmp(name, REGION_TAG)==0)
74 region_tag_ = temp_str_;
75
76 //Finish up polygon
77 if (std::strcmp(name, POLYGON_TAG)==0) {
78 vgl_polygon<double> poly(pts_);
79 polygons_.push_back(poly);
80 }
81
82 if (std::strcmp(name, PIXEL_TAG)==0 || std::strcmp(name, SINGLE_PIONT_TAG)==0 ) {
83 vgl_point_2d<double> pt(x_, y_);
84 pixels_.push_back(pt);
85 }
86
87 //finish up a point
88 if (std::strcmp(name, Y_TAG)==0) {
89 vgl_point_2d<double> pt(x_, y_);
90 pts_.push_back(pt);
91 }
92
93 //finish up an object
94 if (std::strcmp(name, OBJECT_TAG)==0) {
95 obj_min_dists_.push_back(min_dist_);
96 obj_max_dists_.push_back(max_dist_);
97 obj_depth_orders_.push_back(order_);
98 obj_heights_.push_back(height_);
99 obj_nlcd_ids_.push_back(nlcd_id_);
100 obj_weights_.push_back(weight_);
101 obj_frame_ids_.push_back(frame_id_);
102 if (reference_ == 0)
103 obj_references_.push_back(false);
104 else
105 obj_references_.push_back(true);
106 }
107 }
108
109
110 //Grabs data from points
charData(const XML_Char * s,int len)111 void bvgl_labelme_parser::charData(const XML_Char* s, int len)
112 {
113 #if 0
114 if (active_tag_ == X_TAG || active_tag_ == Y_TAG) {
115 int val;
116 convert(std::string(s,len), val);
117 if (active_tag_ == X_TAG)
118 x_ = (double) val;
119 if (active_tag_ == Y_TAG)
120 y_ = (double) val;
121 }
122 #endif
123 if (active_tag_ == X_TAG )
124 convert(std::string(s,len), x_);
125
126 if (active_tag_ == Y_TAG )
127 convert(std::string(s,len), y_);
128
129 if (active_tag_ == FILENAME_TAG)
130 temp_str_ = std::string(s, len);
131
132 if (active_tag_ == IMG_CAT_TAG)
133 image_category_ = std::string(s, len);
134
135 if (active_tag_ == REGION_TAG)
136 temp_str_ = std::string(s,len);
137
138 if (active_tag_ == NAME_TAG) {
139 std::string name = std::string(s,len);
140 this->trim_string(name);
141 if (name.length() != 0)
142 obj_names_.push_back(name);
143 }
144
145 if (active_tag_ == TYPE_TAG) {
146 std::string type = std::string(s,len);
147 this->trim_string(type);
148 if (type.length() != 0)
149 obj_types_.push_back(type);
150 }
151
152 if (active_tag_ == ORIENT_TAG) {
153 std::string orientation = std::string(s,len);
154 this->trim_string(orientation);
155 if (orientation.length() != 0)
156 obj_orientations_.push_back(orientation);
157 }
158
159 if (active_tag_ == LAND_TAG) {
160 std::string land = std::string(s,len);
161 this->trim_string(land);
162 if (land.length() != 0)
163 obj_land_categories_.push_back(land);
164 }
165 if (active_tag_ == HEIGHT_TAG) {
166 convert(std::string(s, len), height_);
167 }
168
169 if (active_tag_ == REFERENCE_TAG)
170 convert(std::string(s, len), reference_);
171
172 if (active_tag_ == OBJECT_MINDIST_TAG)
173 convert(std::string(s,len), min_dist_);
174
175 if (active_tag_ == OBJECT_MAXDIST_TAG)
176 convert(std::string(s,len), max_dist_);
177
178 if (active_tag_ == OBJECT_ORDER_TAG)
179 convert(std::string(s,len), order_);
180
181 if (active_tag_ == WEIGHT_TAG)
182 convert(std::string(s,len), weight_);
183
184 if (active_tag_ == IMG_NI_TAG)
185 convert(std::string(s,len), image_ni_);
186
187 if (active_tag_ == IMG_NJ_TAG)
188 convert(std::string(s,len), image_nj_);
189
190 if (active_tag_ == NLCD_TAG)
191 convert(std::string(s,len), nlcd_id_);
192
193 if (active_tag_ == FRAME_TAG)
194 convert(std::string(s,len), frame_id_);
195 }
196
trim_string(std::string & s)197 void bvgl_labelme_parser::trim_string(std::string& s)
198 {
199 char trims[4];
200 trims[0] = ' '; trims[1] = '\0'; trims[2] = '\n'; trims[3] = '\t';
201 #if 0
202 int i = (int)s.find_first_not_of(' ');
203 int j = (int)s.find_last_not_of(' ');
204 std::string t = s.substr(i,j-i+1);
205 #endif
206 std::string t = s;
207 bool trimmed = true;
208 while (trimmed) {
209 trimmed = false;
210 for (char trim : trims) {
211 std::string current = t;
212 auto i = (unsigned int)current.find_first_not_of(trim);
213 auto j = (unsigned int)current.find_last_not_of(trim);
214 if (i > j || j >= current.size()) {
215 t = ""; break;
216 }
217 else {
218 t = current.substr(i,j-i+1);
219 if (t.size() != current.size())
220 trimmed = true;
221 }
222 }
223 }
224 s = t;
225 }
226