-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalVocXMLSaver.cpp
More file actions
202 lines (168 loc) · 7.01 KB
/
Copy pathPascalVocXMLSaver.cpp
File metadata and controls
202 lines (168 loc) · 7.01 KB
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
// Copyright 2016-2017 Annotator Team
#define Annotator_AnnotatorLib_Saver_PascalVocXMLSaver_BODY
/************************************************************
XMLSaver class body
************************************************************/
// include associated header file
#include "PascalVocXMLSaver.h"
#include <AnnotatorLib/Algo/InterpolateAnnotation.h>
#include <AnnotatorLib/Frame.h>
#include <AnnotatorLib/Object.h>
#include <AnnotatorLib/Session.h>
#include <fstream>
#include <memory>
#include <unordered_map>
#include <boost/filesystem.hpp>
#include <Poco/DOM/DOMWriter.h>
#include <Poco/DOM/Text.h>
#include <Poco/XML/XMLWriter.h>
using namespace std;
void PascalVocXMLSaver::saveFrame(const AnnotatorLib::Session *session,
const shared_ptr<AnnotatorLib::Frame> frame) {
std::string number = std::to_string(frame->getFrameNumber());
// prepend zeros
number = std::string(8 - number.length(), '0') + number;
boost::filesystem::path image_path(
project->getImageSet()->getImagePath(frame->getFrameNumber()));
std::string filename =
path + "/" + image_path.filename().stem().string() + ".xml";
std::ofstream ostr(filename, std::ios::out);
Poco::XML::DOMWriter writer;
writer.setNewLine("\n");
writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);
document = new Poco::XML::Document;
Poco::AutoPtr<Poco::XML::Element> root =
document->createElement("annotation");
// folder
Poco::AutoPtr<Poco::XML::Element> folder = document->createElement("folder");
if (project) {
folder->appendChild(
document->createTextNode(image_path.parent_path().filename().string()));
} else
folder->appendChild(document->createTextNode("."));
root->appendChild(folder);
// filename
Poco::AutoPtr<Poco::XML::Element> f = document->createElement("filename");
if (project) {
f->appendChild(
document->createTextNode(image_path.filename().stem().string()));
} else
f->appendChild(document->createTextNode(number));
root->appendChild(f);
// path
Poco::AutoPtr<Poco::XML::Element> p = document->createElement("path");
if (project)
p->appendChild(document->createTextNode(
project->getImageSet()->getImagePath(frame->getFrameNumber())));
else
p->appendChild(document->createTextNode(path + "/" + number));
root->appendChild(p);
// source
Poco::AutoPtr<Poco::XML::Element> source = document->createElement("source");
Poco::AutoPtr<Poco::XML::Element> database =
document->createElement("database");
database->appendChild(document->createTextNode("Unknown"));
source->appendChild(database);
root->appendChild(source);
// size
Poco::AutoPtr<Poco::XML::Element> size = document->createElement("size");
// width
Poco::AutoPtr<Poco::XML::Element> size_width =
document->createElement("width");
int w = project->getImageSet()->getImage(frame->getFrameNumber()).cols;
size_width->appendChild(document->createTextNode(std::to_string(w)));
size->appendChild(size_width);
// height
Poco::AutoPtr<Poco::XML::Element> size_height =
document->createElement("height");
int h = project->getImageSet()->getImage(frame->getFrameNumber()).rows;
size_height->appendChild(document->createTextNode(std::to_string(h)));
size->appendChild(size_height);
// depth
Poco::AutoPtr<Poco::XML::Element> size_depth =
document->createElement("depth");
int d = project->getImageSet()->getImage(frame->getFrameNumber()).channels();
size_depth->appendChild(document->createTextNode(std::to_string(d)));
size->appendChild(size_depth);
root->appendChild(size);
// segmented
Poco::AutoPtr<Poco::XML::Element> segmented =
document->createElement("segmented");
segmented->appendChild(document->createTextNode("0"));
root->appendChild(segmented);
// attributes
for (auto &attribute : frame->getAttributes()) {
Poco::AutoPtr<Poco::XML::Element> attrElement =
document->createElement(attribute.second->getName());
attrElement->appendChild(
document->createTextNode(attribute.second->getValue()->toString()));
root->appendChild(attrElement);
}
// object
for (auto &pair : session->getObjects()) {
if (session->getAnnotation(frame, pair.second)) // appears in frame?
root->appendChild(fromObject(session, pair.second, frame));
}
document->appendChild(root);
writer.writeNode(ostr, document);
}
void PascalVocXMLSaver::saveAnnotation(AnnotatorLib::Annotation) {}
void PascalVocXMLSaver::setPath(std::string path) { this->path = path; }
AnnotatorLib::StorageType PascalVocXMLSaver::getType() {
return AnnotatorLib::StorageType::PASCALVOCXML;
}
void PascalVocXMLSaver::saveSession(const AnnotatorLib::Session *session) {
for (auto &pair : session->getFrames()) {
saveFrame(session, pair.second);
}
}
void PascalVocXMLSaver::saveProject(
std::shared_ptr<AnnotatorLib::Project> project) {
this->project = project;
saveSession(project->getSession().get());
}
bool PascalVocXMLSaver::close() { return true; }
Poco::AutoPtr<Poco::XML::Element> PascalVocXMLSaver::fromObject(
const AnnotatorLib::Session *session,
const shared_ptr<AnnotatorLib::Object> object,
const shared_ptr<AnnotatorLib::Frame> frame) {
Poco::AutoPtr<Poco::XML::Element> element = document->createElement("object");
// name
Poco::AutoPtr<Poco::XML::Element> name = document->createElement("name");
name->appendChild(document->createTextNode(object->getClass()->getName()));
element->appendChild(name);
// bndbox
shared_ptr<AnnotatorLib::Annotation> annotation =
session->getAnnotation(frame, object);
Poco::AutoPtr<Poco::XML::Element> bndbox = document->createElement("bndbox");
Poco::AutoPtr<Poco::XML::Element> xmin = document->createElement("xmin");
xmin->appendChild(
document->createTextNode(std::to_string((int)annotation->getX())));
Poco::AutoPtr<Poco::XML::Element> ymin = document->createElement("ymin");
ymin->appendChild(
document->createTextNode(std::to_string((int)annotation->getY())));
Poco::AutoPtr<Poco::XML::Element> xmax = document->createElement("xmax");
xmax->appendChild(document->createTextNode(
std::to_string((int)(annotation->getX() + annotation->getWidth()))));
Poco::AutoPtr<Poco::XML::Element> ymax = document->createElement("ymax");
ymax->appendChild(document->createTextNode(
std::to_string((int)(annotation->getY() + annotation->getHeight()))));
bndbox->appendChild(xmin);
bndbox->appendChild(ymin);
bndbox->appendChild(xmax);
bndbox->appendChild(ymax);
element->appendChild(bndbox);
// attributes
for (shared_ptr<AnnotatorLib::Attribute> attribute :
annotation->getAttributes()) {
Poco::AutoPtr<Poco::XML::Element> attrElement =
document->createElement(attribute->getName());
attrElement->appendChild(
document->createTextNode(attribute->getValue()->toString()));
element->appendChild(attrElement);
}
return element;
}
/************************************************************
End of XMLSaver class body
************************************************************/