ReUseX  0.0.5
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
IDataset.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Povl Filip Sonne-Frederiksen
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4#pragma once
5#include "reusex/vision/IData.hpp"
6
7#include <opencv2/core/mat.hpp>
8
9#include <filesystem>
10#include <memory>
11#include <span>
12#include <string>
13#include <vector>
14
15// Forward declaration
16namespace reusex {
17class ProjectDB;
18}
19
20namespace reusex::vision {
21/* Interface for datasets. A dataset is a collection of data samples, where each
22 * sample consists of an image and a label. The dataset is stored in a SQLite
23 * database, where each sample is stored as a row in a table. The table has the
24 * following columns: - id: an integer primary key that uniquely identifies the
25 * sample - image: a blob that contains the image data - label: an integer that
26 * represents the label of the sample. The dataset provides methods for
27 * retrieving samples and saving new samples to the database. The get method
28 * retrieves a sample by its index, and the save method saves a batch of samples
29 * to the database. The dataset also provides methods for retrieving and saving
30 * images, which are used internally by the get and save methods. The dataset is
31 * designed to be used with the IData interface, which represents a single data
32 * sample. The IData interface provides methods for accessing the image and
33 * label of a sample, and for saving the sample to the database. The dataset is
34 * intended to be used in machine learning applications, where it can be used to
35 * train and evaluate models on a collection of labeled images. */
36class IDataset {
37 public:
38 /* A pair of a data sample and its index. The data sample is represented as a
39 * unique pointer to an IData object, and the index is a size_t that
40 * represents the position of the sample in the dataset. The get method
41 * returns a Pair, which allows the caller to access both the data sample
42 * and its index. The save method takes a span of Pairs, which allows the
43 * caller to save a batch of samples to the database. */
44 using Pair = std::pair<std::unique_ptr<IData>, size_t>;
45
46 /* Constructs a new IDataset object with a shared database instance.
47 *
48 * This constructor allows multiple IDataset instances to share the same
49 * database connection. The database is managed by shared_ptr, so it will
50 * remain open as long as any IDataset instance references it.
51 *
52 * @param database Shared pointer to ProjectDB instance
53 */
54 explicit IDataset(std::shared_ptr<ProjectDB> database);
55
56 /* Constructs a new IDataset object by opening a database at the given path.
57 *
58 * This convenience constructor creates a new ProjectDB instance internally
59 * and stores it as a shared_ptr. The database connection is managed by the
60 * IDataset and will be closed when the last reference is destroyed.
61 *
62 * @param dbPath The path to the ReUseX project database file.
63 */
64 explicit IDataset(std::filesystem::path dbPath);
65
66 /* Virtual destructor to ensure proper cleanup of derived classes. */
67 virtual ~IDataset() = default;
68
69 /* Returns the number of samples in the dataset. The size method returns the
70 * number of samples in the dataset, which is equal to the size of the ids_
71 * vector. The size method is used by the caller to determine how many samples
72 * are available in the dataset, and to iterate over the samples using their
73 * indices. The size method is a const method, which means that it does not
74 * modify the state of the IDataset object.
75 * @return The number of samples in the dataset.
76 */
77 size_t size() const;
78
79 /* Returns the RTABMap node id backing the sample at @p index.
80 *
81 * The dataset maps contiguous dataset indices (0, 1, 2, ...) onto the
82 * ascending list of sensor-frame node ids cached at construction. Exposing
83 * the mapping lets ordered/stateful consumers (e.g. the video-tracker
84 * annotation path) detect sequence boundaries: within a single scan node ids
85 * increase monotonically, so a non-increasing delta between consecutive
86 * indices signals the start of a new concatenated sequence.
87 *
88 * @param index The dataset index whose node id to return.
89 * @return The node id at @p index (throws std::out_of_range if invalid).
90 */
91 int node_id(const std::size_t index) const;
92
102
119
120 /* Retrieves a sample by its index. The get method takes an index as input,
121 * which is used to look up the corresponding sample ID in the ids_ vector.
122 * The get method then retrieves the image and label for the sample from the
123 * database, and returns a Pair containing a unique pointer to an IData object
124 * that represents the sample, and the index of the sample in the dataset. The
125 * get method is a const method, which means that it does not modify the state
126 * of the IDataset object. The get method is a pure virtual method, which
127 * means that it must be implemented by derived classes.
128 * @param index The index of the sample to retrieve.
129 * @return A Pair containing a unique pointer to an IData object that
130 * represents the sample, and the index of the sample in the dataset.
131 */
132 virtual Pair get(const std::size_t index) const = 0;
133
134 /* Saves a batch of samples to the database. The save method takes a span of
135 * Pairs as input, which allows the caller to save a batch of samples to the
136 * database. The save method iterates over the span of Pairs, and for each
137 * Pair, it retrieves the IData object and its index, and saves the image and
138 * label for the sample to the database. The save method returns true if all
139 * samples were saved successfully, and false otherwise. The save method is a
140 * pure virtual method, which means that it must be implemented by derived
141 * classes.
142 * @param data A span of Pairs, where each Pair contains a unique pointer to
143 * an IData object that represents a sample, and the index of the sample in
144 * the dataset.
145 * @return true if all samples were saved successfully, and false otherwise.
146 */
147 virtual bool save(const std::span<Pair> &data) = 0;
148
149 /* Set the detection confidence threshold applied to samples produced by
150 * get(). Concrete datasets stamp this onto the IData they create so the model
151 * filters detections at the caller-chosen threshold. Default 0.5. */
152 void set_confidence(float confidence) { confidence_ = confidence; }
153
154 /* Set the concept/class prompts applied to samples produced by get().
155 * Concrete datasets stamp these onto the IData they create (open-vocabulary
156 * models treat the prompt list as the class set). Empty leaves the IData's
157 * built-in default. */
158 void set_prompts(std::vector<std::string> prompts) {
159 prompts_ = std::move(prompts);
160 }
161
162 protected:
163 float confidence_ = 0.5f;
164 std::vector<std::string>
166
167 /* Retrieves the image data for a sample from the database. The getImage
168 * method takes an index as input, which is used to look up the corresponding
169 * sample ID in the ids_ vector. The getImage method then retrieves the image
170 * data for the sample from the database, and returns it as a cv::Mat object.
171 * The getImage method is a const method, which means that it does not modify
172 * the state of the IDataset object. The getImage method is used internally by
173 * the get method to retrieve the image data for a sample when constructing an
174 * IData object to represent the sample.
175 * @param index The index of the sample whose image data to retrieve.
176 * @return A cv::Mat object containing the image data for the sample.
177 */
178 cv::Mat image(const std::size_t index) const;
179
180 /* Saves the image data for a sample to the database. The saveImage method
181 * takes an index and a cv::Mat object as input, which represent the index of
182 * the sample and the image data to save, respectively. The saveImage method
183 * saves the image data for the sample to the database, and returns true if
184 * the image was saved successfully, and false otherwise. The saveImage method
185 * is used internally by the save method to save the image data for a sample
186 * when saving a batch of samples to the database.
187 * @param index The index of the sample whose image data to save.
188 * @param image A cv::Mat object containing the image data to save for the
189 * sample.
190 * @return true if the image was saved successfully, and false otherwise.
191 */
192 bool save_image(const std::size_t index, const cv::Mat &image);
193
194 /* Access to the underlying database for subclasses.
195 *
196 * Subclasses can use this to access database functionality beyond the
197 * basic getImage/saveImage interface if needed.
198 *
199 * @return Shared pointer to the ProjectDB instance
200 */
201 std::shared_ptr<ProjectDB> database() const;
202
203 private:
204 /* Shared pointer to the project database. Multiple IDataset instances can
205 * share the same database connection. The database connection is managed
206 * via RAII and will be closed when the last reference is destroyed.
207 */
208 std::shared_ptr<ProjectDB> db_;
209
210 /* Cached list of node IDs in the dataset. This is populated once during
211 * construction by querying the database. The IDs are used to map from
212 * dataset indices (0, 1, 2, ...) to RTABMap node IDs.
213 */
214 std::vector<int> ids_;
215};
216} // namespace reusex::vision
float confidence_
Detection confidence threshold for get().
Definition IDataset.hpp:163
void set_confidence(float confidence)
Definition IDataset.hpp:152
IDataset(std::filesystem::path dbPath)
virtual ~IDataset()=default
virtual Pair get(const std::size_t index) const =0
std::vector< std::string > prompts_
Concept prompts for get() (empty=default).
Definition IDataset.hpp:165
virtual bool save(const std::span< Pair > &data)=0
void set_prompts(std::vector< std::string > prompts)
Definition IDataset.hpp:158
IDataset(std::shared_ptr< ProjectDB > database)
size_t filter_annotated_prefix()
Remove only the leading contiguous run of already-annotated frames.
size_t filter_annotated()
Remove already-annotated frames from the dataset.
std::pair< std::unique_ptr< IData >, size_t > Pair
Definition IDataset.hpp:44
cv::Mat image(const std::size_t index) const
std::shared_ptr< ProjectDB > database() const
bool save_image(const std::size_t index, const cv::Mat &image)
int node_id(const std::size_t index) const