AxeraModel.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #include <string>
  3. #include <chrono>
  4. #include "nlohmann/json.hpp"
  5. #include "Common.h"
  6. #include "AIStatus.h"
  7. #include "file.hpp"
  8. #include "io.hpp"
  9. #include "ax_sys_api.h"
  10. #include "joint_adv.h"
  11. #include "cv/cv.hpp"
  12. #include "cv/utils.hpp"
  13. #include "Detection.hpp"
  14. class ax_crop_resize_nv12
  15. {
  16. public:
  17. ax_crop_resize_nv12() = default;
  18. int init(int input_h, int input_w, int model_h, int model_w, int ModelConfigype);
  19. int deinit();
  20. int run_crop_resize_nv12(void *input_data, void *output_data, int &output_data_size);
  21. // int run_crop_resize_nv12_new(void *input_data);
  22. // AX_NPU_CV_Image* m_output_image;
  23. private:
  24. AX_NPU_CV_Image* m_input_image;
  25. AX_NPU_CV_Image* m_output_image;
  26. AX_NPU_CV_Box* m_box;
  27. int m_model_type = AX_NPU_MODEL_TYPE_1_1_1;
  28. };
  29. class AxeraModel
  30. {
  31. public:
  32. /**
  33. * @brief 预处理结构体:用于表示图像预处理的状态和配置。
  34. */
  35. struct PreprocessConfig
  36. {
  37. // preprocess resource
  38. void *dstVirtualAddress;
  39. int dstImageSize;
  40. ax_crop_resize_nv12 axcv_imageconvert;
  41. // net wh
  42. int netWidth;
  43. int netHeight;
  44. // src wh
  45. int srcWidth;
  46. int srcHeight;
  47. float scaleInfo;
  48. };
  49. /**
  50. * @brief 模型结构体:用于表示神经网络模型的状态和配置。
  51. */
  52. struct ModelConfig
  53. {
  54. // runtime handle
  55. AX_JOINT_HANDLE joint_handle;
  56. AX_JOINT_SDK_ATTR_T joint_attr;
  57. // context
  58. AX_JOINT_EXECUTION_CONTEXT joint_ctx;
  59. AX_JOINT_EXECUTION_CONTEXT_SETTING_T joint_ctx_settings;
  60. // prepare_io resurce
  61. AX_JOINT_IO_T joint_io_arr;
  62. AX_JOINT_IO_SETTING_T joint_io_setting;
  63. const AX_JOINT_IO_INFO_T *io_info;
  64. // result
  65. int netWidth, netHeight; ///< 网络的宽高
  66. std::vector<detection::Box> objects;
  67. std::vector<float_t *> outputBlobsPtr; ///< 输出 blob 的张量指针(request用)
  68. std::vector<int> outputBlobsPtrSize; ///< 输出 blob 的张量指针(request用)
  69. std::vector<std::array<size_t, 4>> outputDataDims;
  70. unsigned int strides[5] = {0}; ///< 步长数组
  71. std::string modelType; ///< 模型类型
  72. std::vector<std::string> classNames; ///< 类别名称数组
  73. };
  74. struct PostprocessConfig
  75. {
  76. std::string name;
  77. unsigned int yoloAnchor[30] = {0}; ///< yolo_anchor数组
  78. };
  79. /**
  80. * @brief 初始化预处理资源
  81. *
  82. * 根据提供的配置信息初始化预处理资源。
  83. *
  84. * @param cfg 预处理配置的 JSON 对象
  85. * @param preprocessConfig 预处理状态结构体指针
  86. * @return 表示预处理资源初始化成功或失败的错误代码。
  87. */
  88. int initializePreprocessResource(const nlohmann::json_abi_v3_11_2::json cfg, PreprocessConfig *preprocessConfig);
  89. /**
  90. * @brief 初始化模型资源
  91. *
  92. * 根据提供的配置信息初始化模型资源。
  93. *
  94. * @param cfg 模型配置的 JSON 对象
  95. * @param modelConfig 模型状态结构体指针
  96. * @return 表示模型资源初始化成功或失败的错误代码。
  97. */
  98. int initializeModelResource(const nlohmann::json_abi_v3_11_2::json cfg, ModelConfig *modelConfig);
  99. /**
  100. * @brief 预处理图像
  101. *
  102. * 使用提供的预处理状态和图像帧状态执行图像预处理。
  103. *
  104. * @param preprocessConfig 预处理状态结构体指针
  105. * @param frame_st 图像帧状态结构体指针
  106. * @return 表示预处理图像操作成功或失败的错误代码。
  107. */
  108. int preprocessImage(PreprocessConfig *preprocessConfig, frame_t *frame_st);
  109. /**
  110. * @brief 模型推理
  111. *
  112. * 使用提供的预处理状态和模型状态执行推理。
  113. *
  114. * @param preprocessConfig 预处理状态结构体指针
  115. * @param modelConfig 模型状态结构体指针
  116. * @param infer_result 存储模型结果的指针
  117. * @return 表示模型推理成功或失败的错误代码。
  118. */
  119. int inferModel(unsigned char *preProcessedImg,int preProcessedImgSize, ModelConfig *modelConfig);
  120. /**
  121. * @brief 释放预处理资源
  122. *
  123. * 使用提供的预处理状态释放相关资源。
  124. *
  125. * @param preprocessConfig 预处理状态结构体指针
  126. * @return 表示释放预处理资源成功或失败的错误代码。
  127. */
  128. int postprocess(std::vector<BoundingBox> *boundingBoxes,
  129. std::vector<float *> outputBlobsPtr,
  130. std::vector<std::array<size_t, 4>> outputDataDims,
  131. unsigned int strides[5],
  132. unsigned int yoloAnchor[30],
  133. std::vector<std::string> classNames,
  134. std::unordered_map<std::string, float> classAttributes,
  135. float scaleInfo, int srcWidth,int srcHeight);
  136. int releasePreprocessResources(PreprocessConfig *preprocessConfig);
  137. /**
  138. * @brief 释放模型资源
  139. *
  140. * 使用提供的模型状态释放相关资源。
  141. *
  142. * @param modelConfig 模型状态结构体指针
  143. * @return 表示释放模型资源成功或失败的错误代码。
  144. */
  145. int releaseModelResources(ModelConfig *modelConfig);
  146. };