LightlyEdge C++ SDK
Loading...
Searching...
No Matches
lightly_edge_sdk.h
1#ifndef LIGHTLY_EDGE_SDK_H
2#define LIGHTLY_EDGE_SDK_H
3
4#include "cxx.h"
5#include "lightly_edge_cxx.rs.h"
6#include "lightly_edge_error.h"
7#include "lightly_edge_rs_bindings.h"
8#include "lightly_edge_rs_bindings_ext.h"
9#include "tl/expected.hpp"
10#include <cassert>
11#include <cstdint>
12#include <cstdio>
13#include <string>
14#include <vector>
15
16namespace lightly_edge_sdk {
17
18const size_t BUFFER_MAX_LENGTH = 3000;
19const uint32_t MAX_CLASSIFICATIONS_DEFAULT = 5;
20const int CPP_SDK_ERROR_CODE = 30;
21
25struct Frame {
29 size_t width_;
30
34 size_t height_;
35
43
51 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
52 Frame(size_t width, size_t height, void *rgbImageData)
53 : width_(width), height_(height), rgbImageData_(rgbImageData) {}
54};
55
57using lightly_edge_rs::CStatus;
60using lightly_edge_rs::InferenceDeviceType;
64
65inline auto default_config() -> LightlyEdgeConfig {
66 LightlyEdgeConfig config{};
67 config.detector_config.object_detector_enable = false;
68 config.detector_config.classifiers_enable = false;
69 config.detector_config.max_classifications = MAX_CLASSIFICATIONS_DEFAULT;
70 config.inference_device_type = lightly_edge_rs::InferenceDeviceType::Auto;
71 return config;
72}
73
74// Converts CxxStatus to tl::unexpected.
75inline auto cxx_status_to_unexpected(lightly_edge_cxx::CxxStatus &status)
76 -> tl::unexpected<Error> {
77 return tl::make_unexpected(
78 lightly_edge_sdk::Error(status.code, std::string(status.message)));
79}
80
84struct SelectInfo {
89 std::vector<DiversitySelectInfo> diversity;
90
95 std::vector<SimilaritySelectInfo> similarity;
96
101 std::vector<AdaptiveDiversitySelectInfo> adaptive_diversity;
102
107 std::vector<DetectionSelectInfo> detection;
108};
109
111
120public:
125 if (this->lightly_edge != nullptr) {
126 lightly_edge_rs::destroy(this->lightly_edge);
127 }
128 }
129
133 LightlyEdge(LightlyEdge &&other) noexcept
134 : lightly_edge(other.lightly_edge), lightly_edge_cxx(other.lightly_edge_cxx) {
135 other.lightly_edge = nullptr;
136 other.lightly_edge_cxx = nullptr;
137 }
138
142 auto operator=(LightlyEdge &&other) noexcept -> LightlyEdge & {
143 if (this != &other) {
144 if (this->lightly_edge != nullptr) {
145 lightly_edge_rs::destroy(this->lightly_edge);
146 }
147 this->lightly_edge = other.lightly_edge;
148 this->lightly_edge_cxx = other.lightly_edge_cxx;
149 other.lightly_edge = nullptr;
150 other.lightly_edge_cxx = nullptr;
151 }
152 return *this;
153 }
154
158 LightlyEdge(const LightlyEdge &other) = delete;
159
163 auto operator=(const LightlyEdge &other) -> LightlyEdge & = delete;
164
171 [[nodiscard]] static auto new_from_tar(const char *archive_path,
172 LightlyEdgeConfig config) -> LightlyEdge {
173 return value_or_throw(LightlyEdge::new_from_tar_noexcept(archive_path, config));
174 }
175
182 [[nodiscard]] static auto new_from_tar_noexcept(const char *archive_path,
183 LightlyEdgeConfig config) noexcept
184 -> tl::expected<LightlyEdge, Error> {
185 lightly_edge_rs::LightlyEdge *lightly_edge = nullptr;
186 CStatus status =
187 lightly_edge_rs::lightly_edge_new_from_tar(archive_path, config, &lightly_edge);
188 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
189 return status_to_unexpected(&status);
190 }
191 return LightlyEdge(lightly_edge);
192 }
193
200 [[nodiscard]] static auto new_from_directory(const char *model_directory,
201 LightlyEdgeConfig config) {
202 return value_or_throw(
203 LightlyEdge::new_from_directory_noexcept(model_directory, config));
204 }
205
212 [[nodiscard]] static auto
213 new_from_directory_noexcept(const char *model_directory,
214 LightlyEdgeConfig config) noexcept
215 -> tl::expected<LightlyEdge, Error> {
216 lightly_edge_rs::LightlyEdge *lightly_edge = nullptr;
217 CStatus status = lightly_edge_rs::lightly_edge_new_from_directory(
218 model_directory, config, &lightly_edge);
219 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
220 return status_to_unexpected(&status);
221 }
222 return LightlyEdge(lightly_edge);
223 }
224
231 [[nodiscard]] auto embed_frame(const Frame &frame) const -> std::vector<float> {
233 }
234
241 [[nodiscard]] auto embed_frame_noexcept(const Frame &frame) const noexcept
242 -> tl::expected<std::vector<float>, Error> {
243 size_t embedding_dimension =
244 lightly_edge_rs::embedding_dimension(this->lightly_edge);
245
246 std::vector<float> embedding_buffer(embedding_dimension);
247 auto status = lightly_edge_rs::embed_frame(
248 this->lightly_edge, static_cast<uint8_t *>(frame.rgbImageData_),
249 frame.width_ * frame.height_ * 3, frame.width_, frame.height_,
250 embedding_buffer.data());
251 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
252 return status_to_unexpected(&status);
253 }
254 // Moving the vector is more efficient than copying it.
255 return std::move(embedding_buffer);
256 }
257
263 [[nodiscard]] auto embedding_dimension() const noexcept -> size_t {
264 return lightly_edge_rs::embedding_dimension(this->lightly_edge);
265 }
266
278 [[nodiscard]] auto embed_texts(const std::vector<std::string> &texts) const
279 -> std::vector<std::vector<float>> {
281 }
282
294 [[nodiscard]] auto
295 embed_texts_noexcept(const std::vector<std::string> &texts) const noexcept
296 -> tl::expected<std::vector<std::vector<float>>, Error> {
297
298 // Handle the case with no input texts.
299 if (texts.empty()) {
300 return {};
301 }
302
303 // Convert std::string texts to C-style strings.
304 std::vector<const char *> c_texts;
305 c_texts.reserve(texts.size());
306 for (const auto &text : texts) {
307 c_texts.push_back(text.c_str()); // Get C-style string pointer.
308 }
309
310 size_t embedding_dimension = lightly_edge_rs::embedding_dimension(lightly_edge);
311
312 // Prepare the embeddings vector.
313 std::vector<std::vector<float>> embeddings(texts.size(),
314 std::vector<float>(embedding_dimension));
315
316 // Prepare a vector of pointers to the data of each embedding vector.
317 std::vector<float *> embeddings_ptrs;
318 embeddings_ptrs.reserve(texts.size());
319 for (auto &embedding : embeddings) {
320 embeddings_ptrs.push_back(embedding.data());
321 }
322
323 // Call the Rust function to fill in the embeddings.
324 auto status = lightly_edge_rs::embed_texts(lightly_edge, c_texts.data(),
325 c_texts.size(), embeddings_ptrs.data());
326
327 // Check if the embedding was successful.
328 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
329 return status_to_unexpected(&status);
330 }
331
332 // Return the populated embeddings vector.
333 return embeddings;
334 }
335
356 void insert_into_embedding_database(const std::vector<float> &embedding) const {
358 }
359
381 const std::vector<float> &embedding) const noexcept -> tl::expected<void, Error> {
382 auto status = lightly_edge_rs::insert_into_embedding_database(
383 this->lightly_edge, embedding.data(), embedding.size());
384 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
385 return status_to_unexpected(&status);
386 }
387 return {};
388 }
389
400
406 [[nodiscard]] auto clear_embedding_database_noexcept() const noexcept
407 -> tl::expected<void, Error> {
408 auto status = lightly_edge_cxx::clear_embedding_database(*this->lightly_edge_cxx);
409 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
410 return cxx_status_to_unexpected(status);
411 }
412 return {};
413 }
414
420 [[nodiscard]] auto num_diversity_strategies() const noexcept -> size_t {
421 return lightly_edge_rs::num_diversity_strategies(this->lightly_edge);
422 }
423
429 [[nodiscard]] auto num_similarity_strategies() const noexcept -> size_t {
430 return lightly_edge_rs::num_similarity_strategies(this->lightly_edge);
431 }
432
438 [[nodiscard]] auto num_adaptive_diversity_strategies() const noexcept -> size_t {
439 return lightly_edge_rs::num_adaptive_diversity_strategies(this->lightly_edge);
440 }
441
447 [[nodiscard]] auto num_detection_strategies() const noexcept -> size_t {
448 return lightly_edge_rs::num_detection_strategies(this->lightly_edge);
449 }
487 auto register_diversity_strategy(float min_distance) const -> void {
489 }
490
533 [[nodiscard]] auto
534 register_diversity_strategy_noexcept(float min_distance) const noexcept
535 -> tl::expected<void, Error> {
536 auto status =
537 lightly_edge_rs::register_diversity_strategy(this->lightly_edge, min_distance);
538 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
539 return status_to_unexpected(&status);
540 }
541 return {};
542 }
543
583 auto register_similarity_strategy(const std::vector<float> &query_embedding,
584 float max_distance) const -> void {
586 register_similarity_strategy_noexcept(query_embedding, max_distance));
587 }
588
637 [[nodiscard]] auto
638 register_similarity_strategy_noexcept(const std::vector<float> &query_embedding,
639 float max_distance) const noexcept
640 -> tl::expected<void, Error> {
641 auto status = lightly_edge_rs::register_similarity_strategy(
642 this->lightly_edge, query_embedding.data(), query_embedding.size(),
643 max_distance);
644 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
645 return status_to_unexpected(&status);
646 }
647 return {};
648 }
649
682 float target_ratio, size_t buffer_max_length = BUFFER_MAX_LENGTH) const -> void {
684 register_adaptive_diversity_strategy_noexcept(target_ratio, buffer_max_length));
685 }
686
722 float target_ratio, size_t buffer_max_length = BUFFER_MAX_LENGTH) const noexcept
723 -> tl::expected<void, Error> {
724 auto status = lightly_edge_rs::register_adaptive_diversity_strategy(
725 this->lightly_edge, target_ratio, buffer_max_length);
726 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
727 return status_to_unexpected(&status);
728 }
729 return {};
730 }
731
765 auto register_detection_strategy(uint32_t class_id, int32_t subclass_id,
766 float threshold) const -> void {
768 register_detection_strategy_noexcept(class_id, subclass_id, threshold));
769 }
770
805 [[nodiscard]] auto
806 register_detection_strategy_noexcept(uint32_t class_id, int32_t subclass_id,
807 float threshold) const noexcept
808 -> tl::expected<void, Error> {
809 auto status = lightly_edge_rs::register_detection_strategy(
810 this->lightly_edge, class_id, subclass_id, threshold);
811 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
812 return status_to_unexpected(&status);
813 }
814 return {};
815 }
816
820 void clear_strategies() { lightly_edge_rs::clear_strategies(this->lightly_edge); }
821
848 [[nodiscard]] auto embed_patches(const Frame &frame) const
849 -> std::vector<std::vector<float>> {
851 }
852
878 [[nodiscard]] auto embed_patches_noexcept(const Frame &frame) const noexcept
879 -> tl::expected<std::vector<std::vector<float>>, Error> {
880 size_t embedding_dimension =
881 lightly_edge_rs::embedding_dimension(this->lightly_edge);
882 size_t num_patches = lightly_edge_rs::num_patches(this->lightly_edge);
883
884 // Prepare a vector of pointers to the data of each embedding vector.
885 std::vector<std::vector<float>> embeddings(num_patches,
886 std::vector<float>(embedding_dimension));
887 std::vector<float *> embeddings_ptrs;
888 embeddings_ptrs.reserve(num_patches);
889 for (auto &embedding : embeddings) {
890 embeddings_ptrs.push_back(embedding.data());
891 }
892
893 // Call the Rust function to fill in the embeddings.
894 auto status = lightly_edge_rs::embed_patches(
895 this->lightly_edge, static_cast<uint8_t *>(frame.rgbImageData_),
896 frame.width_ * frame.height_ * 3, frame.width_, frame.height_,
897 embeddings_ptrs.data());
898
899 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
900 return status_to_unexpected(&status);
901 }
902
903 // Return the populated embeddings vector.
904 return embeddings;
905 }
906
933 auto set_embedding_patches(const std::vector<PatchCoordsRel> &patches) const -> void {
935 }
936
959 const std::vector<PatchCoordsRel> &patches) const noexcept
960 -> tl::expected<void, Error> {
961 auto status = lightly_edge_rs::set_embedding_patches(
962 this->lightly_edge, patches.data(), patches.size());
963 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
964 return status_to_unexpected(&status);
965 }
966 return {};
967 }
968
973 lightly_edge_cxx::reset_embedding_patches(*this->lightly_edge_cxx);
974 }
975
1017 [[nodiscard]] auto should_select(const std::vector<float> &embedding,
1018 const std::vector<ObjectDetection> &detections) const
1019 -> SelectInfo {
1020 return value_or_throw(should_select_noexcept(embedding, detections));
1021 }
1022
1066 [[nodiscard]] auto
1067 should_select_noexcept(const std::vector<float> &embedding,
1068 const std::vector<ObjectDetection> &detections) const noexcept
1069 -> tl::expected<SelectInfo, Error> {
1070 // Initialize the output buffers.
1071 CStatus status = {};
1072 auto select_info_buffers = SelectInfoBuffers(this->lightly_edge);
1073 auto out_info = select_info_buffers.to_c_select_info();
1074
1075 status = lightly_edge_rs::should_select(this->lightly_edge, embedding.data(),
1076 embedding.size(), detections.data(),
1077 detections.size(), &out_info);
1078 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
1079 return status_to_unexpected(&status);
1080 }
1081
1082 // Convert to the output C++ struct.
1083 auto select_info = select_info_buffers.to_select_info();
1084 return select_info;
1085 }
1086
1131 [[nodiscard]] auto
1132 should_select_with_patches(const std::vector<std::vector<float>> &embeddings,
1133 const std::vector<ObjectDetection> &detections) const
1134 -> SelectInfo {
1135 return value_or_throw(should_select_with_patches_noexcept(embeddings, detections));
1136 }
1137
1187 const std::vector<std::vector<float>> &embeddings,
1188 const std::vector<ObjectDetection> &detections) const noexcept
1189 -> tl::expected<SelectInfo, Error> {
1190 // Initialize the output buffers.
1191 CStatus status = {};
1192 auto select_info_buffers = SelectInfoBuffers(this->lightly_edge);
1193 auto out_info = select_info_buffers.to_c_select_info();
1194
1195 // Get the embedding dimension. If there are no embeddings, the value does not
1196 // matter.
1197 // TODO(Michal, 12/2024): IMPORTANT! Calling this function with no embeddings will
1198 // fail in Rust. Decide how to handle this.
1199 size_t embedding_dim = embeddings.empty() ? 0 : embeddings[0].size();
1200
1201 // Validate all embeddings have the same dimension.
1202 for (const auto &embedding : embeddings) {
1203 if (embedding.size() != embedding_dim) {
1204 return tl::make_unexpected(lightly_edge_sdk::Error(
1205 CPP_SDK_ERROR_CODE, "All patch embeddings must have the same dimension"));
1206 }
1207 }
1208
1209 // Convert to a vector of pointers to the data of each embedding vector.
1210 std::vector<const float *> embeddings_ptrs;
1211 embeddings_ptrs.reserve(embeddings.size());
1212 for (const auto &embedding : embeddings) {
1213 embeddings_ptrs.push_back(embedding.data());
1214 }
1215
1216 status = lightly_edge_rs::should_select_with_patches(
1217 this->lightly_edge, embeddings_ptrs.data(), embeddings_ptrs.size(),
1218 embedding_dim, detections.data(), detections.size(), &out_info);
1219 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
1220 return status_to_unexpected(&status);
1221 }
1222
1223 // Convert to the output C++ struct.
1224 auto select_info = select_info_buffers.to_select_info();
1225 return select_info;
1226 }
1227
1228 // TODO(lukas 8/24): Add docstrings
1234 [[nodiscard]] auto detect_noexcept(const Frame &frame) const noexcept
1235 -> tl::expected<std::vector<ObjectDetection>, Error> {
1236 std::vector<ObjectDetection> detections;
1237 // TODO(lukas 7/25): 1000 is a constant for the current model, we should read this
1238 // from JSON
1239 const auto capacity = 1000;
1240 detections.resize(capacity);
1241 size_t detections_count = detections.size();
1242 auto status = lightly_edge_rs::detect(
1243 this->lightly_edge, static_cast<uint8_t *>(frame.rgbImageData_),
1244 frame.width_ * frame.height_ * 3, frame.width_, frame.height_,
1245 detections.data(), &detections_count);
1246 detections.resize(detections_count);
1247 if (status.code != lightly_edge_rs::SUCCESS_CODE) {
1248 return status_to_unexpected(&status);
1249 }
1250 return detections;
1251 }
1252
1258 [[nodiscard]] auto detect(const Frame &frame) const -> std::vector<ObjectDetection> {
1259 return value_or_throw(detect_noexcept(frame));
1260 }
1261
1274 [[nodiscard]] auto detection_class_labels() const noexcept
1275 -> std::vector<std::string> {
1276 // TODO(lukas 8/24): consider exposing the number of label classes from Rust (or
1277 // switch to cxx).
1278 const auto capacity = 1000;
1279 size_t classes_count = capacity;
1280 std::vector<const char *> cstr_class_labels(classes_count);
1281 auto status = lightly_edge_rs::detection_class_labels(
1282 this->lightly_edge, cstr_class_labels.data(), &classes_count);
1283 // class_labels() failing is our fault. We either provided an invalid pointer or the
1284 // array is too short. This is a bug, so we assert here.
1285 // Note: assert only runs when NDEBUG is undefined.
1286 assert(status.code == lightly_edge_rs::SUCCESS_CODE); // NOLINT
1287
1288 // Convert C-strings to std::string's.
1289 std::vector<std::string> class_labels(classes_count);
1290 for (size_t i = 0; i < classes_count; ++i) {
1291 class_labels[i] = cstr_class_labels[i];
1292 }
1293 return class_labels;
1294 }
1295
1312 [[nodiscard]] auto detection_subclass_labels(uint32_t class_id) const noexcept
1313 -> std::vector<std::string> {
1314 // TODO(lukas 8/24): consider exposing the number of label classes from Rust (or
1315 // switch to cxx).
1316 const auto capacity = 1000;
1317 size_t subclasses_count = capacity;
1318 std::vector<const char *> cstr_subclass_labels(subclasses_count);
1319 auto status = lightly_edge_rs::detection_subclass_labels(
1320 this->lightly_edge, class_id, cstr_subclass_labels.data(), &subclasses_count);
1321 // detection_subclass_labels() failing is our fault. We either provided an invalid
1322 // pointer or the array is too short. This is a bug, so we assert here. Note: assert
1323 // only runs when NDEBUG is undefined.
1324 assert(status.code == lightly_edge_rs::SUCCESS_CODE); // NOLINT
1325
1326 // Convert C-strings to std::string's.
1327 std::vector<std::string> subclass_labels(subclasses_count);
1328 for (size_t i = 0; i < subclasses_count; ++i) {
1329 subclass_labels[i] = cstr_subclass_labels[i];
1330 }
1331 return subclass_labels;
1332 }
1333
1334private:
1335 // Private constructor. Use `new_from_tar` to create a new instance.
1336 explicit LightlyEdge(lightly_edge_rs::LightlyEdge *lightly_edge_)
1337 : lightly_edge(lightly_edge_),
1338 lightly_edge_cxx(
1339 reinterpret_cast<lightly_edge_cxx::LightlyEdge *>(lightly_edge_)) {}
1340 lightly_edge_cxx::LightlyEdge *lightly_edge_cxx{};
1341 lightly_edge_rs::LightlyEdge *lightly_edge{};
1342
1346 class SelectInfoBuffers {
1347 public:
1348 explicit SelectInfoBuffers(lightly_edge_rs::LightlyEdge *lightly_edge)
1349 : out_diversity_size(lightly_edge_rs::num_diversity_strategies(lightly_edge)),
1350 out_similarity_size(lightly_edge_rs::num_similarity_strategies(lightly_edge)),
1351 out_adaptive_diversity_size(
1352 lightly_edge_rs::num_adaptive_diversity_strategies(lightly_edge)),
1353 out_detection_selection_size(
1354 lightly_edge_rs::num_detection_strategies(lightly_edge)) {
1355
1356 resize_buffers_to_match_sizes();
1357 }
1358
1363 [[nodiscard]] auto to_c_select_info()
1364 -> lightly_edge_rs::SelectionStrategiesSelectInfo {
1365 lightly_edge_rs::SelectionStrategiesSelectInfo out_info{};
1366 out_info.out_diversity_buffer = diversity.data();
1367 out_info.diversity_buffer_size = diversity.size();
1368 out_info.out_diversity_size = &out_diversity_size;
1369
1370 out_info.out_similarity_buffer = similarity.data();
1371 out_info.similarity_buffer_size = similarity.size();
1372 out_info.out_similarity_size = &out_similarity_size;
1373
1374 out_info.out_adaptive_diversity_buffer = adaptive_diversity.data();
1375 out_info.adaptive_diversity_buffer_size = adaptive_diversity.size();
1376 out_info.out_adaptive_diversity_size = &out_adaptive_diversity_size;
1377
1378 out_info.out_detection_buffer = detection.data();
1379 out_info.detection_buffer_size = detection.size();
1380 out_info.out_detection_size = &out_detection_selection_size;
1381
1382 return out_info;
1383 }
1384
1388 [[nodiscard]] auto to_select_info() -> lightly_edge_sdk::SelectInfo {
1389 // Important: should_select* methods modify the out_*_size variables. We need to
1390 // resize the vectors to the correct size before moving the data to the SelectInfo
1391 // struct.
1392 resize_buffers_to_match_sizes();
1393
1394 lightly_edge_sdk::SelectInfo select_info;
1395 select_info.diversity = std::move(diversity);
1396 select_info.similarity = std::move(similarity);
1397 select_info.adaptive_diversity = std::move(adaptive_diversity);
1398 select_info.detection = std::move(detection);
1399 return select_info;
1400 }
1401
1402 private:
1403 size_t out_diversity_size;
1404 size_t out_similarity_size;
1405 size_t out_adaptive_diversity_size;
1406 size_t out_detection_selection_size;
1407 std::vector<lightly_edge_rs::DiversitySelectInfo> diversity;
1408 std::vector<lightly_edge_rs::SimilaritySelectInfo> similarity;
1409 std::vector<lightly_edge_rs::AdaptiveDiversitySelectInfo> adaptive_diversity;
1410 std::vector<lightly_edge_rs::DetectionSelectInfo> detection;
1411
1412 void resize_buffers_to_match_sizes() {
1413 diversity.resize(out_diversity_size);
1414 similarity.resize(out_similarity_size);
1415 adaptive_diversity.resize(out_adaptive_diversity_size);
1416 detection.resize(out_detection_selection_size);
1417 }
1418 };
1419};
1420
1421} // namespace lightly_edge_sdk
1422
1423#endif // LIGHTLY_EDGE_SDK_H
LightlyEdge.
Definition lightly_edge_sdk.h:119
static auto new_from_directory(const char *model_directory, LightlyEdgeConfig config)
Initialize LightlyEdge with an ort inference backend.
Definition lightly_edge_sdk.h:200
auto embed_texts(const std::vector< std::string > &texts) const -> std::vector< std::vector< float > >
Embed a list of text strings.
Definition lightly_edge_sdk.h:278
auto register_similarity_strategy_noexcept(const std::vector< float > &query_embedding, float max_distance) const noexcept -> tl::expected< void, Error >
Register a similarity strategy, noexcept version.
Definition lightly_edge_sdk.h:638
auto embed_frame_noexcept(const Frame &frame) const noexcept -> tl::expected< std::vector< float >, Error >
Embed an RGB image.
Definition lightly_edge_sdk.h:241
auto num_diversity_strategies() const noexcept -> size_t
Get the number of registered diversity strategies.
Definition lightly_edge_sdk.h:420
LightlyEdge(LightlyEdge &&other) noexcept
Move constructor.
Definition lightly_edge_sdk.h:133
auto clear_embedding_database() const -> void
Clear the embedding database.
Definition lightly_edge_sdk.h:397
auto register_diversity_strategy(float min_distance) const -> void
Register a diversity strategy.
Definition lightly_edge_sdk.h:487
LightlyEdge(const LightlyEdge &other)=delete
Copy constructor (disallowed).
auto register_adaptive_diversity_strategy_noexcept(float target_ratio, size_t buffer_max_length=BUFFER_MAX_LENGTH) const noexcept -> tl::expected< void, Error >
Register an adaptive diversity strategy, no except version.
Definition lightly_edge_sdk.h:721
void reset_embedding_patches()
Reset the embedding patches to full image patch.
Definition lightly_edge_sdk.h:972
auto num_similarity_strategies() const noexcept -> size_t
Get the number of registered similarity strategies.
Definition lightly_edge_sdk.h:429
auto should_select_with_patches(const std::vector< std::vector< float > > &embeddings, const std::vector< ObjectDetection > &detections) const -> SelectInfo
Check if a frame should be selected taking patches into account.
Definition lightly_edge_sdk.h:1132
auto set_embedding_patches(const std::vector< PatchCoordsRel > &patches) const -> void
Set the embedding patches.
Definition lightly_edge_sdk.h:933
auto register_detection_strategy_noexcept(uint32_t class_id, int32_t subclass_id, float threshold) const noexcept -> tl::expected< void, Error >
Register a detection strategy, noexcept version.
Definition lightly_edge_sdk.h:806
auto register_detection_strategy(uint32_t class_id, int32_t subclass_id, float threshold) const -> void
Register a detection strategy.
Definition lightly_edge_sdk.h:765
auto detect(const Frame &frame) const -> std::vector< ObjectDetection >
Run object detection on a frame.
Definition lightly_edge_sdk.h:1258
auto register_similarity_strategy(const std::vector< float > &query_embedding, float max_distance) const -> void
Register a similarity strategy.
Definition lightly_edge_sdk.h:583
auto insert_into_embedding_database_noexcept(const std::vector< float > &embedding) const noexcept -> tl::expected< void, Error >
Insert an embedding into the database.
Definition lightly_edge_sdk.h:380
auto register_diversity_strategy_noexcept(float min_distance) const noexcept -> tl::expected< void, Error >
Register a diversity strategy, noexcept version.
Definition lightly_edge_sdk.h:534
auto operator=(const LightlyEdge &other) -> LightlyEdge &=delete
Copy assignment operator (disallowed).
static auto new_from_tar_noexcept(const char *archive_path, LightlyEdgeConfig config) noexcept -> tl::expected< LightlyEdge, Error >
Initialize LightlyEdge with an ort inference backend.
Definition lightly_edge_sdk.h:182
void insert_into_embedding_database(const std::vector< float > &embedding) const
Insert an embedding into the database.
Definition lightly_edge_sdk.h:356
~LightlyEdge()
Deallocate the memory associated with a LightlyEdge.
Definition lightly_edge_sdk.h:124
auto detect_noexcept(const Frame &frame) const noexcept -> tl::expected< std::vector< ObjectDetection >, Error >
Run object detection on a frame.
Definition lightly_edge_sdk.h:1234
auto embed_texts_noexcept(const std::vector< std::string > &texts) const noexcept -> tl::expected< std::vector< std::vector< float > >, Error >
Embed a list of text strings.
Definition lightly_edge_sdk.h:295
auto should_select(const std::vector< float > &embedding, const std::vector< ObjectDetection > &detections) const -> SelectInfo
Check if a frame should be selected.
Definition lightly_edge_sdk.h:1017
auto detection_class_labels() const noexcept -> std::vector< std::string >
Get labels of object detection classes.
Definition lightly_edge_sdk.h:1274
auto clear_embedding_database_noexcept() const noexcept -> tl::expected< void, Error >
Clear the embedding database.
Definition lightly_edge_sdk.h:406
auto should_select_with_patches_noexcept(const std::vector< std::vector< float > > &embeddings, const std::vector< ObjectDetection > &detections) const noexcept -> tl::expected< SelectInfo, Error >
Check if a frame should be selected taking patches into account.
Definition lightly_edge_sdk.h:1186
void clear_strategies()
Clear all registered selection strategies.
Definition lightly_edge_sdk.h:820
auto detection_subclass_labels(uint32_t class_id) const noexcept -> std::vector< std::string >
Get the subclass labels for a specified detection class.
Definition lightly_edge_sdk.h:1312
auto embed_patches_noexcept(const Frame &frame) const noexcept -> tl::expected< std::vector< std::vector< float > >, Error >
Embed patches of an image, noexcept version.
Definition lightly_edge_sdk.h:878
auto operator=(LightlyEdge &&other) noexcept -> LightlyEdge &
Move assignment operator.
Definition lightly_edge_sdk.h:142
auto num_adaptive_diversity_strategies() const noexcept -> size_t
Get the number of registered adaptive diversity strategies.
Definition lightly_edge_sdk.h:438
auto embed_patches(const Frame &frame) const -> std::vector< std::vector< float > >
Embed patches of an image.
Definition lightly_edge_sdk.h:848
auto num_detection_strategies() const noexcept -> size_t
Get the number of registered detection strategies.
Definition lightly_edge_sdk.h:447
auto register_adaptive_diversity_strategy(float target_ratio, size_t buffer_max_length=BUFFER_MAX_LENGTH) const -> void
Register an adaptive diversity strategy.
Definition lightly_edge_sdk.h:681
auto set_embedding_patches_noexcept(const std::vector< PatchCoordsRel > &patches) const noexcept -> tl::expected< void, Error >
Set the embedding patches, noexcept version.
Definition lightly_edge_sdk.h:958
static auto new_from_tar(const char *archive_path, LightlyEdgeConfig config) -> LightlyEdge
Initialize LightlyEdge with an ort embedding inference backend.
Definition lightly_edge_sdk.h:171
auto should_select_noexcept(const std::vector< float > &embedding, const std::vector< ObjectDetection > &detections) const noexcept -> tl::expected< SelectInfo, Error >
Check if a frame should be selected.
Definition lightly_edge_sdk.h:1067
static auto new_from_directory_noexcept(const char *model_directory, LightlyEdgeConfig config) noexcept -> tl::expected< LightlyEdge, Error >
Initialize LightlyEdge with an ort inference backend.
Definition lightly_edge_sdk.h:213
auto embed_frame(const Frame &frame) const -> std::vector< float >
Embed an RGB image.
Definition lightly_edge_sdk.h:231
auto embedding_dimension() const noexcept -> size_t
Get the embedding dimension.
Definition lightly_edge_sdk.h:263
Namespace with core LightlyEdge SDK functionality.
Definition lightly_edge_error.h:15
auto value_or_throw(tl::expected< T, lightly_edge_sdk::Error > &&result) -> T
Convenience function to unwrap a result of lightly_edge_sdk::LightlyEdge functions.
Definition lightly_edge_error.h:79
Holds information whether a frame should be selected or not.
Definition lightly_edge_rs_bindings.h:72
Definition lightly_edge_rs_bindings.h:77
Holds information whether a frame should be selected or not.
Definition lightly_edge_rs_bindings.h:60
Definition lightly_edge_rs_bindings.h:44
Definition lightly_edge_rs_bindings.h:49
A patch specified by relative coordinates.
Definition lightly_edge_rs_bindings.h:100
Holds information whether a frame should be selected or not.
Definition lightly_edge_rs_bindings.h:66
Error struct to hold error code and message.
Definition lightly_edge_error.h:20
Frame data for LightlyEdge.
Definition lightly_edge_sdk.h:25
size_t width_
Width of the image.
Definition lightly_edge_sdk.h:29
void * rgbImageData_
Pointer to the RGB image data.
Definition lightly_edge_sdk.h:42
size_t height_
Height of the image.
Definition lightly_edge_sdk.h:34
Frame(size_t width, size_t height, void *rgbImageData)
Construct a new Frame object.
Definition lightly_edge_sdk.h:52
Selection information about a processed frame.
Definition lightly_edge_sdk.h:84
std::vector< DiversitySelectInfo > diversity
The diversity selection info for each diversity strategy in the order of registration.
Definition lightly_edge_sdk.h:89
std::vector< SimilaritySelectInfo > similarity
The similarity selection info for each similarity strategy in the order of registration.
Definition lightly_edge_sdk.h:95
std::vector< AdaptiveDiversitySelectInfo > adaptive_diversity
The adaptive diversity selection info for each adaptive diversity strategy in the order of registrati...
Definition lightly_edge_sdk.h:101
std::vector< DetectionSelectInfo > detection
The detection selection info for each detection strategy in the order of registration.
Definition lightly_edge_sdk.h:107