-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.cpp
301 lines (243 loc) · 8.49 KB
/
Scene.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//--------------------------------------------------------------------------------------------------
// Implementation of the papers "Exact Acceleration of Linear Object Detectors", 12th European
// Conference on Computer Vision, 2012 and "Deformable Part Models with Individual Part Scaling",
// 24th British Machine Vision Conference, 2013.
//
// Copyright (c) 2013 Idiap Research Institute, <http://www.idiap.ch/>
// Written by Charles Dubout <[email protected]>
//
// This file is part of FFLDv2 (the Fast Fourier Linear Detector version 2)
//
// FFLDv2 is free software: you can redistribute it and/or modify it under the terms of the GNU
// Affero General Public License version 3 as published by the Free Software Foundation.
//
// FFLDv2 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 Affero
// General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with FFLDv2. If
// not, see <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------------
#include "Scene.h"
#include <algorithm>
#include <iostream>
#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)
{
}
bool Scene::empty() const
{
return ((width() <= 0) || (height() <= 0) || (depth() <= 0) || filename().empty()) &&
objects().empty();
}
template <typename Result>
static 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;
}
template<>string content<string>(const xmlNodePtr cur)
{
if ((cur == NULL) || (cur->xmlChildrenNode == NULL))
return string();
istringstream iss(reinterpret_cast<const char *>(cur->xmlChildrenNode->content));
return iss.str();
}
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 Names[80] =
{
"airplane", "apple", "backpack", "banana", "baseball bat",
"baseball glove", "bear", "bed", "bench", "bicycle", "bird",
"boat", "book", "bottle", "bowl", "broccoli", "bus", "cake",
"car", "carrot", "cat", "cell phone", "chair", "clock", "couch",
"cow", "cup", "dining table", "dog", "donut", "elephant",
"fire hydrant", "fork", "frisbee", "giraffe", "hair drier",
"handbag", "horse", "hot dog", "keyboard", "kite", "knife",
"laptop", "microwave", "motorcycle", "mouse", "orange",
"oven", "parking meter", "person", "pizza", "potted plant",
"refrigerator", "remote", "sandwich", "scissors", "sheep",
"sink", "skateboard", "skis", "snowboard", "spoon", "sports ball",
"stop sign", "suitcase", "surfboard", "teddy bear", "tennis racket",
"tie", "toaster", "toilet", "toothbrush", "traffic light", "train",
"truck", "tv", "umbrella", "vase", "wine", "zebra"
};
const string Poses[4] =
{
"Frontal", "Left", "Rear", "Right"
};
xmlDoc * doc = xmlParseFile(filename.c_str());
if (doc == NULL) {
cerr << "Could not open " << filename << endl;
return;
}
xmlNodePtr cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
xmlFreeDoc(doc);
cerr << "Could not open " << filename << endl;
return;
}
if (xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>("annotation"))) {
xmlFreeDoc(doc);
cerr << "Could not open " << filename << endl;
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/" +
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_ = content<int>(cur2);
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("height")))
height_ = content<int>(cur2);
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("depth")))
depth_ = 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 + 80, content<string>(cur2));
if (iter != Names + 80)
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, 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(content<bool>(cur2));
}
else if (!xmlStrcmp(cur2->name, reinterpret_cast<const xmlChar *>("difficult"))) {
objects_.back().setDifficult(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(content<int>(cur3));
else if (!xmlStrcmp(cur3->name, reinterpret_cast<const xmlChar *>("ymin")))
bndbox.setY(content<int>(cur3));
else if (!xmlStrcmp(cur3->name, reinterpret_cast<const xmlChar *>("xmax")))
bndbox.setWidth(content<int>(cur3));
else if (!xmlStrcmp(cur3->name, reinterpret_cast<const xmlChar *>("ymax")))
bndbox.setHeight(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;
}
ostream & FFLD::operator<<(ostream & os, const Scene & scene)
{
os << scene.width() << ' ' << scene.height() << ' ' << scene.depth() << ' '
<< scene.objects().size() << ' ' << scene.filename() << endl;
for (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;
}