pptinypose.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "ultra_infer/ultra_infer_model.h"
  16. #include "ultra_infer/vision/common/processors/transform.h"
  17. #include "ultra_infer/vision/common/result.h"
  18. #include "ultra_infer/vision/keypointdet/pptinypose/pptinypose_utils.h"
  19. namespace ultra_infer {
  20. namespace vision {
  21. /** \brief All keypoint detection model APIs are defined inside this namespace
  22. *
  23. */
  24. namespace keypointdetection {
  25. /*! @brief PPTinyPose model object used when to load a PPTinyPose model exported
  26. * by PaddleDetection
  27. */
  28. class ULTRAINFER_DECL PPTinyPose : public UltraInferModel {
  29. public:
  30. /** \brief Set path of model file and configuration file, and the
  31. * configuration of runtime
  32. *
  33. * \param[in] model_file Path of model file, e.g pptinypose/model.pdmodel
  34. * \param[in] params_file Path of parameter file, e.g
  35. * pptinypose/model.pdiparams, if the model format is ONNX, this parameter
  36. * will be ignored \param[in] config_file Path of configuration file for
  37. * deployment, e.g pptinypose/infer_cfg.yml \param[in] custom_option
  38. * RuntimeOption for inference, the default will use cpu, and choose the
  39. * backend defined in `valid_cpu_backends` \param[in] model_format Model
  40. * format of the loaded model, default is Paddle format
  41. */
  42. PPTinyPose(const std::string &model_file, const std::string &params_file,
  43. const std::string &config_file,
  44. const RuntimeOption &custom_option = RuntimeOption(),
  45. const ModelFormat &model_format = ModelFormat::PADDLE);
  46. /// Get model's name
  47. std::string ModelName() const { return "PaddleDetection/PPTinyPose"; }
  48. /** \brief Predict the keypoint detection result for an input image
  49. *
  50. * \param[in] im The input image data, comes from cv::imread()
  51. * \param[in] result The output keypoint detection result will be written to
  52. * this structure \return true if the keypoint prediction succeeded, otherwise
  53. * false
  54. */
  55. bool Predict(cv::Mat *im, KeyPointDetectionResult *result);
  56. /** \brief Predict the keypoint detection result with given detection result
  57. * for an input image
  58. *
  59. * \param[in] im The input image data, comes from cv::imread()
  60. * \param[in] result The output keypoint detection result will be written to
  61. * this structure \param[in] detection_result The structure stores pedestrian
  62. * detection result, which is used to crop image for multi-persons keypoint
  63. * detection \return true if the keypoint prediction succeeded, otherwise
  64. * false
  65. */
  66. bool Predict(cv::Mat *im, KeyPointDetectionResult *result,
  67. const DetectionResult &detection_result);
  68. /** \brief Whether using Distribution-Aware Coordinate Representation for
  69. * Human Pose Estimation(DARK for short) in postprocess, default is true
  70. */
  71. bool use_dark = true;
  72. /// This function will disable normalize in preprocessing step.
  73. void DisableNormalize() {
  74. disable_normalize_ = true;
  75. BuildPreprocessPipelineFromConfig();
  76. }
  77. /// This function will disable hwc2chw in preprocessing step.
  78. void DisablePermute() {
  79. disable_permute_ = true;
  80. BuildPreprocessPipelineFromConfig();
  81. }
  82. protected:
  83. bool Initialize();
  84. /// Build the preprocess pipeline from the loaded model
  85. bool BuildPreprocessPipelineFromConfig();
  86. /// Preprocess an input image, and set the preprocessed results to `outputs`
  87. bool Preprocess(Mat *mat, std::vector<FDTensor> *outputs);
  88. /// Postprocess the inferenced results, and set the final result to `result`
  89. bool Postprocess(std::vector<FDTensor> &infer_result,
  90. KeyPointDetectionResult *result,
  91. const std::vector<float> &center,
  92. const std::vector<float> &scale);
  93. private:
  94. std::vector<std::shared_ptr<Processor>> processors_;
  95. std::string config_file_;
  96. // for recording the switch of hwc2chw
  97. bool disable_permute_ = false;
  98. // for recording the switch of normalize
  99. bool disable_normalize_ = false;
  100. };
  101. } // namespace keypointdetection
  102. } // namespace vision
  103. } // namespace ultra_infer