File size: 6,997 Bytes
adc9971 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
#include "convert_pascal_xml.h"
#include "dlib/data_io.h"
#include <iostream>
#include <dlib/xml_parser.h>
#include <string>
#include <dlib/dir_nav.h>
#include <dlib/cmd_line_parser.h>
using namespace std;
using namespace dlib;
namespace
{
using namespace dlib::image_dataset_metadata;
// ----------------------------------------------------------------------------------------
class doc_handler : public document_handler
{
image& temp_image;
std::string& dataset_name;
std::vector<std::string> ts;
box temp_box;
public:
doc_handler(
image& temp_image_,
std::string& dataset_name_
):
temp_image(temp_image_),
dataset_name(dataset_name_)
{}
virtual void start_document (
)
{
ts.clear();
temp_image = image();
temp_box = box();
dataset_name.clear();
}
virtual void end_document (
)
{
}
virtual void start_element (
const unsigned long ,
const std::string& name,
const dlib::attribute_list&
)
{
if (ts.size() == 0 && name != "annotation")
{
std::ostringstream sout;
sout << "Invalid XML document. Root tag must be <annotation>. Found <" << name << "> instead.";
throw dlib::error(sout.str());
}
ts.push_back(name);
}
virtual void end_element (
const unsigned long ,
const std::string& name
)
{
ts.pop_back();
if (ts.size() == 0)
return;
if (name == "object" && ts.back() == "annotation")
{
temp_image.boxes.push_back(temp_box);
temp_box = box();
}
}
virtual void characters (
const std::string& data
)
{
if (ts.size() == 2 && ts[1] == "filename")
{
temp_image.filename = trim(data);
}
else if (ts.size() == 3 && ts[2] == "database" && ts[1] == "source")
{
dataset_name = trim(data);
}
else if (ts.size() >= 3)
{
if (ts[ts.size()-2] == "bndbox" && ts[ts.size()-3] == "object")
{
if (ts.back() == "xmin") temp_box.rect.left() = string_cast<double>(data);
else if (ts.back() == "ymin") temp_box.rect.top() = string_cast<double>(data);
else if (ts.back() == "xmax") temp_box.rect.right() = string_cast<double>(data);
else if (ts.back() == "ymax") temp_box.rect.bottom() = string_cast<double>(data);
}
else if (ts.back() == "name" && ts[ts.size()-2] == "object")
{
temp_box.label = trim(data);
}
else if (ts.back() == "difficult" && ts[ts.size()-2] == "object")
{
if (trim(data) == "0" || trim(data) == "false")
{
temp_box.difficult = false;
}
else
{
temp_box.difficult = true;
}
}
else if (ts.back() == "truncated" && ts[ts.size()-2] == "object")
{
if (trim(data) == "0" || trim(data) == "false")
{
temp_box.truncated = false;
}
else
{
temp_box.truncated = true;
}
}
else if (ts.back() == "occluded" && ts[ts.size()-2] == "object")
{
if (trim(data) == "0" || trim(data) == "false")
{
temp_box.occluded = false;
}
else
{
temp_box.occluded = true;
}
}
}
}
virtual void processing_instruction (
const unsigned long ,
const std::string& ,
const std::string&
)
{
}
};
// ----------------------------------------------------------------------------------------
class xml_error_handler : public error_handler
{
public:
virtual void error (
const unsigned long
) { }
virtual void fatal_error (
const unsigned long line_number
)
{
std::ostringstream sout;
sout << "There is a fatal error on line " << line_number << " so parsing will now halt.";
throw dlib::error(sout.str());
}
};
// ----------------------------------------------------------------------------------------
void parse_annotation_file(
const std::string& file,
dlib::image_dataset_metadata::image& img,
std::string& dataset_name
)
{
doc_handler dh(img, dataset_name);
xml_error_handler eh;
xml_parser::kernel_1a parser;
parser.add_document_handler(dh);
parser.add_error_handler(eh);
ifstream fin(file.c_str());
if (!fin)
throw dlib::error("Unable to open file " + file);
parser.parse(fin);
}
// ----------------------------------------------------------------------------------------
}
void convert_pascal_xml(
const command_line_parser& parser
)
{
cout << "Convert from PASCAL XML annotation format..." << endl;
dlib::image_dataset_metadata::dataset dataset;
std::string name;
dlib::image_dataset_metadata::image img;
const std::string filename = parser.option("c").argument();
// make sure the file exists so we can use the get_parent_directory() command to
// figure out it's parent directory.
make_empty_file(filename);
const std::string parent_dir = get_parent_directory(file(filename)).full_name();
for (unsigned long i = 0; i < parser.number_of_arguments(); ++i)
{
try
{
parse_annotation_file(parser[i], img, name);
const string root = get_parent_directory(get_parent_directory(file(parser[i]))).full_name();
const string img_path = root + directory::get_separator() + "JPEGImages" + directory::get_separator();
dataset.name = name;
img.filename = strip_path(img_path + img.filename, parent_dir);
dataset.images.push_back(img);
}
catch (exception& )
{
cout << "Error while processing file " << parser[i] << endl << endl;
throw;
}
}
save_image_dataset_metadata(dataset, filename);
}
|