#ifndef ANSONNXCL_H #define ANSONNXCL_H #pragma once #include "ANSEngineCommon.h" #include "engine.h" #include namespace ANSCENTER { // Forward declaration: avoid pulling ANSONNXYOLO.h into every consumer of // ANSONNXCL.h. Definition is included in ANSONNXCL.cpp. class ANSONNXYOLO; // ──────────────────────────────────────────────────────────────────────── // ANSONNXCL — verification facade over ANSONNXYOLO. // // ANSONNXYOLO already auto-detects classification models (single 2D // output [B, nc]) and routes through its own classification postprocess. // It also has the safe DML config (DisableMemPattern + ORT_SEQUENTIAL + // EXTENDED) and the IsDmlFatalError catch-and-rebuild path. // // We delegate every public method here to an internal ANSONNXYOLO so we // can A/B test whether ANSONNXCL's own session/postprocess code is the // real cause of the AMD same-process DML races we saw with the custom // helmet model. Public ABI is unchanged — external callers don't move. // ──────────────────────────────────────────────────────────────────────── class ANSENGINE_API ANSONNXCL :public ANSODBase { public: // Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale). ANSONNXCL(const ANSONNXCL&) = delete; ANSONNXCL& operator=(const ANSONNXCL&) = delete; ANSONNXCL(ANSONNXCL&&) = delete; ANSONNXCL& operator=(ANSONNXCL&&) = delete; ANSONNXCL() = default; 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 RunInference(const cv::Mat& input); std::vector RunInference(const cv::Mat& input, const std::string& camera_id); bool Destroy(); ~ANSONNXCL(); private: // The wrapped engine. Lazy-initialised on first public call so we // don't pay the cost (and pull in the dependency) for callers who // never use this class. std::unique_ptr _yoloDelegate; private: std::string _modelFilePath; bool _modelLoadValid; bool _fp16{ false }; // DML device-lost recovery: when DirectML fails fatally // (HRESULT 887A0005 device-removed, 80004005 graph/command-recorder // E_FAIL, etc. — see ANSCENTER::IsDmlFatalError) the Ort::Session // is permanently broken. We detect once, recreate on CPU via // Init(_modelFilePath, inputImageShape_, /*useGPU=*/false), and // suppress further error-log flooding via this flag. bool _dmlDeviceLost{ false }; size_t vectorProduct(const std::vector& vector); bool preprocessImageToTensor(const cv::Mat& image, cv::Mat& outImage, const cv::Size& targetShape, const cv::Scalar& color = cv::Scalar(0, 0, 0), bool scaleUp = true, const std::string& strategy = "resize"); void warmupModel(); bool Init(const std::string& modelPath, 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 // Ort::Value receiver. See comment at the GetTensorMutableData // call site in postprocess() for the full rationale. std::vector postprocess(std::vector& outputTensors, const std::string& camera_id); std::vector classify(const cv::Mat& image, const std::string& camera_id); private: Ort::Env env_{ nullptr }; Ort::SessionOptions sessionOptions_{ nullptr }; Ort::Session session_{ nullptr }; bool isDynamicInputShape_{}; cv::Size inputImageShape_{}; std::vector inputNodeNameAllocatedStrings_{}; std::vector inputNames_{}; std::vector outputNodeNameAllocatedStrings_{}; std::vector outputNames_{}; size_t numInputNodes_{}, numOutputNodes_{}; int numClasses_{ 0 }; float m_imgWidth = 0; float m_imgHeight = 0; }; } #endif #pragma once