ReUseX
0.0.5
3D Point Cloud Processing for Building Reuse
Toggle main menu visibility
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
16
namespace
reusex
{
17
class
ProjectDB
;
18
}
19
20
namespace
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. */
36
class
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
101
size_t
filter_annotated
();
102
118
size_t
filter_annotated_prefix
();
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
/* Returns the model's built-in default prompt strings (the class list the
163
* model uses when no caller prompts are set). Subclasses override this to
164
* expose their backend's default class list so callers can prepend it before
165
* appending glass prompts. Returns an empty vector by default. */
166
virtual
std::vector<std::string>
default_prompt_strings
()
const
{
return
{}; }
167
168
/* Set the 0-based class IDs (positions in the merged prompt list) that
169
* correspond to glass/mirror/transparent classes. When non-empty,
170
* concrete datasets that support glass filtering will build a CV_8U glass
171
* confidence map for each saved frame, zero those pixels in the label image,
172
* and persist the map so 'rux create clouds --glass-filter' can apply it. */
173
void
set_glass_class_ids
(std::vector<int> ids) {
174
glass_class_ids_
= std::move(ids);
175
}
176
177
protected
:
178
float
confidence_
= 0.5f;
179
std::vector<std::string>
180
prompts_
;
181
std::vector<int>
182
glass_class_ids_
;
183
184
/* Retrieves the image data for a sample from the database. The getImage
185
* method takes an index as input, which is used to look up the corresponding
186
* sample ID in the ids_ vector. The getImage method then retrieves the image
187
* data for the sample from the database, and returns it as a cv::Mat object.
188
* The getImage method is a const method, which means that it does not modify
189
* the state of the IDataset object. The getImage method is used internally by
190
* the get method to retrieve the image data for a sample when constructing an
191
* IData object to represent the sample.
192
* @param index The index of the sample whose image data to retrieve.
193
* @return A cv::Mat object containing the image data for the sample.
194
*/
195
cv::Mat
image
(
const
std::size_t index)
const
;
196
197
/* Saves the image data for a sample to the database. The saveImage method
198
* takes an index and a cv::Mat object as input, which represent the index of
199
* the sample and the image data to save, respectively. The saveImage method
200
* saves the image data for the sample to the database, and returns true if
201
* the image was saved successfully, and false otherwise. The saveImage method
202
* is used internally by the save method to save the image data for a sample
203
* when saving a batch of samples to the database.
204
* @param index The index of the sample whose image data to save.
205
* @param image A cv::Mat object containing the image data to save for the
206
* sample.
207
* @return true if the image was saved successfully, and false otherwise.
208
*/
209
bool
save_image
(
const
std::size_t index,
const
cv::Mat &
image
);
210
211
/* Access to the underlying database for subclasses.
212
*
213
* Subclasses can use this to access database functionality beyond the
214
* basic getImage/saveImage interface if needed.
215
*
216
* @return Shared pointer to the ProjectDB instance
217
*/
218
std::shared_ptr<ProjectDB>
database
()
const
;
219
220
private
:
221
/* Shared pointer to the project database. Multiple IDataset instances can
222
* share the same database connection. The database connection is managed
223
* via RAII and will be closed when the last reference is destroyed.
224
*/
225
std::shared_ptr<ProjectDB> db_;
226
227
/* Cached list of node IDs in the dataset. This is populated once during
228
* construction by querying the database. The IDs are used to map from
229
* dataset indices (0, 1, 2, ...) to RTABMap node IDs.
230
*/
231
std::vector<int> ids_;
232
};
233
}
// namespace reusex::vision
reusex::ProjectDB
Definition
ProjectDB.hpp:45
reusex::vision::IDataset::confidence_
float confidence_
Detection confidence threshold for get().
Definition
IDataset.hpp:178
reusex::vision::IDataset::set_confidence
void set_confidence(float confidence)
Definition
IDataset.hpp:152
reusex::vision::IDataset::IDataset
IDataset(std::filesystem::path dbPath)
reusex::vision::IDataset::~IDataset
virtual ~IDataset()=default
reusex::vision::IDataset::get
virtual Pair get(const std::size_t index) const =0
reusex::vision::IDataset::prompts_
std::vector< std::string > prompts_
Concept prompts for get() (empty=default).
Definition
IDataset.hpp:180
reusex::vision::IDataset::save
virtual bool save(const std::span< Pair > &data)=0
reusex::vision::IDataset::set_prompts
void set_prompts(std::vector< std::string > prompts)
Definition
IDataset.hpp:158
reusex::vision::IDataset::IDataset
IDataset(std::shared_ptr< ProjectDB > database)
reusex::vision::IDataset::set_glass_class_ids
void set_glass_class_ids(std::vector< int > ids)
Definition
IDataset.hpp:173
reusex::vision::IDataset::size
size_t size() const
reusex::vision::IDataset::filter_annotated_prefix
size_t filter_annotated_prefix()
Remove only the leading contiguous run of already-annotated frames.
reusex::vision::IDataset::filter_annotated
size_t filter_annotated()
Remove already-annotated frames from the dataset.
reusex::vision::IDataset::Pair
std::pair< std::unique_ptr< IData >, size_t > Pair
Definition
IDataset.hpp:44
reusex::vision::IDataset::default_prompt_strings
virtual std::vector< std::string > default_prompt_strings() const
Definition
IDataset.hpp:166
reusex::vision::IDataset::image
cv::Mat image(const std::size_t index) const
reusex::vision::IDataset::glass_class_ids_
std::vector< int > glass_class_ids_
Class IDs treated as glass (empty=off).
Definition
IDataset.hpp:182
reusex::vision::IDataset::database
std::shared_ptr< ProjectDB > database() const
reusex::vision::IDataset::save_image
bool save_image(const std::size_t index, const cv::Mat &image)
reusex::vision::IDataset::node_id
int node_id(const std::size_t index) const
reusex::vision
Definition
annotate.hpp:12
reusex
Definition
component_record.hpp:12
libs
reusex
include
vision
IDataset.hpp
Generated by
1.18.0