-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathScene.cpp
More file actions
273 lines (219 loc) · 7.01 KB
/
Scene.cpp
File metadata and controls
273 lines (219 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//--------------------------------------------------------------------------------------------------
// Implementation of the paper "Exact Acceleration of Linear Object Detectors", 12th European
// Conference on Computer Vision, 2012.
//
// Copyright (c) 2012 Idiap Research Institute, <http://www.idiap.ch/>
// Written by Charles Dubout <charles.dubout@idiap.ch>
//
// This file is part of FFLD (the Fast Fourier Linear Detector)
//
// FFLD is free software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License version 3 as published by the Free Software Foundation.
//
// FFLD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with FFLD. If not, see
// <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------------
#include "Scene.h"
#include <algorithm>
#include <sstream>
#include <libxml/parser.h>
using namespace FFLD;
using namespace std;
Scene::Scene() : width_(0), height_(0), depth_(0)
{
}
Scene::Scene(int width, int height, int depth, const string & filename,
const vector<Object> & objects) : width_(width), height_(height), depth_(depth),
filename_(filename), objects_(objects)
{
}
namespace FFLD
{
namespace detail
{
template <typename Result>
inline Result content(const xmlNodePtr cur)
{
if ((cur == NULL) || (cur->xmlChildrenNode == NULL))
return Result();
istringstream iss(reinterpret_cast<const char *>(cur->xmlChildrenNode->content));
Result result;
iss >> result;
return result;
}
}
}
Scene::Scene(const string & filename)
{
const string Names[20] =
{
"aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow",
"diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa",
"train", "tvmonitor"
};
const string Poses[4] =
{
"Frontal", "Left", "Rear", "Right"
};
xmlDoc * doc = xmlParseFile(filename.c_str());
if (doc == NULL)
return;
xmlNodePtr cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>("annotation"))) {
xmlFreeDoc(doc);
return;
}
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>("filename"))) {
// Full path
size_t last = filename.rfind('/');
if (last != string::npos) {
last = filename.rfind('/', last - 1);
if (last != string::npos)
filename_ = filename.substr(0, last) + "/JPEGImages/" +
detail::content<string>(cur);
}
}
else if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>("size"))) {
xmlNodePtr cur2 = cur->xmlChildrenNode;
while (cur2 != NULL) {
if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("width")))
width_ = detail::content<int>(cur2);
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("height")))
height_ = detail::content<int>(cur2);
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("depth")))
depth_ = detail::content<int>(cur2);
cur2 = cur2->next;
}
}
else if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>("object"))) {
objects_.push_back(Object());
xmlNodePtr cur2 = cur->xmlChildrenNode;
while (cur2 != NULL) {
if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("name"))) {
const string * iter =
find(Names, Names + 20, detail::content<string>(cur2));
if (iter != Names + 20)
objects_.back().setName(static_cast<Object::Name>(iter - Names));
}
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("pose"))) {
const string * iter =
find(Poses, Poses + 4, detail::content<string>(cur2));
if (iter != Poses + 4)
objects_.back().setPose(static_cast<Object::Pose>(iter - Poses));
}
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("truncated"))) {
objects_.back().setTruncated(detail::content<bool>(cur2));
}
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("difficult"))) {
objects_.back().setDifficult(detail::content<bool>(cur2));
}
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("bndbox"))) {
Rectangle bndbox;
xmlNodePtr cur3 = cur2->xmlChildrenNode;
while (cur3 != NULL) {
if (!xmlStrcmp(cur3->name, reinterpret_cast<const xmlChar *>("xmin")))
bndbox.setX(detail::content<int>(cur3));
else if (!xmlStrcmp(cur3->name, reinterpret_cast<const xmlChar *>("ymin")))
bndbox.setY(detail::content<int>(cur3));
else if (!xmlStrcmp(cur3->name, reinterpret_cast<const xmlChar *>("xmax")))
bndbox.setWidth(detail::content<int>(cur3));
else if (!xmlStrcmp(cur3->name, reinterpret_cast<const xmlChar *>("ymax")))
bndbox.setHeight(detail::content<int>(cur3));
cur3 = cur3->next;
}
// Only set the bounding box if all values have been assigned
if (bndbox.x() && bndbox.y() && bndbox.width() && bndbox.height()) {
bndbox.setX(bndbox.x() - 1);
bndbox.setY(bndbox.y() - 1);
bndbox.setWidth(bndbox.width() - bndbox.x());
bndbox.setHeight(bndbox.height() - bndbox.y());
objects_.back().setBndbox(bndbox);
}
}
cur2 = cur2->next;
}
}
cur = cur->next;
}
xmlFreeDoc(doc);
}
int Scene::width() const
{
return width_;
}
void Scene::setWidth(int width)
{
width_ = width;
}
int Scene::height() const
{
return height_;
}
void Scene::setHeight(int height)
{
height_ = height;
}
int Scene::depth() const
{
return depth_;
}
void Scene::setDepth(int depth)
{
depth_ = depth;
}
const string & Scene::filename() const
{
return filename_;
}
void Scene::setFilename(const string &filename)
{
filename_ = filename;
}
const vector<Object> & Scene::objects() const
{
return objects_;
}
void Scene::setObjects(const vector<Object> &objects)
{
objects_ = objects;
}
bool Scene::empty() const
{
return ((width() <= 0) || (height() <= 0) || (depth() <= 0) || filename().empty()) &&
objects().empty();
}
ostream & FFLD::operator<<(ostream & os, const Scene & scene)
{
os << scene.width() << ' ' << scene.height() << ' ' << scene.depth() << ' '
<< scene.objects().size() << ' ' << scene.filename() << endl;
for (unsigned int i = 0; i < scene.objects().size(); ++i)
os << scene.objects()[i] << endl;
return os;
}
istream & FFLD::operator>>(istream & is, Scene & scene)
{
int width, height, depth, nbObjects;
is >> width >> height >> depth >> nbObjects;
is.get(); // Remove the space
string filename;
getline(is, filename);
vector<Object> objects(nbObjects);
for (int i = 0; i < nbObjects; ++i)
is >> objects[i];
if (!is) {
scene = Scene();
return is;
}
scene = Scene(width, height, depth, filename, objects);
return is;
}