processors.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. import math
  15. import numpy as np
  16. from ....utils.deps import class_requires_deps, is_dep_available
  17. from ...utils.benchmark import benchmark
  18. from ..common.vision import funcs as F
  19. from ..common.vision.processors import _BaseResize
  20. if is_dep_available("opencv-contrib-python"):
  21. import cv2
  22. @benchmark.timeit
  23. class Resize(_BaseResize):
  24. """Resize the image."""
  25. def __init__(
  26. self, target_size=-1, keep_ratio=False, size_divisor=None, interp="LINEAR"
  27. ):
  28. """
  29. Initialize the instance.
  30. Args:
  31. target_size (list|tuple|int, optional): Target width and height. -1 will return the images directly without resizing.
  32. keep_ratio (bool, optional): Whether to keep the aspect ratio of resized
  33. image. Default: False.
  34. size_divisor (int|None, optional): Divisor of resized image size.
  35. Default: None.
  36. interp (str, optional): Interpolation method. Choices are 'NEAREST',
  37. 'LINEAR', 'CUBIC', 'AREA', and 'LANCZOS4'. Default: 'LINEAR'.
  38. """
  39. super().__init__(size_divisor=size_divisor, interp=interp)
  40. if isinstance(target_size, int):
  41. target_size = (target_size, target_size)
  42. F.check_image_size(target_size)
  43. self.target_size = target_size
  44. self.keep_ratio = keep_ratio
  45. def __call__(self, imgs, target_size=None):
  46. """apply"""
  47. target_size = self.target_size if target_size is None else target_size
  48. if isinstance(target_size, int):
  49. target_size = (target_size, target_size)
  50. F.check_image_size(target_size)
  51. return [self.resize(img, target_size) for img in imgs]
  52. def resize(self, img, target_size):
  53. if target_size == (-1, -1):
  54. # If the final target_size == (-1, -1), it means use the source input image directly.
  55. return img
  56. original_size = img.shape[:2][::-1]
  57. assert target_size[0] > 0 and target_size[1] > 0
  58. if self.keep_ratio:
  59. h, w = img.shape[0:2]
  60. target_size, _ = self._rescale_size((w, h), target_size)
  61. if self.size_divisor:
  62. target_size = [
  63. math.ceil(i / self.size_divisor) * self.size_divisor
  64. for i in target_size
  65. ]
  66. img = F.resize(img, target_size, interp=self.interp)
  67. return img
  68. @benchmark.timeit
  69. @class_requires_deps("opencv-contrib-python")
  70. class SegPostProcess:
  71. """Semantic Segmentation PostProcess
  72. This class is responsible for post-processing detection results, only including
  73. restoring the prediction segmentation map to the original image size for now.
  74. """
  75. def __call__(self, imgs, src_images):
  76. assert len(imgs) == len(src_images)
  77. src_sizes = [src_image.shape[:2][::-1] for src_image in src_images]
  78. return [
  79. self.reverse_resize(img, src_size) for img, src_size in zip(imgs, src_sizes)
  80. ]
  81. def reverse_resize(self, img, src_size):
  82. """Restore the prediction map to source image size using nearest interpolation.
  83. Args:
  84. img (np.ndarray): prediction map with shape of (1, width, height)
  85. src_size (Tuple[int, int]): source size of the input image, with format of (width, height).
  86. """
  87. assert isinstance(src_size, (tuple, list)) and len(src_size) == 2
  88. assert src_size[0] > 0 and src_size[1] > 0
  89. assert img.ndim == 3
  90. reversed_img = cv2.resize(
  91. img[0], dsize=src_size, interpolation=cv2.INTER_NEAREST
  92. )
  93. reversed_img = np.expand_dims(reversed_img, axis=0)
  94. return reversed_img