processors.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import numpy as np
  15. from typing import List, Union, Tuple
  16. from ...utils.benchmark import benchmark
  17. class DocTrPostProcess:
  18. """
  19. Post-processing class for cropping regions from images (though currently only performs scaling and color channel adjustments).
  20. Attributes:
  21. scale (np.float32): A scaling factor to be applied to the image pixel values.
  22. Defaults to 255.0 if not provided.
  23. Methods:
  24. __call__(imgs: List[Union[np.ndarray, Tuple[np.ndarray, ...]]]) -> List[np.ndarray]:
  25. Call method to process a list of images.
  26. doctr(pred: Union[np.ndarray, Tuple[np.ndarray, ...]]) -> np.ndarray:
  27. Method to process a single image or a tuple/list containing an image.
  28. """
  29. def __init__(self, scale: Union[str, float, None] = None):
  30. """
  31. Initializes the DocTrPostProcess class with a scaling factor.
  32. Args:
  33. scale (Union[str, float, None]): A scaling factor for the image pixel values.
  34. If a string is provided, it will be converted to a float. Defaults to 255.0.
  35. """
  36. super().__init__()
  37. self.scale = (
  38. np.float32(scale) if isinstance(scale, (str, float)) else np.float32(255.0)
  39. )
  40. @benchmark.timeit
  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