processors.py 4.0 KB

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