#pragma once #include #include #include "ax_type.h" #include "VX/vx.h" #include "VX/vx_vendors.h" #include "VX/vx_types.h" #include "libnn/net_api.h" #include "nlohmann/json.hpp" #include "Detection.hpp" #include "AIStatus.h" #include "Common.h" /** * @brief EZModel模型类 * * 该类包含了用于管理和操作Eeasy模型的各种成员变量和方法。 */ class EeasyModel { public: /** * @brief 预处理结构体:用于表示图像预处理的状态和配置。 */ struct PreprocessConfig { vx_size dstHandle; ///< 预处理用的句柄 vx_context handleContext; ///< 预处理用的context int netWidth, netHeight; ///< 网络的宽高 int srcWidth, srcHeight; ///< 媒体流的宽高 float scaleInfo; ///< 缩放因子 vx_size dstImageSize; ///< 预处理结果图像大小 vx_size dstVirtualAddress; ///< 预处理结果虚拟地址 }; /** * @brief 模型结构体:用于表示神经网络模型的状态和配置。 */ struct ModelConfig { vx_context context; ///< 模型用的context vx_graph graph; ///< 模型用的graph int netWidth, netHeight; ///< 网络的宽高 // unsigned int anchorsG[30] = {0}; ///< yolo_anchor数组 std::string modelType; ///< 模型类型 std::vector classNames; ///< 类别名称数组 std::vector inputBlobsName; ///< 输入 blob 的名称数组 std::vector inputBlobsTensor; ///< 输入 blob 的张量数组 std::vector outputBlobsName; ///< 输出 blob 的名称数组 std::vector outputBlobsTensor; ///< 输出 blob 的张量数组 std::vector outputBlobsPtr; ///< 输出 blob 的张量指针(request用) std::vector outputBlobsPtrSize; ///< 输出 blob 的张量指针(request用) ///< 输入数据的维度 // std::vector> inputDataDims; // std::vector outputDataDims; ///< 输出数据的维度 std::vector> outputDataDims; // vx_size outputDataDims[4]; // std::vector proposals; ///< 推理解码后的box vx_enum dataType[5]; ///< 数据类型数组 int floatingPoint[5]; ///< 浮点数位数数组 // int floatingPoint[5] = {0}; ///< 浮点数位数数组 unsigned int strides[5] = {0}; ///< 步长数组 }; struct PostprocessConfig { std::string name; unsigned int yoloAnchor[30] = {0}; ///< yolo_anchor数组 // 模型传入 // std::string modelType; ///< 模型类型 // std::vector classNames; ///< 类别名称数组 // vx_enum dataType[5]; ///< 数据类型数组 // int floatingPoint[5] = {0}; ///< 浮点数位数数组 // unsigned int strides[5] = {0}; ///< 步长数组 // std::vector outputDataDims; ///< 输出数据的维度 // vx_size outputDataDims[4]; // int srcWidth, srcHeight; ///< 媒体流的宽高 // float scaleInfo; ///< 缩放因子 }; /** * @brief 初始化预处理资源 * * 根据提供的配置信息初始化预处理资源。 * * @param cfg 预处理配置的 JSON 对象 * @param preprocess_st 预处理状态结构体指针 * @return 表示预处理资源初始化成功或失败的错误代码。 * - AIStatus::StatusCode::SUCCESS :初始化成功 * - AIStatus::StatusCode::FAILED :初始化失败(已创建资源销毁成功) * - AIStatus::EeasyErrorCode::RELEASE_PREPROCESS_RESOURCES_ERROR :初始化失败(已创建资源销毁也失败) */ int initializePreprocessResource(const nlohmann::json_abi_v3_11_2::json cfg, PreprocessConfig *preprocessConfig); /** * @brief 初始化模型资源 * * 根据提供的配置信息初始化模型资源。 * * @param cfg 模型配置的 JSON 对象 * @param model_st 模型状态结构体指针 * @return 表示模型资源初始化成功或失败的错误代码。 * - AIStatus::StatusCode::SUCCESS :初始化成功 * - AIStatus::StatusCode::FAILED :初始化失败(已创建资源销毁成功) * - AIStatus::EeasyErrorCode::RELEASE_MODEL_RESOURCES_ERROR :初始化成功 */ int initializeModelResource(const nlohmann::json_abi_v3_11_2::json cfg, ModelConfig *modelConfig); /** * @brief 预处理图像 * * 使用提供的预处理状态和图像帧状态执行图像预处理。 * * @param preprocess_st 预处理状态结构体指针 * @param frame_st 图像帧状态结构体指针 * @return 表示预处理图像操作成功或失败的错误代码。 * - AIStatus::StatusCode::SUCCESS :预处理图像成功 * - AIStatus::EeasyErrorCode::INFER_IMAGECONVERT_ERROR :ImageConvert处理失败 */ int preprocessImage(PreprocessConfig *preprocessConfig, frame_t *frame); /** * @brief 模型推理 * * 使用提供的预处理状态和模型状态执行推理。 * * @param preprocess_st 预处理状态结构体指针 * @param model_st 模型状态结构体指针 * @param boundingBoxes 存储推理结果的边界框列表 * @param class_attributes 类别属性的映射表 * @return 表示模型推理成功或失败的错误代码。 * - AIStatus::StatusCode::SUCCESS :模型推理成功 * - AIStatus::EeasyErrorCode::INFER_INPUTDATAFROMMEM_ERROR :ImportNetInputDataFromMem操作失败 * - AIStatus::EeasyErrorCode::INFER_PROCESSGRAPH_ERROR :vxProcessGraph操作失败 * - AIStatus::EeasyErrorCode::INFER_FINISHGRAPH_ERROR :vxFinish操作失败 */ int inferModel(unsigned char *preProcessedImg,int preProcessedImgSize, ModelConfig *modelConfig); // std::vector *proposals, std::unordered_map classAttributes); // int postprocess(std::vector outputBlobsPtr, PostprocessConfig *postProcessConfig, std::vector *boundingBoxes, std::unordered_map classAttributes); int postprocess(std::vector *boundingBoxes, std::vector outputBlobsPtr, std::vector> outputDataDims, unsigned int strides[5], int floatingPoint[5], vx_enum dataType[5], unsigned int yoloAnchor[30], std::vector classNames, std::unordered_map classAttributes, float scaleInfo,int srcWidth,int srcHeight); int postprocess(std::vector *boundingBoxes); /** * @brief 释放预处理资源 * * 使用提供的预处理状态释放相关资源。 * * @param preprocess_st 预处理状态结构体指针 * @return 表示释放预处理资源成功或失败的错误代码。 * - AIStatus::StatusCode::SUCCESS :释放预处理资源成功 * - AIStatus::EeasyErrorCode::RELEASE_PREPROCESS_RESOURCES_ERROR :释放预处理资源失败 */ int releasePreprocessResources(PreprocessConfig *preprocessConfig); int releaseModelResources(ModelConfig *modelConfig); private: /** * @brief 将图像格式转换为EasyImage格式。 * * 此函数将给定的图像格式转换为EasyImage格式。 * * @param asj_format 输入图像格式,支持的格式包括 YUV420SPNV21。 * @param[out] out 输出的EasyImage格式。 * * @return 成功转换返回0,否则返回-1。 */ int convertImageFormatToEeasyImage(image_format_t asjFormat, img_fmt *out); };