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