ppmatting.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. namespace ultra_infer {
  19. namespace vision {
  20. /** \brief All object matting model APIs are defined inside this namespace
  21. *
  22. */
  23. namespace matting {
  24. /*! @brief PPMatting model object used when to load a PPMatting model exported
  25. * by PPMatting.
  26. */
  27. class ULTRAINFER_DECL PPMatting : public UltraInferModel {
  28. public:
  29. /** \brief Set path of model file and configuration file, and the
  30. * configuration of runtime
  31. *
  32. * \param[in] model_file Path of model file, e.g PPMatting-512/model.pdmodel
  33. * \param[in] params_file Path of parameter file, e.g
  34. * PPMatting-512/model.pdiparams, if the model format is ONNX, this parameter
  35. * will be ignored \param[in] config_file Path of configuration file for
  36. * deployment, e.g PPMatting-512/infer_cfg.yml \param[in] custom_option
  37. * RuntimeOption for inference, the default will use cpu, and choose the
  38. * backend defined in `valid_cpu_backends` \param[in] model_format Model
  39. * format of the loaded model, default is Paddle format
  40. */
  41. PPMatting(const std::string &model_file, const std::string &params_file,
  42. const std::string &config_file,
  43. const RuntimeOption &custom_option = RuntimeOption(),
  44. const ModelFormat &model_format = ModelFormat::PADDLE);
  45. std::string ModelName() const { return "PaddleMatting"; }
  46. /** \brief Predict the matting result for an input image
  47. *
  48. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  49. * with layout HWC, BGR format \param[in] result The output matting result
  50. * will be written to this structure \return true if the prediction succeeded,
  51. * otherwise false
  52. */
  53. virtual bool Predict(cv::Mat *im, MattingResult *result);
  54. private:
  55. bool Initialize();
  56. bool BuildPreprocessPipelineFromConfig();
  57. bool Preprocess(Mat *mat, FDTensor *outputs,
  58. std::map<std::string, std::array<int, 2>> *im_info);
  59. bool Postprocess(std::vector<FDTensor> &infer_result, MattingResult *result,
  60. const std::map<std::string, std::array<int, 2>> &im_info);
  61. std::vector<std::shared_ptr<Processor>> processors_;
  62. std::string config_file_;
  63. bool is_fixed_input_shape_;
  64. };
  65. } // namespace matting
  66. } // namespace vision
  67. } // namespace ultra_infer