Calibration Library 1.0.0
A C++ library for camera calibration and vision-related geometric transformations
Loading...
Searching...
No Matches
loaders.cpp
Go to the documentation of this file.
2
3// std
4#include <fstream>
5#include <stdexcept>
6
7// third-party
8#include <nlohmann/json.hpp>
9
10namespace calib::pipeline {
11
12namespace {
13
14struct LoadedPlanarSource final {
15 PlanarDetections detections;
16 nlohmann::json source_summary;
17 nlohmann::json raw_payload;
18};
19
20auto load_planar_source(const JsonPlanarDatasetLoader::Entry& entry) -> LoadedPlanarSource {
21 std::ifstream stream(entry.path);
22 if (!stream) {
23 throw std::runtime_error("JsonPlanarDatasetLoader: failed to open " + entry.path.string());
24 }
25
26 nlohmann::json json_data;
27 stream >> json_data;
28
29 auto detections = json_data.get<PlanarDetections>();
30 detections.source_file = entry.path;
31
32 if (entry.sensor_id.has_value() && detections.sensor_id != *entry.sensor_id) {
33 throw std::runtime_error("Requested sensor_id '" + *entry.sensor_id +
34 "' not found in dataset.");
35 }
36
37 nlohmann::json source_info{{"path", entry.path.string()}, {"sensor_id", detections.sensor_id}};
38 if (!detections.metadata.is_null()) {
39 source_info["detector"] = detections.metadata.value("detector", nlohmann::json::object());
40 }
41
42 return LoadedPlanarSource{
43 .detections = std::move(detections),
44 .source_summary = std::move(source_info),
45 .raw_payload = std::move(json_data),
46 };
47}
48
49} // namespace
50
51void JsonPlanarDatasetLoader::add_entry(const std::filesystem::path& path,
52 std::optional<std::string> sensor_id) {
53 entries_.push_back(Entry{path, std::move(sensor_id)});
54}
55
57 if (entries_.empty()) {
58 throw std::runtime_error("JsonPlanarDatasetLoader: no dataset entries configured.");
59 }
60
61 CalibrationDataset dataset;
62 dataset.metadata = nlohmann::json::object();
63 dataset.metadata["sources"] = nlohmann::json::array();
64 dataset.raw_json = nlohmann::json::object();
65
66 for (const auto& entry : entries_) {
67 auto loaded = load_planar_source(entry);
68 dataset.metadata["sources"].push_back(std::move(loaded.source_summary));
69 dataset.raw_json[entry.path.string()] = std::move(loaded.raw_payload);
70 dataset.planar_cameras.push_back(std::move(loaded.detections));
71 }
72
73 dataset.schema_version = 1;
74 return dataset;
75}
76
77} // namespace calib::pipeline
auto load() -> CalibrationDataset override
Load and validate all configured dataset entries.
Definition loaders.cpp:56
void add_entry(const std::filesystem::path &path, std::optional< std::string > sensor_id=std::nullopt)
Append a dataset file to the loader queue.
Definition loaders.cpp:51
PlanarDetections detections
Definition loaders.cpp:15
nlohmann::json source_summary
Definition loaders.cpp:16
nlohmann::json raw_payload
Definition loaders.cpp:17
Aggregated dataset consumed by the calibration pipeline.
Definition dataset.h:44
nlohmann::json raw_json
Original dataset payloads keyed by source path.
Definition dataset.h:48
std::vector< PlanarDetections > planar_cameras
Definition dataset.h:47
Description of a JSON dataset entry on disk.
Definition loaders.h:24
std::filesystem::path source_file
Definition dataset.h:37