image_reader.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. import cv2
  16. from ...utils.io import ImageReader, PDFReader
  17. from ...utils.benchmark import benchmark
  18. @benchmark.timeit_with_options(name=None, is_read_operation=True)
  19. class ReadImage:
  20. """Load image from the file."""
  21. _FLAGS_DICT = {
  22. "BGR": cv2.IMREAD_COLOR,
  23. "RGB": cv2.IMREAD_COLOR,
  24. "GRAY": cv2.IMREAD_GRAYSCALE,
  25. }
  26. def __init__(self, format="BGR"):
  27. """
  28. Initialize the instance.
  29. Args:
  30. format (str, optional): Target color format to convert the image to.
  31. Choices are 'BGR', 'RGB', and 'GRAY'. Default: 'BGR'.
  32. """
  33. super().__init__()
  34. self.format = format
  35. flags = self._FLAGS_DICT[self.format]
  36. self._img_reader = ImageReader(backend="opencv", flags=flags)
  37. def __call__(self, imgs):
  38. """apply"""
  39. return [self.read(img) for img in imgs]
  40. def read(self, img):
  41. if isinstance(img, np.ndarray):
  42. if self.format == "RGB":
  43. img = img[:, :, ::-1]
  44. return img
  45. elif isinstance(img, str):
  46. blob = self._img_reader.read(img)
  47. if blob is None:
  48. raise Exception(f"Image read Error: {img}")
  49. if self.format == "RGB":
  50. if blob.ndim != 3:
  51. raise RuntimeError("Array is not 3-dimensional.")
  52. # BGR to RGB
  53. blob = blob[..., ::-1]
  54. return blob
  55. else:
  56. raise TypeError(
  57. f"ReadImage only supports the following types:\n"
  58. f"1. str, indicating a image file path or a directory containing image files.\n"
  59. f"2. numpy.ndarray.\n"
  60. f"However, got type: {type(img).__name__}."
  61. )