diff --git a/modules/ANSODEngine/ANSModelSource.h b/modules/ANSODEngine/ANSModelSource.h new file mode 100644 index 0000000..c5ff230 --- /dev/null +++ b/modules/ANSODEngine/ANSModelSource.h @@ -0,0 +1,76 @@ +#ifndef ANSMODELSOURCE_H +#define ANSMODELSOURCE_H +#pragma once +// ANSModelSource — where a model's bytes come from: a file, or memory. +// +// Every engine here built its session from a file path, which is why loading +// an encrypted model package means inflating it to a directory first: the +// unprotected model lands on disk, is optimised, and is zipped back into a +// cache. Handing the runtime a buffer instead removes both the round trip and +// the plaintext on disk, and it is the one change the new .ansmodel package +// format cannot work without. +// +// A model in memory still needs two things a path gave for free. `name` is +// what logs call it, since there is no file name to fall back on. And +// ONNX Runtime copies what it needs out of the buffer while the session is +// being built, so the caller may wipe its plaintext the moment MakeSession +// returns — which is exactly what a package reader should do. + +#include + +#include + +namespace ANSCENTER { + + struct ModelSource { + std::string path; // file on disk; empty for a model already in memory + std::string name; // what to call it in logs + const void* data = nullptr; + size_t size = 0; + + bool inMemory() const { return data != nullptr && size > 0; } + + // Named factories, never implicit conversions: an engine that offers + // both Init(std::string) and Init(ModelSource) must not become + // ambiguous for a caller passing a plain string literal. + static ModelSource File(std::string p) { + ModelSource s; + s.name = p; + s.path = std::move(p); + return s; + } + static ModelSource Memory(const void* d, size_t n, std::string name) { + ModelSource s; + s.data = d; + s.size = n; + s.name = std::move(name); + return s; + } + }; + + // What to call this model in a log line. + inline std::string ModelSourceName(const ModelSource& src) { + if (!src.name.empty()) return src.name; + if (!src.path.empty()) return src.path; + return ""; + } + + // Build a session from wherever the bytes are. One place, so the Windows + // wide-path conversion and the from-memory call are not repeated in every + // engine — they were already duplicated eleven times. + inline Ort::Session MakeSession(Ort::Env& env, const ModelSource& src, + const Ort::SessionOptions& options) { + if (src.inMemory()) + return Ort::Session(env, src.data, src.size, options); +#ifdef _WIN32 + // ORT's Windows API takes a wide (ORTCHAR_T = wchar_t) path. + const std::wstring wide(src.path.begin(), src.path.end()); + return Ort::Session(env, wide.c_str(), options); +#else + return Ort::Session(env, src.path.c_str(), options); +#endif + } + +} // namespace ANSCENTER + +#endif // ANSMODELSOURCE_H diff --git a/modules/ANSODEngine/ANSONNXCL.cpp b/modules/ANSODEngine/ANSONNXCL.cpp index 450e9bc..8d07cf1 100644 --- a/modules/ANSODEngine/ANSONNXCL.cpp +++ b/modules/ANSODEngine/ANSONNXCL.cpp @@ -55,6 +55,11 @@ namespace ANSCENTER return std::accumulate(vector.begin(), vector.end(), 1LL, std::multiplies()); } bool ANSONNXCL::Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU) + { + return Init(ModelSource::File(modelPath), targetInputShape, useGPU); + } + + bool ANSONNXCL::Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU) { std::lock_guard lock(_mutex); try { @@ -198,12 +203,7 @@ namespace ANSCENTER } // ── Load model ────────────────────────────────────────────────────── -#ifdef _WIN32 - std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end()); - session_ = Ort::Session(env_, w_modelPath.c_str(), sessionOptions_); -#else - session_ = Ort::Session(env_, modelPath.c_str(), sessionOptions_); -#endif + session_ = MakeSession(env_, model, sessionOptions_); Ort::AllocatorWithDefaultOptions allocator; @@ -300,7 +300,7 @@ namespace ANSCENTER std::cout << "Warning: No class names loaded. Predictions will use numeric IDs." << std::endl; } - std::cout << "[ANSONNXCL] Initialized successfully. Model: " << modelPath << std::endl; + std::cout << "[ANSONNXCL] Initialized successfully. Model: " << ModelSourceName(model) << std::endl; // ── Warmup ────────────────────────────────────────────────────────── DEBUG_PRINT("Starting model warmup..."); diff --git a/modules/ANSODEngine/ANSONNXCL.h b/modules/ANSODEngine/ANSONNXCL.h index f60659b..fdc4428 100644 --- a/modules/ANSODEngine/ANSONNXCL.h +++ b/modules/ANSODEngine/ANSONNXCL.h @@ -2,6 +2,7 @@ #define ANSONNXCL_H #pragma once #include "ANSEngineCommon.h" +#include "ANSModelSource.h" #include "engine.h" #include @@ -65,6 +66,8 @@ namespace ANSCENTER { const std::string& strategy = "resize"); void warmupModel(); bool Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU = true); + // Same load, from a model already in memory (an opened package). + bool Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU = true); bool preprocess(const cv::Mat& image, float*& blob, std::vector& inputTensorShape); // outputTensors is non-const because GetTensorMutableData() (the // ORT API that doesn't hang on AMD DirectML) requires a non-const diff --git a/modules/ANSODEngine/ANSONNXOBB.cpp b/modules/ANSODEngine/ANSONNXOBB.cpp index feafafe..b07a600 100644 --- a/modules/ANSODEngine/ANSONNXOBB.cpp +++ b/modules/ANSODEngine/ANSONNXOBB.cpp @@ -686,6 +686,11 @@ namespace ANSCENTER { } } bool ANSONNXOBB::Init(const std::string& modelPath, bool useGPU, int deviceId) + { + return Init(ModelSource::File(modelPath), useGPU, deviceId); + } + + bool ANSONNXOBB::Init(const ModelSource& model, bool useGPU, int deviceId) { std::lock_guard lock(_mutex); try { @@ -834,12 +839,7 @@ namespace ANSCENTER { } // ── Load model ────────────────────────────────────────────────────── -#ifdef _WIN32 - std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end()); - session = Ort::Session(env, w_modelPath.c_str(), sessionOptions); -#else - session = Ort::Session(env, modelPath.c_str(), sessionOptions); -#endif + session = MakeSession(env, model, sessionOptions); Ort::AllocatorWithDefaultOptions allocator; diff --git a/modules/ANSODEngine/ANSONNXOBB.h b/modules/ANSODEngine/ANSONNXOBB.h index f7ab2ac..3fdfe4f 100644 --- a/modules/ANSODEngine/ANSONNXOBB.h +++ b/modules/ANSODEngine/ANSONNXOBB.h @@ -2,6 +2,7 @@ #define ANSONNXOBB_H #pragma once #include "ANSEngineCommon.h" +#include "ANSModelSource.h" #include "engine.h" namespace ANSCENTER { @@ -73,6 +74,8 @@ namespace ANSCENTER { void warmupModel(); bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0); + // Same load, from a model already in memory (an opened package). + bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0); cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector& inputTensorShape); // outputTensors is non-const because GetTensorMutableData() requires // a non-const Ort::Value receiver — see ANSONNXCL.h for full note. diff --git a/modules/ANSODEngine/ANSONNXPOSE.cpp b/modules/ANSODEngine/ANSONNXPOSE.cpp index 1832288..9932dd6 100644 --- a/modules/ANSODEngine/ANSONNXPOSE.cpp +++ b/modules/ANSODEngine/ANSONNXPOSE.cpp @@ -270,6 +270,11 @@ namespace ANSCENTER { } bool ANSONNXPOSE::Init(const std::string& modelPath, bool useGPU, int deviceId) + { + return Init(ModelSource::File(modelPath), useGPU, deviceId); + } + + bool ANSONNXPOSE::Init(const ModelSource& model, bool useGPU, int deviceId) { std::lock_guard lock(_mutex); try { @@ -418,12 +423,7 @@ namespace ANSCENTER { } // ── Load model ────────────────────────────────────────────────────── -#ifdef _WIN32 - std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end()); - session = Ort::Session(env, w_modelPath.c_str(), sessionOptions); -#else - session = Ort::Session(env, modelPath.c_str(), sessionOptions); -#endif + session = MakeSession(env, model, sessionOptions); Ort::AllocatorWithDefaultOptions allocator; diff --git a/modules/ANSODEngine/ANSONNXPOSE.h b/modules/ANSODEngine/ANSONNXPOSE.h index c571721..c593dd9 100644 --- a/modules/ANSODEngine/ANSONNXPOSE.h +++ b/modules/ANSODEngine/ANSONNXPOSE.h @@ -2,6 +2,7 @@ #define ANSONNXPOSE_H #pragma once #include "ANSEngineCommon.h" +#include "ANSModelSource.h" #include "engine.h" namespace ANSCENTER { @@ -40,6 +41,8 @@ namespace ANSCENTER { float kptThreshold = 0.5); void warmupModel(); bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0); + // Same load, from a model already in memory (an opened package). + bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0); cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector& inputTensorShape); // outputTensors is non-const because GetTensorMutableData() requires // a non-const Ort::Value receiver — see ANSONNXCL.h for full note. diff --git a/modules/ANSODEngine/ANSONNXSEG.cpp b/modules/ANSODEngine/ANSONNXSEG.cpp index 08a7da7..0ab5404 100644 --- a/modules/ANSODEngine/ANSONNXSEG.cpp +++ b/modules/ANSODEngine/ANSONNXSEG.cpp @@ -267,6 +267,11 @@ namespace ANSCENTER { } } bool ANSONNXSEG::Init(const std::string& modelPath, bool useGPU, int deviceId) + { + return Init(ModelSource::File(modelPath), useGPU, deviceId); + } + + bool ANSONNXSEG::Init(const ModelSource& model, bool useGPU, int deviceId) { std::lock_guard lock(_mutex); try { @@ -415,12 +420,7 @@ namespace ANSCENTER { } // ── Load model ────────────────────────────────────────────────────── -#ifdef _WIN32 - std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end()); - session = Ort::Session(env, w_modelPath.c_str(), sessionOptions); -#else - session = Ort::Session(env, modelPath.c_str(), sessionOptions); -#endif + session = MakeSession(env, model, sessionOptions); numInputNodes = session.GetInputCount(); numOutputNodes = session.GetOutputCount(); diff --git a/modules/ANSODEngine/ANSONNXSEG.h b/modules/ANSODEngine/ANSONNXSEG.h index fcc2e46..d1f3cdf 100644 --- a/modules/ANSODEngine/ANSONNXSEG.h +++ b/modules/ANSODEngine/ANSONNXSEG.h @@ -2,6 +2,7 @@ #define ANSONNXSEG_H #pragma once #include "ANSEngineCommon.h" +#include "ANSModelSource.h" #include "engine.h" namespace ANSCENTER { @@ -50,6 +51,8 @@ namespace ANSCENTER { void warmupModel(); bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0); + // Same load, from a model already in memory (an opened package). + bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0); cv::Mat preprocess(const cv::Mat& image,float*& blobPtr,std::vector& inputTensorShape); // outputs is non-const because GetTensorMutableData() requires a // non-const Ort::Value receiver — see ANSONNXCL.h for full note. diff --git a/modules/ANSODEngine/ANSYOLO12OD.cpp b/modules/ANSODEngine/ANSYOLO12OD.cpp index adb492e..78c4bd9 100644 --- a/modules/ANSODEngine/ANSYOLO12OD.cpp +++ b/modules/ANSODEngine/ANSYOLO12OD.cpp @@ -278,6 +278,11 @@ namespace ANSCENTER { } } bool YOLO12OD::loadModel(const std::string& modelPath, bool useGPU) + { + return loadModel(ModelSource::File(modelPath), useGPU); + } + + bool YOLO12OD::loadModel(const ModelSource& model, bool useGPU) { try { const auto& ep = ANSCENTER::EPLoader::Current(); @@ -418,12 +423,7 @@ namespace ANSCENTER { } // ── Load model ────────────────────────────────────────────────────── -#ifdef _WIN32 - std::wstring w_modelPath(modelPath.begin(), modelPath.end()); - session = Ort::Session(env, w_modelPath.c_str(), sessionOptions); -#else - session = Ort::Session(env, modelPath.c_str(), sessionOptions); -#endif + session = MakeSession(env, model, sessionOptions); Ort::AllocatorWithDefaultOptions allocator; diff --git a/modules/ANSODEngine/ANSYOLO12OD.h b/modules/ANSODEngine/ANSYOLO12OD.h index f7dc04f..9410779 100644 --- a/modules/ANSODEngine/ANSYOLO12OD.h +++ b/modules/ANSODEngine/ANSYOLO12OD.h @@ -2,6 +2,7 @@ #define ANSYOL12OOD_H #pragma once #include "ANSEngineCommon.h" +#include "ANSModelSource.h" #include #include #include @@ -46,6 +47,8 @@ namespace ANSCENTER { float m_imgHeight = 0; protected: bool loadModel(const std::string& modelPath, bool useGPU = true); + // Same load, from a model already in memory (an opened package). + bool loadModel(const ModelSource& model, bool useGPU = true); std::vector detect(const cv::Mat& image, float confThreshold = 0.4f, float iouThreshold = 0.45f); //cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector& inputTensorShape); cv::Mat preprocess(const cv::Mat& image, std::vector& blob, std::vector& inputTensorShape); diff --git a/modules/ANSODEngine/ANSYOLOOD.cpp b/modules/ANSODEngine/ANSYOLOOD.cpp index 263836a..648e404 100644 --- a/modules/ANSODEngine/ANSYOLOOD.cpp +++ b/modules/ANSODEngine/ANSYOLOOD.cpp @@ -205,6 +205,11 @@ namespace ANSCENTER } } bool YOLOOD::loadYoloModel(const std::string& modelPath, const bool& isGPU, const cv::Size& inputSize) + { + return loadYoloModel(ModelSource::File(modelPath), isGPU, inputSize); + } + + bool YOLOOD::loadYoloModel(const ModelSource& model, const bool& isGPU, const cv::Size& inputSize) { try { const auto& ep = ANSCENTER::EPLoader::Current(); @@ -358,8 +363,7 @@ namespace ANSCENTER } // ── Load model ────────────────────────────────────────────────────── - std::wstring w_modelPath = String2WString(modelPath.c_str()); - session = Ort::Session(env, w_modelPath.c_str(), sessionOptions); + session = MakeSession(env, model, sessionOptions); Ort::AllocatorWithDefaultOptions allocator; // ── Input shape ───────────────────────────────────────────────────── diff --git a/modules/ANSODEngine/ANSYOLOOD.h b/modules/ANSODEngine/ANSYOLOOD.h index 8bb3002..8de6079 100644 --- a/modules/ANSODEngine/ANSYOLOOD.h +++ b/modules/ANSODEngine/ANSYOLOOD.h @@ -2,6 +2,7 @@ #define ANSYOLOOD_H #pragma once #include "ANSEngineCommon.h" +#include "ANSModelSource.h" #include //#define USEOPENCVDNN namespace ANSCENTER { @@ -38,6 +39,8 @@ namespace ANSCENTER { Ort::SessionOptions sessionOptions{ nullptr }; Ort::Session session{ nullptr }; bool loadYoloModel(const std::string& modelPath,const bool& isGPU = true,const cv::Size& inputSize = cv::Size(640, 640)); + // Same load, from a model already in memory (an opened package). + bool loadYoloModel(const ModelSource& model, const bool& isGPU = true, const cv::Size& inputSize = cv::Size(640, 640)); std::vector detect(cv::Mat& image, const float& confThreshold, const float& iouThreshold); void preprocessing(const cv::Mat& image,std::vector& blob,std::vector& inputTensorShape); //cv::Mat preprocessv11(const cv::Mat& image, float*& blob, std::vector& inputTensorShape);