infer_base.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*************************************************************************
  2. * Copyright (C) [2021] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef MODULES_INFER_BASE_HPP_
  21. #define MODULES_INFER_BASE_HPP_
  22. #include <memory>
  23. #include <string>
  24. #include <vector>
  25. #include "cnis/contrib/video_helper.h"
  26. #include "cnis/infer_server.h"
  27. #include "cnis/processor.h"
  28. #include "cnstream_frame_va.hpp"
  29. #include "obj_filter.hpp"
  30. #include "video_postproc.hpp"
  31. #include "video_preproc.hpp"
  32. namespace cnstream {
  33. using InferEngine = infer_server::InferServer;
  34. using InferVideoPixelFmt = infer_server::video::PixelFmt;
  35. using InferVideoFrame = infer_server::video::VideoFrame;
  36. using VFrameBoundingBox = infer_server::video::BoundingBox;
  37. using InferPreprocessType = infer_server::video::PreprocessType;
  38. using InferMluPreprocess = infer_server::video::PreprocessorMLU;
  39. using InferDataType = infer_server::DataType;
  40. using InferDimOrder = infer_server::DimOrder;
  41. using InferStatus = infer_server::Status;
  42. using InferBatchStrategy = infer_server::BatchStrategy;
  43. using InferModelInfoPtr = infer_server::ModelPtr;
  44. using InferEngineSession = infer_server::Session_t;
  45. using InferSessionDesc = infer_server::SessionDesc;
  46. using InferEngineDataObserver = infer_server::Observer;
  47. using InferPackagePtr = infer_server::PackagePtr;
  48. using InferBuffer = infer_server::Buffer;
  49. using InferDataPtr = infer_server::InferDataPtr;
  50. using InferBatchData = infer_server::BatchData;
  51. using InferUserData = infer_server::any;
  52. using InferShape = infer_server::Shape;
  53. using InferCpuPreprocess = infer_server::PreprocessorHost;
  54. using InferPostprocess = infer_server::Postprocessor;
  55. /**
  56. * @brief The inference parameters used in Inferencer2 Module.
  57. */
  58. struct Infer2Param {
  59. uint32_t device_id = 0;
  60. uint32_t priority = 0;
  61. uint32_t engine_num = 1;
  62. bool show_stats = false;
  63. InferBatchStrategy batch_strategy = InferBatchStrategy::DYNAMIC;
  64. uint32_t batching_timeout = 1000; ///< only support in dynamic batch strategy
  65. bool keep_aspect_ratio = false;
  66. InferVideoPixelFmt model_input_pixel_format = InferVideoPixelFmt::RGBA;
  67. InferDimOrder data_order = InferDimOrder::NHWC;
  68. std::vector<float> mean_;
  69. std::vector<float> std_;
  70. std::string func_name = "";
  71. std::string model_path = "";
  72. std::string preproc_name = "";
  73. std::string postproc_name = "";
  74. std::string obj_filter_name = "";
  75. bool normalize = false;
  76. bool object_infer = false;
  77. float threshold = 0.f;
  78. uint32_t infer_interval = 0;
  79. }; // struct Infer2Param
  80. class Inferencer2;
  81. class InferHandler {
  82. public:
  83. explicit InferHandler(Inferencer2* module, const Infer2Param& infer_params,
  84. std::shared_ptr<VideoPostproc> post_processor, std::shared_ptr<VideoPreproc> pre_processor,
  85. std::shared_ptr<ObjFilter> obj_filter)
  86. : module_(module),
  87. params_(infer_params),
  88. postprocessor_(post_processor),
  89. preprocessor_(pre_processor),
  90. obj_filter_(obj_filter) {}
  91. virtual ~InferHandler() {}
  92. virtual bool Open() = 0;
  93. virtual void Close() = 0;
  94. virtual int Process(CNFrameInfoPtr data, bool with_objs = false) = 0;
  95. virtual void WaitTaskDone(const std::string& stream_id) = 0;
  96. void TransmitData(const CNFrameInfoPtr& data);
  97. protected:
  98. Inferencer2* module_ = nullptr;
  99. Infer2Param params_;
  100. std::shared_ptr<VideoPostproc> postprocessor_ = nullptr;
  101. std::shared_ptr<VideoPreproc> preprocessor_ = nullptr;
  102. std::shared_ptr<ObjFilter> obj_filter_ = nullptr;
  103. };
  104. } // namespace cnstream
  105. #endif