ReUseX  0.0.1
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
BackendFactory.hpp
Go to the documentation of this file.
1#pragma once
3#include <filesystem>
4#include <fmt/std.h>
5#include <spdlog/spdlog.h>
6
8
9namespace ReUseX::vision {
19
21 public:
22 /* Detects the appropriate backend based on the model path.
23 * @param model_path: The file or directory path of the model.
24 * @return The detected backend type. Currently hardcoded to TensorRT.
25 */
26 static Backend detect_backend(const std::filesystem::path &model_path) {
27 using namespace std::filesystem;
28
29 if (is_regular_file(model_path))
30 return detect_backend_from_file(model_path);
31
32 if (is_directory(model_path))
33 for (const auto &entry : directory_iterator(model_path))
34 if (entry.is_regular_file())
35 if (auto backend = detect_backend_from_file(entry.path());
36 backend != Backend::Unknown)
37 return backend;
38
39 return Backend::Unknown;
40 }
41
42 /* Creates an instance of the specified backend type.
43 * @param type: The backend type to create.
44 * @return A unique pointer to the created backend instance.
45 * @throws std::runtime_error if the backend type is not implemented or
46 * unsupported.
47 */
48 static std::unique_ptr<IMLBackend> create(Backend type) {
49 switch (type) {
50 case Backend::OpenCV:
51 spdlog::error("OpenCV backend is not implemented yet.");
52 throw std::runtime_error("OpenCV backend not implemented");
54 return std::make_unique<ReUseX::vision::tensor_rt::TensorRTBackend>();
56 spdlog::error("libTorch backend is not implemented yet.");
57 throw std::runtime_error("libTorch backend not implemented");
58 case Backend::DNN:
59 spdlog::error("DNN backend is not implemented yet.");
60 throw std::runtime_error("DNN backend not implemented");
62 spdlog::error("ONNXRuntime backend is not implemented yet.");
63 throw std::runtime_error("ONNXRuntime backend not implemented");
65 spdlog::error("OpenVINO backend is not implemented yet.");
66 throw std::runtime_error("OpenVINO backend not implemented");
67 default:
68 spdlog::error("Unsupported backend type: {}", static_cast<int>(type));
69 throw std::runtime_error("Unsupported backend");
70 }
71 }
72
73 private:
74 /* Helper function to detect backend type from a single file based on its
75 * extension.
76 * @param file_path: The path of the file to analyze.
77 * @return The detected backend type or Unknown if the extension is not
78 * recognized.
79 */
80 static Backend
81 detect_backend_from_file(const std::filesystem::path &file_path) {
82 auto ext = file_path.extension();
83
84 if (ext.empty()) {
85 spdlog::warn("File {} has no extension. Unable to detect backend.",
86 file_path);
87 return Backend::Unknown;
88 }
89
90 else if (ext == ".engine") {
91 spdlog::info("Detected TensorRT engine file: {}", file_path);
92 return Backend::TensorRT;
93 } else if (ext == ".pt" || ext == ".pth" || ext == ".torchscript") {
94 spdlog::info("Detected PyTorch model file: {}", file_path);
95 return Backend::libTorch;
96 } else if (ext == ".onnx") {
97 spdlog::info("Detected ONNX model file: {}", file_path);
99 } else if (ext == ".xml" || ext == ".bin") {
100 spdlog::info("Detected OpenVINO model files: {}", file_path);
101 return Backend::OpenVINO;
102 }
103
104 spdlog::warn("Unknown model file extension: {}. Defaulting to TensorRT.",
105 ext);
106 return Backend::Unknown;
107 }
108};
109} // namespace ReUseX::vision
static Backend detect_backend(const std::filesystem::path &model_path)
static std::unique_ptr< IMLBackend > create(Backend type)