rvm.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) 2024 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. from __future__ import absolute_import
  15. from .... import UltraInferModel, ModelFormat
  16. from .... import c_lib_wrap as C
  17. class RobustVideoMatting(UltraInferModel):
  18. def __init__(
  19. self,
  20. model_file,
  21. params_file="",
  22. runtime_option=None,
  23. model_format=ModelFormat.ONNX,
  24. ):
  25. """Load a video matting model exported by RobustVideoMatting.
  26. :param model_file: (str)Path of model file, e.g rvm/rvm_mobilenetv3_fp32.onnx
  27. :param params_file: (str)Path of parameters file, if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string
  28. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  29. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model, default is ONNX
  30. """
  31. super(RobustVideoMatting, self).__init__(runtime_option)
  32. self._model = C.vision.matting.RobustVideoMatting(
  33. model_file, params_file, self._runtime_option, model_format
  34. )
  35. assert self.initialized, "RobustVideoMatting initialize failed."
  36. def predict(self, input_image):
  37. """Matting an input image
  38. :param im: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  39. :return: MattingResult
  40. """
  41. return self._model.predict(input_image)
  42. @property
  43. def size(self):
  44. """
  45. Returns the preprocess image size
  46. """
  47. return self._model.size
  48. @property
  49. def video_mode(self):
  50. """
  51. Whether to open the video mode, if there are some irrelevant pictures, set it to fasle, the default is true
  52. """
  53. return self._model.video_mode
  54. @property
  55. def swap_rb(self):
  56. """
  57. Whether convert to RGB, Set to false if you have converted YUV format images to RGB outside the model, dafault true
  58. """
  59. return self._model.swap_rb
  60. @size.setter
  61. def size(self, wh):
  62. """
  63. Set the preprocess image size
  64. """
  65. assert isinstance(
  66. wh, (list, tuple)
  67. ), "The value to set `size` must be type of tuple or list."
  68. assert (
  69. len(wh) == 2
  70. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  71. len(wh)
  72. )
  73. self._model.size = wh
  74. @video_mode.setter
  75. def video_mode(self, value):
  76. """
  77. Set video_mode property, the default is true
  78. """
  79. assert isinstance(
  80. value, bool
  81. ), "The value to set `video_mode` must be type of bool."
  82. self._model.video_mode = value
  83. @swap_rb.setter
  84. def swap_rb(self, value):
  85. """
  86. Set swap_rb property, the default is true
  87. """
  88. assert isinstance(
  89. value, bool
  90. ), "The value to set `swap_rb` must be type of bool."
  91. self._model.swap_rb = value