#ifndef ANSYOLOOD_H #define ANSYOLOOD_H #pragma once #include "ANSEngineCommon.h" #include //#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 RunInference(const cv::Mat& input); std::vector RunInference(const cv::Mat& input, const std::string& camera_id); bool Destroy(); ~YOLOOD(); private: std::shared_ptr _net; std::string _modelFilePath; bool _letterBoxForSquare{ true }; bool _isDarkNet{false}; std::vector _blob; // Reusable buffer cv::Mat _frameBuffer; // Reusable buffer for color conversion private: std::vector RunInferenceFromONNX(const cv::Mat& input, const std::string& camera_id); std::vector RunInferenceFromYoloV5(const cv::Mat& input, const std::string& camera_id); std::vector RunInferenceFromYoloV11(const cv::Mat& input, const std::string& camera_id); std::vector RunInferenceFromDarkNet(const cv::Mat& input, const std::string& camera_id); std::vector 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)); 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); cv::Mat preprocessv11(const cv::Mat& image, std::vector& blob, std::vector& inputTensorShape); std::vector postprocessing(const cv::Size& resizedImageShape,const cv::Size& originalImageShape,std::vector& outputTensors, const float& confThreshold, const float& iouThreshold); std::vector postprocessv11(const cv::Size& originalImageSize,const cv::Size& resizedImageShape,std::vector& outputTensors,float confThreshold,float iouThreshold); BoundingBox scaleCoordsv11(const cv::Size& imageShape, BoundingBox coords,const cv::Size& imageOriginalShape, bool p_Clip); std::vector inputNodeNames; std::vector 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 detectv11(const cv::Mat& image, float confThreshold, float iouThreshold); //ultility functions void getBestClassInfo(std::vector::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& vector); }; } #endif