123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #pragma once
- #include <string>
- #include <unistd.h>
- #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<std::string> classNames; ///< 类别名称数组
- std::vector<std::string> inputBlobsName; ///< 输入 blob 的名称数组
- std::vector<vx_tensor> inputBlobsTensor; ///< 输入 blob 的张量数组
- std::vector<std::string> outputBlobsName; ///< 输出 blob 的名称数组
- std::vector<vx_tensor> outputBlobsTensor; ///< 输出 blob 的张量数组
- std::vector<int8_t *> outputBlobsPtr; ///< 输出 blob 的张量指针(request用)
- std::vector<int> outputBlobsPtrSize; ///< 输出 blob 的张量指针(request用)
- ///< 输入数据的维度
- // std::vector<std::array<vx_size, 4>> inputDataDims;
- // std::vector<vx_size[4]> outputDataDims; ///< 输出数据的维度
- std::vector<std::array<vx_size, 4>> outputDataDims;
- // vx_size outputDataDims[4];
- // std::vector<detection::Box> 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<std::string> classNames; ///< 类别名称数组
- // vx_enum dataType[5]; ///< 数据类型数组
- // int floatingPoint[5] = {0}; ///< 浮点数位数数组
- // unsigned int strides[5] = {0}; ///< 步长数组
- // std::vector<vx_size[4]> 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<detection::Box> *proposals, std::unordered_map<std::string, float> classAttributes);
- // int postprocess(std::vector<int8_t *> outputBlobsPtr, PostprocessConfig *postProcessConfig, std::vector<BoundingBox> *boundingBoxes, std::unordered_map<std::string, float> classAttributes);
- int postprocess(std::vector<BoundingBox> *boundingBoxes,
- std::vector<int8_t *> outputBlobsPtr,
- std::vector<std::array<size_t, 4>> outputDataDims,
- unsigned int strides[5],
- int floatingPoint[5],
- vx_enum dataType[5],
- unsigned int yoloAnchor[30],
- std::vector<std::string> classNames,
- std::unordered_map<std::string, float> classAttributes,
- float scaleInfo,int srcWidth,int srcHeight);
- int postprocess(std::vector<BoundingBox> *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);
- };
|