processors.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 typing import List, Tuple, Union
  15. import numpy as np
  16. from ...utils.benchmark import benchmark
  17. @benchmark.timeit
  18. class DocTrPostProcess:
  19. """
  20. Post-processing class for cropping regions from images (though currently only performs scaling and color channel adjustments).
  21. Attributes:
  22. scale (np.float32): A scaling factor to be applied to the image pixel values.
  23. Defaults to 255.0 if not provided.
  24. Methods:
  25. __call__(imgs: List[Union[np.ndarray, Tuple[np.ndarray, ...]]]) -> List[np.ndarray]:
  26. Call method to process a list of images.
  27. doctr(pred: Union[np.ndarray, Tuple[np.ndarray, ...]]) -> np.ndarray:
  28. Method to process a single image or a tuple/list containing an image.
  29. """
  30. def __init__(self, scale: Union[str, float, None] = None):
  31. """
  32. Initializes the DocTrPostProcess class with a scaling factor.
  33. Args:
  34. scale (Union[str, float, None]): A scaling factor for the image pixel values.
  35. If a string is provided, it will be converted to a float. Defaults to 255.0.
  36. """
  37. super().__init__()
  38. self.scale = (
  39. np.float32(scale) if isinstance(scale, (str, float)) else np.float32(255.0)
  40. )
  41. def __call__(
  42. self, imgs: List[Union[np.ndarray, Tuple[np.ndarray, ...]]]
  43. ) -> List[np.ndarray]:
  44. """
  45. Processes a list of images using the `doctr` method.
  46. Args:
  47. imgs (List[Union[np.ndarray, Tuple[np.ndarray, ...]]]): A list of images to process.
  48. Each image can be a numpy array or a tuple containing a numpy array.
  49. Returns:
  50. List[np.ndarray]: A list of processed images.
  51. """
  52. return [self.doctr(img) for img in imgs]
  53. def doctr(self, pred: Union[np.ndarray, Tuple[np.ndarray, ...]]) -> np.ndarray:
  54. """
  55. Processes a single image.
  56. Args:
  57. pred (Union[np.ndarray, Tuple[np.ndarray, ...]]): The image to process, which can be
  58. a numpy array or a tuple containing a numpy array. Only the first element is used if it's a tuple.
  59. Returns:
  60. np.ndarray: The processed image.
  61. Raises:
  62. AssertionError: If the input is not a numpy array.
  63. """
  64. if isinstance(pred, tuple):
  65. im = pred[0]
  66. else:
  67. im = pred
  68. assert isinstance(
  69. im, np.ndarray
  70. ), "Invalid input 'im' in DocTrPostProcess. Expected a numpy array."
  71. im = im.squeeze()
  72. im = im.transpose(1, 2, 0)
  73. im *= self.scale
  74. im = im[:, :, ::-1]
  75. im = im.astype("uint8", copy=False)
  76. return im