processors.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 c_lib_wrap as C
  16. class Processor:
  17. def __init__(self):
  18. self.processor = None
  19. def __call__(self, mat):
  20. """call for processing input.
  21. :param mat: The input data FDMat or FDMatBatch.
  22. """
  23. self.processor(mat)
  24. class ResizeByShort(Processor):
  25. def __init__(self, target_size: int, interp=1, use_scale=True, max_hw=[]):
  26. """Create a ResizeByShort operation with the given parameters.
  27. :param target_size: The target short size to resize the image
  28. :param interp: Optionally, the interpolation mode for resizing image
  29. :param use_scale: Optionally, whether to scale image
  30. :param max_hw: Max spatial size which is used by ResizeByShort
  31. """
  32. self.processor = C.vision.processors.ResizeByShort(
  33. target_size, interp, use_scale, max_hw
  34. )
  35. class CenterCrop(Processor):
  36. def __init__(self, width, height):
  37. """Create a CenterCrop operation with the given parameters.
  38. :param width: Desired width of the cropped image
  39. :param height: Desired height of the cropped image
  40. """
  41. self.processor = C.vision.processors.CenterCrop(width, height)
  42. class Pad(Processor):
  43. def __init__(self, top: int, bottom: int, left: int, right: int, value=[]):
  44. """Create a Pad operation with the given parameters.
  45. :param top: The top padding
  46. :param bottom: The bottom padding
  47. :param left: The left padding
  48. :param right: The right padding
  49. :param value: the value that is used to pad on the input image
  50. """
  51. self.processor = C.vision.processors.Pad(top, bottom, left, right, value)
  52. class NormalizeAndPermute(Processor):
  53. def __init__(self, mean=[], std=[], is_scale=True, min=[], max=[], swap_rb=False):
  54. """Creae a Normalize and a Permute operation with the given parameters.
  55. :param mean: A list containing the mean of each channel
  56. :param std: A list containing the standard deviation of each channel
  57. :param is_scale: Specifies if the image are being scaled or not
  58. :param min: A list containing the minimum value of each channel
  59. :param max: A list containing the maximum value of each channel
  60. """
  61. self.processor = C.vision.processors.NormalizeAndPermute(
  62. mean, std, is_scale, min, max, swap_rb
  63. )
  64. class Cast(Processor):
  65. def __init__(self, dtype="float"):
  66. """Creat a new cast opereaton with given dtype
  67. :param dtype: Target dtype of the output
  68. """
  69. self.processor = C.vision.processors.Cast(dtype)
  70. class HWC2CHW(Processor):
  71. def __init__(self):
  72. """Creat a new hwc2chw processor with default dtype.
  73. :return An instance of processor `HWC2CHW`
  74. """
  75. self.processor = C.vision.processors.HWC2CHW()
  76. class Normalize(Processor):
  77. def __init__(self, mean, std, is_scale=True, min=[], max=[], swap_rb=False):
  78. """Creat a new normalize opereator with given paremeters.
  79. :param mean: A list containing the mean of each channel
  80. :param std: A list containing the standard deviation of each channel
  81. :param is_scale: Specifies if the image are being scaled or not
  82. :param min: A list containing the minimum value of each channel
  83. :param max: A list containing the maximum value of each channel
  84. """
  85. self.processor = C.vision.processors.Normalize(
  86. mean, std, is_scale, min, max, swap_rb
  87. )
  88. class PadToSize(Processor):
  89. def __init__(self, width, height, value=[]):
  90. """Create a new PadToSize opereator with given parameters.
  91. :param width: Desired width of the output image
  92. :param height: Desired height of the output image
  93. :param value: Values to pad with
  94. """
  95. self.processor = C.vision.processors.PadToSize(width, height, value)
  96. class Resize(Processor):
  97. def __init__(
  98. self, width, height, scale_w=-1.0, scale_h=-1.0, interp=1, use_scale=False
  99. ):
  100. """Create a Resize operation with the given parameters.
  101. :param width: Desired width of the output image
  102. :param height: Desired height of the output image
  103. :param scale_w: Scales the width in x-direction
  104. :param scale_h: Scales the height in y-direction
  105. :param interp: Optionally, the interpolation mode for resizing image
  106. :param use_scale: Optionally, whether to scale image
  107. """
  108. self.processor = C.vision.processors.Resize(
  109. width, height, scale_w, scale_h, interp, use_scale
  110. )
  111. class StridePad(Processor):
  112. def __init__(self, stride, value=[]):
  113. """Create a StridePad processor with given parameters.
  114. :param stride: Stride of the processor
  115. :param value: Values to pad with
  116. """
  117. self.processor = C.vision.processors.StridePad(stride, value)