licornea_tools
dataset.cc
Go to the documentation of this file.
1 #include "dataset.h"
2 #include "string.h"
3 #include "filesystem.h"
4 #include "os.h"
5 #include <format.h>
6 #include <stdexcept>
7 #include <fstream>
8 #include <string>
9 #include <ostream>
10 
11 namespace tlz {
12 
13 int dataset_view::local_filename_x_() const {
14  float factor = get_or(local_parameters(), "filename_x_index_factor", 1.0);
15  int offset = get_or(local_parameters(), "filename_x_index_offset", 0);
16 
17  int loc_x = x_;
18  loc_x *= factor;
19  loc_x += offset;
20  return loc_x;
21 }
22 
23 int dataset_view::local_filename_y_() const {
24  float factor = get_or(local_parameters(), "filename_y_index_factor", 1.0);
25  int offset = get_or(local_parameters(), "filename_y_index_offset", 0);
26 
27  int loc_y = y_;
28  loc_y *= factor;
29  loc_y += offset;
30  return loc_y;
31 }
32 
33 std::string dataset_view::format_name(const std::string& tpl) const {
34  if(dataset_.is_2d()) return fmt::format(tpl, fmt::arg("x", x_), fmt::arg("y", y_));
35  else return fmt::format(tpl, fmt::arg("x", x_));
36 }
37 
38 std::string dataset_view::format_filename(const std::string& tpl) const {
39  std::string relpath;
40  if(dataset_.is_2d()) relpath = fmt::format(tpl, fmt::arg("x", local_filename_x_()), fmt::arg("y", local_filename_y_()));
41  else relpath = fmt::format(tpl, fmt::arg("x", local_filename_x_()));
42  return dataset_.filepath(relpath);
43 }
44 
45 dataset_view::dataset_view(const dataset& datas, int x, int y, const std::string& grp) :
46  dataset_(datas), x_(x), y_(y), group_(grp) { }
47 
48 
50  if(group_.empty()) return dataset_.parameters();
51  else return dataset_.parameters()[group_];
52 }
53 
54 std::string dataset_view::local_filename(const std::string& name, const std::string& def) const {
55  std::string tpl = get_or(local_parameters(), name, std::string());
56  if(! tpl.empty()) return format_filename(tpl);
57  else return def;
58 }
59 
60 std::string dataset_view::camera_name() const {
61  return format_name(dataset_.parameters()["camera_name_format"]);
62 }
63 
64 std::string dataset_view::image_filename() const {
65  return local_filename("image_filename_format");
66 }
67 
68 std::string dataset_view::depth_filename() const {
69  return local_filename("depth_filename_format");
70 }
71 
72 std::string dataset_view::mask_filename() const {
73  return local_filename("mask_filename_format");
74 }
75 
76 std::string dataset_view::group() const {
77  return group_;
78 }
79 
80 dataset_view dataset_view::group_view(const std::string& grp) const {
81  if(grp.empty()) return dataset_view(dataset_, x_, y_);
82  else if(has(dataset_.parameters(), grp)) return dataset_view(dataset_, x_, y_, grp);
83  else throw std::runtime_error("no group '" + grp + "' in dataset");
84 }
85 
87 
88 dataset_group::dataset_group(const dataset& datas, const std::string& grp) :
89  dataset_(datas), group_(grp) { }
90 
92  if(group_.empty()) return dataset_.parameters();
93  else return dataset_.parameters()[group_];
94 }
95 
97  if(has(parameters(), "border")) return decode_border(parameters()["border"]);
98  else return border();
99 }
100 
102  return add_border(image_border(), dataset_.image_size());
103 }
104 
106  return dataset_.view(x).group_view(group_);
107 }
108 
109 dataset_view dataset_group::view(int x, int y) const {
110  return dataset_.view(x, y).group_view(group_);
111 }
112 
114  return dataset_.view(idx).group_view(group_);
115 }
116 
118 
119 
120 dataset::dataset(const std::string& parameters_filename) {
121  std::size_t last_sep_pos = parameters_filename.find_last_of('/');
122  if(last_sep_pos == std::string::npos) dirname_ = "./";
123  else dirname_ = parameters_filename.substr(0, last_sep_pos + 1);
124 
125  std::ifstream stream(parameters_filename);
126  stream >> parameters_;
127 
128  for(int v : parameters_["x_index_range"]) x_index_range_.push_back(v);
129  if(parameters_.count("y_index_range") == 1)
130  for(int v : parameters_["y_index_range"]) y_index_range_.push_back(v);
131 }
132 
133 bool dataset::is_1d() const {
134  return (y_index_range_.size() == 0);
135 }
136 
137 bool dataset::is_2d() const {
138  return (y_index_range_.size() > 0);
139 }
140 
141 std::string dataset::filepath(const std::string& relpath) const {
142  if(relpath.front() == '/') return relpath;
143  #ifdef LICORNEA_OS_WINDOWS
144  if((relpath.size() > 2) && (relpath[1] == ':')) return relpath;
145  #endif
146  return filename_append(dirname_, relpath);
147 }
148 
149 std::string dataset::cameras_filename() const {
150  return filepath(parameters_["cameras_filename"]);
151 }
152 
153 int dataset::image_width() const {
154  return parameters_["width"];
155 }
156 
158  return parameters_["height"];
159 }
160 
161 cv::Size dataset::image_size() const {
162  return cv::Size(image_width(), image_height());
163 }
164 
165 int dataset::x_min() const {
166  return x_index_range_[0];
167 }
168 
169 int dataset::x_max() const {
170  return x_index_range_[1];
171 }
172 
173 int dataset::x_step() const {
174  if(x_index_range_.size() == 3) return x_index_range_[2];
175  else return 1;
176 }
177 
178 bool dataset::x_valid(int x) const {
179  return (x >= x_min()) && (x <= x_max()) && (((x - x_min()) % x_step()) == 0);
180 }
181 
182 int dataset::x_count() const {
183  return (x_max() - x_min() + x_step()) / x_step();
184 }
185 
186 int dataset::x_mid() const {
187  return x_min() + (((x_max() - x_min()) / (2 * x_step())) * x_step());
188 }
189 
190 std::vector<int> dataset::x_indices() const {
191  std::vector<int> indices;
192  for(int x = x_min(); x <= x_max(); x += x_step()) indices.push_back(x);
193  return indices;
194 }
195 
196 int dataset::y_min() const {
197  if(is_2d()) return y_index_range_[0];
198  else return 0;
199 }
200 
201 int dataset::y_max() const {
202  if(is_2d()) return y_index_range_[1];
203  else return 0;
204 }
205 
206 int dataset::y_step() const {
207  if(is_2d() && (y_index_range_.size() == 3)) return y_index_range_[2];
208  else return 1;
209 }
210 
211 bool dataset::y_valid(int y) const {
212  return (y >= y_min()) && (y <= y_max()) && (((y - y_min()) % y_step()) == 0);
213 }
214 
215 std::vector<int> dataset::y_indices() const {
216  std::vector<int> indices;
217  for(int y = y_min(); y <= y_max(); y += y_step()) indices.push_back(y);
218  return indices;
219 }
220 
221 int dataset::y_count() const {
222  if(is_2d()) return (y_max() - y_min() + y_step()) / y_step();
223  else return 1;
224 }
225 
226 int dataset::y_mid() const {
227  if(is_2d()) return y_min() + (((y_max() - y_min()) / (2 * y_step())) * y_step());
228  else return 0;
229 }
230 
231 bool dataset::valid(view_index idx) const {
232  return x_valid(idx.x) && y_valid(idx.y);
233 }
234 
235 std::vector<view_index> dataset::indices() const {
236  std::vector<view_index> list;
237  for(int x = x_min(); x <= x_max(); x += x_step())
238  for(int y = y_min(); y <= y_max(); y += y_step())
239  list.emplace_back(x, y);
240  return list;
241 }
242 
244  if(is_2d()) throw std::runtime_error("must specify y view index, for 2d dataset");
245  if(! x_valid(x)) throw std::runtime_error("x view index out of range");
246  return dataset_view(*this, x, 0);
247 }
248 
249 dataset_view dataset::view(int x, int y) const {
250  if(! x_valid(x)) throw std::runtime_error("x view index out of range");
251  if(! y_valid(y)) throw std::runtime_error("y view index out of range");
252  return dataset_view(*this, x, y);
253 }
254 
256  if(is_2d()) {
257  if(! idx.is_2d()) throw std::runtime_error("must specify 2d view index");
258  return view(idx.x, idx.y);
259  } else {
260  if(idx.is_2d() && idx.y != 0) throw std::runtime_error("must specify 1d view index");
261  return view(idx.x);
262  }
263 }
264 
265 dataset_group dataset::group(const std::string& grp) const {
266  if(grp.empty()) return dataset_group(*this, "");
267  else if(has(parameters_, grp)) return dataset_group(*this, grp);
268  else throw std::runtime_error("no group '" + grp + "' in dataset");
269 }
270 
271 
273 
274 
275 std::string encode_view_index(view_index idx) {
276  std::string key = std::to_string(idx.x);
277  if(idx.is_2d()) key += "," + std::to_string(idx.y);
278  return key;
279 }
280 
281 view_index decode_view_index(const std::string& key) {
282  view_index idx;
283  auto j_idx = explode_from_string<int>(',', key);
284  idx.x = j_idx[0];
285  if(j_idx.size() == 2) idx.y = j_idx[1];
286  else idx.y = -1;
287  return idx;
288 }
289 
290 std::ostream& operator<<(std::ostream& stream, const view_index& idx) {
291  if(idx.is_2d()) stream << '(' << idx.x << ", " << idx.y << ')';
292  else stream << '(' << idx.x << ')';
293  return stream;
294 }
295 
296 
298  std::cout << "loading dataset parameters" << std::endl;
299  return dataset(in_filename_arg());
300 }
301 
302 
303 
304 }
dataset_group(const dataset &, const std::string &grp)
Definition: dataset.cc:88
std::string filename_append(const std::string &a, const std::string &b)
bool has(const json &j, const std::string &key)
Definition: json.h:24
cv::Size image_size_with_border() const
Definition: dataset.cc:101
int y_count() const
Definition: dataset.cc:221
std::string mask_filename() const
Definition: dataset.cc:72
int x_step() const
Definition: dataset.cc:173
std::string image_filename() const
Definition: dataset.cc:64
std::string encode_view_index(view_index idx)
Definition: dataset.cc:275
const int border
std::string in_filename_arg()
Definition: args.cc:98
bool is_2d() const
Definition: dataset.cc:137
border image_border() const
Definition: dataset.cc:96
int x_max() const
Definition: dataset.cc:169
int x_min() const
Definition: dataset.cc:165
const json & parameters() const
Definition: dataset.h:86
std::string cameras_filename() const
Definition: dataset.cc:149
bool is_1d() const
Definition: dataset.cc:133
std::vector< int > y_indices() const
Definition: dataset.cc:215
int image_width() const
Definition: dataset.cc:153
dataset_view view(int x) const
Definition: dataset.cc:105
bool x_valid(int x) const
Definition: dataset.cc:178
border decode_border(const json &j_bord)
Definition: border.cc:15
int x_mid() const
Definition: dataset.cc:186
dataset_view group_view(const std::string &name) const
Definition: dataset.cc:80
std::ostream & operator<<(std::ostream &, const view_index &)
Definition: dataset.cc:290
dataset_view view(int x) const
Definition: dataset.cc:243
bool valid(view_index) const
Definition: dataset.cc:231
std::string depth_filename() const
Definition: dataset.cc:68
dataset(const std::string &parameters_filename)
Definition: dataset.cc:120
int x() const
Definition: dataset.h:33
dataset dataset_arg()
Definition: dataset.cc:297
int image_height() const
Definition: dataset.cc:157
bool y_valid(int y) const
Definition: dataset.cc:211
std::vector< int > x_indices() const
Definition: dataset.cc:190
std::string local_filename(const std::string &name, const std::string &def="") const
Definition: dataset.cc:54
int y_step() const
Definition: dataset.cc:206
int y() const
Definition: dataset.h:34
const json & parameters() const
Definition: dataset.cc:91
std::string group() const
Definition: dataset.cc:76
std::string camera_name() const
Definition: dataset.cc:60
cv::Size add_border(const border &bord, const cv::Size &sz)
Definition: border.cc:24
const json & local_parameters() const
Definition: dataset.cc:49
std::string to_string(const T &)
int x_count() const
Definition: dataset.cc:182
int y_max() const
Definition: dataset.cc:201
view_index decode_view_index(const std::string &key)
Definition: dataset.cc:281
cv::Size image_size() const
Definition: dataset.cc:161
nlohmann::json json
Definition: json.h:11
T get_or(const json &j, const std::string &key, const T &default_value)
Definition: json.h:28
std::string filepath(const std::string &relpath) const
Definition: dataset.cc:141
dataset_view(const dataset &, int x, int y, const std::string &grp="")
Definition: dataset.cc:45
int y_min() const
Definition: dataset.cc:196
dataset_group group(const std::string &grp) const
Definition: dataset.cc:265
std::vector< view_index > indices() const
Definition: dataset.cc:235
bool is_2d() const
Definition: common.h:121
int y_mid() const
Definition: dataset.cc:226