processors.py 4.1 KB

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