-
Notifications
You must be signed in to change notification settings - Fork 4
/
GroundTruthLoader.cpp
128 lines (100 loc) · 3.78 KB
/
GroundTruthLoader.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
#include <iostream>
#include <fstream>
#include <libcohog.hpp>
#include <tinyxml2.h>
#include "GroundTruthLoader.hpp"
std::string libcohog::filepath_to_filename(const std::string& filepath)
{
int p;
if((p = filepath.find_last_of("/")) != std::string::npos || (p = filepath.find_last_of("\\")) != std::string::npos)
return filepath.substr(p + 1);
return filepath;
}
std::map<std::string, std::vector<libcohog::TruthRect> > libcohog::load_daimler_ground_truth(const char* filename, int min_h, bool only_confident)
{
std::ifstream is(filename);
std::map<std::string, std::vector<libcohog::TruthRect> > result;
//シーケンス先頭まで読み飛ばす
while(!is.eof() && is.get() != ':')
;
//ヘッダの読み込み
std::string name;
std::string path;
int cnt;
is >> name >> path >> cnt;
//データの読み込み
for(int i = 0; i < cnt; i++)
{
//データのあるところ(';'文字で始まる)まで読み込み
while(!is.eof() && is.get() != ';')
;
std::string img_name;
int w, h;
int ignore, n;
is >> img_name >> w >> h;
is >> ignore >> n;
std::vector<libcohog::TruthRect> rects;
for(int j = 0; j < n; ++j)
{
libcohog::TruthRect rect;
//#の行を1行読み込む(読み捨て)
//※この時点でカーソルは前の行の末にあるかもしれない
char c;
int categ;
is >> c >> categ;
int obj_id, unique_id;
float confidence;
is >> obj_id >> unique_id >> confidence;
is >> rect.rect.x >> rect.rect.y >> rect.rect.width >> rect.rect.height;
is >> ignore;
rect.rect.width -= rect.rect.x;
rect.rect.height -= rect.rect.y;
if(rect.rect.height < min_h || (only_confident && confidence < 1.0) || categ != 0)
rect.confident = false;
else
rect.confident = true;
rects.push_back(rect);
}
result[img_name] = rects;
}
return result;
}
std::map<std::string, std::vector<libcohog::TruthRect> > libcohog::load_rectan_ground_truth(const char *filename, int min_h, const std::set<int>& category)
{
using namespace tinyxml2;
std::map<std::string, std::vector<libcohog::TruthRect> > result;
XMLDocument xml;
xml.LoadFile(filename);
XMLElement* images = xml.FirstChildElement("images");
const std::string path = images->Attribute("path");
//画像ごとに回す
for(XMLElement* img = images->FirstChildElement("image");
img;
img = img->NextSiblingElement("image"))
{
std::vector<libcohog::TruthRect> rects;
//属性読み取り
const std::string src = img->Attribute("src");
const std::string img_name = filepath_to_filename(src); //boost::filesystem::path(src).filename().string();
//子要素(rect)があれば
for(XMLElement* rct = img->FirstChildElement("rect");
rct;
rct = rct->NextSiblingElement("rect"))
{
int categ;
libcohog::TruthRect rect;
rect.rect.x = std::atoi(rct->Attribute("x"));
rect.rect.y = std::atoi(rct->Attribute("y"));
rect.rect.width = std::atoi(rct->Attribute("w"));
rect.rect.height = std::atoi(rct->Attribute("h"));
categ = std::atoi(rct->Attribute("category"));
if(rect.rect.height <= min_h || category.find(categ) == category.end())
rect.confident = false;
else
rect.confident = true;
rects.push_back(rect);
}
result[img_name] = rects;
}
return result;
}