Load an image from file or url. Added or updated keys are "filename", "img", "img_shape", "ori_shape" (same as `img_shape`), "pad_shape" (same as `img_shape`), "scale_factor" (1.0) and "img_norm_cfg" (means=0 and stds=1). Args: mode (str): See :ref:`PIL.Mode<https://pillow.re
| 19 | |
| 20 | @PREPROCESSORS.register_module(Fields.cv, Preprocessors.load_image) |
| 21 | class LoadImage: |
| 22 | """Load an image from file or url. |
| 23 | Added or updated keys are "filename", "img", "img_shape", |
| 24 | "ori_shape" (same as `img_shape`), "pad_shape" (same as `img_shape`), |
| 25 | "scale_factor" (1.0) and "img_norm_cfg" (means=0 and stds=1). |
| 26 | Args: |
| 27 | mode (str): See :ref:`PIL.Mode<https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes>`. |
| 28 | backend (str): Type of loading image. Should be: cv2 or pillow. Default is pillow. |
| 29 | """ |
| 30 | |
| 31 | def __init__(self, mode='rgb', backend='pillow'): |
| 32 | self.mode = mode.upper() |
| 33 | self.backend = backend |
| 34 | |
| 35 | def __call__(self, input: Union[str, Dict[str, str]]): |
| 36 | """Call functions to load image and get image meta information. |
| 37 | Args: |
| 38 | input (str or dict): input image path or input dict with |
| 39 | a key `filename`. |
| 40 | Returns: |
| 41 | dict: The dict contains loaded image. |
| 42 | """ |
| 43 | if isinstance(input, dict): |
| 44 | image_path_or_url = input['filename'] |
| 45 | else: |
| 46 | image_path_or_url = input |
| 47 | |
| 48 | if self.backend == 'cv2': |
| 49 | storage = File._get_storage(image_path_or_url) |
| 50 | with storage.as_local_path(image_path_or_url) as img_path: |
| 51 | img = cv2.imread(img_path, cv2.IMREAD_COLOR) |
| 52 | if self.mode == 'RGB': |
| 53 | cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) |
| 54 | img_h, img_w, img_c = img.shape[0], img.shape[1], img.shape[2] |
| 55 | img_shape = (img_h, img_w, img_c) |
| 56 | elif self.backend == 'pillow': |
| 57 | bytes = File.read(image_path_or_url) |
| 58 | # TODO @wenmeng.zwm add opencv decode as optional |
| 59 | # we should also look at the input format which is the most commonly |
| 60 | # used in Mind' image related models |
| 61 | with io.BytesIO(bytes) as infile: |
| 62 | img = Image.open(infile) |
| 63 | img = ImageOps.exif_transpose(img) |
| 64 | img = img.convert(self.mode) |
| 65 | img_shape = (img.size[1], img.size[0], 3) |
| 66 | else: |
| 67 | raise TypeError(f'backend should be either cv2 or pillow,' |
| 68 | f'but got {self.backend}') |
| 69 | |
| 70 | results = { |
| 71 | 'filename': image_path_or_url, |
| 72 | 'img': img, |
| 73 | 'img_shape': img_shape, |
| 74 | 'img_field': 'img', |
| 75 | } |
| 76 | if isinstance(input, dict): |
| 77 | input_ret = input.copy() |
| 78 | input_ret.update(results) |
no outgoing calls
no test coverage detected
searching dependent graphs…