Files
ANSCORE/modules/ANSODEngine/ANSYOLOOD.h
nghianguyenandClaude Opus 5 5820a5e61b feat(ODEngine): load a model from memory, not only from a path
Every engine built its session from a file path, which is why opening 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. The new package format cannot avoid that from the outside — the
engines have to be able to take bytes.

ANSModelSource.h introduces a ModelSource (a path, or bytes with a name
for logs) and one MakeSession that builds from either. The six ONNX
engines — classification, OBB, pose, segmentation, YOLO12 and YOLO —
each gain a load overload taking it, with the existing string form
delegating so no caller changes. The Windows wide-path conversion and
the from-memory call now live in one place instead of the eleven copies
they were spread across; YOLOOD, which had only the Windows branch,
becomes portable as a side effect.

The factories are named rather than implicit, so a call with a plain
string literal still resolves to the path overload without ambiguity.

Not compiled here: this layer builds only where CUDA is present, so
verification on this machine was the new header compiled and run, plus a
structural check that every engine declares and defines both overloads,
calls MakeSession exactly once, and has no raw session construction
left. It needs a build on a CUDA box before it ships.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 20:29:59 +10:00

67 lines
4.3 KiB
C++

#ifndef ANSYOLOOD_H
#define ANSYOLOOD_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include <onnxruntime_cxx_api.h>
//#define USEOPENCVDNN
namespace ANSCENTER {
class ANSENGINE_API YOLOOD :public ANSODBase {
public:
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
virtual bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap)override;
virtual bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
std::vector<Object> RunInference(const cv::Mat& input);
std::vector<Object> RunInference(const cv::Mat& input, const std::string& camera_id);
bool Destroy();
~YOLOOD();
private:
std::shared_ptr<cv::dnn::Net> _net;
std::string _modelFilePath;
bool _letterBoxForSquare{ true };
bool _isDarkNet{false};
std::vector<float> _blob; // Reusable buffer
cv::Mat _frameBuffer; // Reusable buffer for color conversion
private:
std::vector<Object> RunInferenceFromONNX(const cv::Mat& input, const std::string& camera_id);
std::vector<Object> RunInferenceFromYoloV5(const cv::Mat& input, const std::string& camera_id);
std::vector<Object> RunInferenceFromYoloV11(const cv::Mat& input, const std::string& camera_id);
std::vector<Object> RunInferenceFromDarkNet(const cv::Mat& input, const std::string& camera_id);
std::vector<Object> RunInferenceFromYoloV8(const cv::Mat& input, const std::string& camera_id);// OpenCV Style
void LoadOnnxNetwork();
void LoadDarknetNetwork();
void SetEngineType();
private:
Ort::Env env{ nullptr };
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<Object> detect(cv::Mat& image, const float& confThreshold, const float& iouThreshold);
void preprocessing(const cv::Mat& image,std::vector<float>& blob,std::vector<int64_t>& inputTensorShape);
//cv::Mat preprocessv11(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
cv::Mat preprocessv11(const cv::Mat& image, std::vector<float>& blob, std::vector<int64_t>& inputTensorShape);
std::vector<Object> postprocessing(const cv::Size& resizedImageShape,const cv::Size& originalImageShape,std::vector<Ort::Value>& outputTensors,
const float& confThreshold, const float& iouThreshold);
std::vector<Object> postprocessv11(const cv::Size& originalImageSize,const cv::Size& resizedImageShape,std::vector<Ort::Value>& outputTensors,float confThreshold,float iouThreshold);
BoundingBox scaleCoordsv11(const cv::Size& imageShape, BoundingBox coords,const cv::Size& imageOriginalShape, bool p_Clip);
std::vector<const char*> inputNodeNames;
std::vector<const char*> outputNodeNames;
bool isDynamicInputShape{};
cv::Size inputImageShape; // Expected input image shape for the model
float resizeScales;//letterbox scale
float m_imgWidth = 0;
float m_imgHeight = 0;
std::vector<Object> detectv11(const cv::Mat& image, float confThreshold, float iouThreshold);
//ultility functions
void getBestClassInfo(std::vector<float>::iterator it, const int& numClasses,float& bestConf, int& bestClassId);
void letterbox(const cv::Mat& image, cv::Mat& outImage,const cv::Size& newShape,const cv::Scalar& color,bool auto_,bool scaleFill,bool scaleUp,int stride);
void scaleCoords(const cv::Size& imageShape, cv::Rect& box, const cv::Size& imageOriginalShape);
size_t vectorProduct(const std::vector<int64_t>& vector);
};
}
#endif