You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

image.py 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import io
  3. from typing import Dict, Union
  4. from PIL import Image, ImageOps
  5. from maas_lib.fileio import File
  6. from maas_lib.utils.constant import Fields
  7. from .builder import PREPROCESSORS
  8. @PREPROCESSORS.register_module(Fields.image)
  9. class LoadImage:
  10. """Load an image from file or url.
  11. Added or updated keys are "filename", "img", "img_shape",
  12. "ori_shape" (same as `img_shape`), "pad_shape" (same as `img_shape`),
  13. "scale_factor" (1.0) and "img_norm_cfg" (means=0 and stds=1).
  14. Args:
  15. mode (str): See :ref:`PIL.Mode<https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes>`.
  16. to_float32 (bool): Whether to convert the loaded image to a float32
  17. numpy array. If set to False, the loaded image is an uint8 array.
  18. Defaults to False.
  19. """
  20. def __init__(self, mode='rgb'):
  21. self.mode = mode.upper()
  22. def __call__(self, input: Union[str, Dict[str, str]]):
  23. """Call functions to load image and get image meta information.
  24. Args:
  25. input (str or dict): input image path or input dict with
  26. a key `filename`.
  27. Returns:
  28. dict: The dict contains loaded image.
  29. """
  30. if isinstance(input, dict):
  31. image_path_or_url = input['filename']
  32. else:
  33. image_path_or_url = input
  34. bytes = File.read(image_path_or_url)
  35. # TODO @wenmeng.zwm add opencv decode as optional
  36. # we should also look at the input format which is the most commonly
  37. # used in Mind' image related models
  38. with io.BytesIO(bytes) as infile:
  39. img = Image.open(infile)
  40. img = ImageOps.exif_transpose(img)
  41. img = img.convert(self.mode)
  42. results = {
  43. 'filename': image_path_or_url,
  44. 'img': img,
  45. 'img_shape': (img.size[1], img.size[0], 3),
  46. 'img_field': 'img',
  47. }
  48. return results
  49. def __repr__(self):
  50. repr_str = (f'{self.__class__.__name__}(' f'mode={self.mode})')
  51. return repr_str
  52. def load_image(image_path_or_url: str) -> Image.Image:
  53. """ simple interface to load an image from file or url
  54. Args:
  55. image_path_or_url (str): image file path or http url
  56. """
  57. loader = LoadImage()
  58. return loader(image_path_or_url)['img']

致力于通过开放的社区合作,开源AI模型以及相关创新技术,推动基于模型即服务的生态繁荣发展