Preprocess the image input. Args: image (`pipeline_image_input`): The image input, accepted formats are PIL images, NumPy arrays, PyTorch tensors; Also accept list of supported formats. height (`int`, *optional*, defaults to `
(
self,
image: PipelineImageInput,
height: Optional[int] = None,
width: Optional[int] = None,
resize_mode: str = "default", # "default", "fill", "crop"
crops_coords: Optional[Tuple[int, int, int, int]] = None,
)
| 467 | return height, width |
| 468 | |
| 469 | def preprocess( |
| 470 | self, |
| 471 | image: PipelineImageInput, |
| 472 | height: Optional[int] = None, |
| 473 | width: Optional[int] = None, |
| 474 | resize_mode: str = "default", # "default", "fill", "crop" |
| 475 | crops_coords: Optional[Tuple[int, int, int, int]] = None, |
| 476 | ) -> torch.Tensor: |
| 477 | """ |
| 478 | Preprocess the image input. |
| 479 | |
| 480 | Args: |
| 481 | image (`pipeline_image_input`): |
| 482 | The image input, accepted formats are PIL images, NumPy arrays, PyTorch tensors; Also accept list of |
| 483 | supported formats. |
| 484 | height (`int`, *optional*, defaults to `None`): |
| 485 | The height in preprocessed image. If `None`, will use the `get_default_height_width()` to get default |
| 486 | height. |
| 487 | width (`int`, *optional*`, defaults to `None`): |
| 488 | The width in preprocessed. If `None`, will use get_default_height_width()` to get the default width. |
| 489 | resize_mode (`str`, *optional*, defaults to `default`): |
| 490 | The resize mode, can be one of `default` or `fill`. If `default`, will resize the image to fit within |
| 491 | the specified width and height, and it may not maintaining the original aspect ratio. If `fill`, will |
| 492 | resize the image to fit within the specified width and height, maintaining the aspect ratio, and then |
| 493 | center the image within the dimensions, filling empty with data from image. If `crop`, will resize the |
| 494 | image to fit within the specified width and height, maintaining the aspect ratio, and then center the |
| 495 | image within the dimensions, cropping the excess. Note that resize_mode `fill` and `crop` are only |
| 496 | supported for PIL image input. |
| 497 | crops_coords (`List[Tuple[int, int, int, int]]`, *optional*, defaults to `None`): |
| 498 | The crop coordinates for each image in the batch. If `None`, will not crop the image. |
| 499 | """ |
| 500 | supported_formats = (PIL.Image.Image, np.ndarray, torch.Tensor) |
| 501 | |
| 502 | # Expand the missing dimension for 3-dimensional pytorch tensor or numpy array that represents grayscale image |
| 503 | if self.config.do_convert_grayscale and isinstance(image, (torch.Tensor, np.ndarray)) and image.ndim == 3: |
| 504 | if isinstance(image, torch.Tensor): |
| 505 | # if image is a pytorch tensor could have 2 possible shapes: |
| 506 | # 1. batch x height x width: we should insert the channel dimension at position 1 |
| 507 | # 2. channel x height x width: we should insert batch dimension at position 0, |
| 508 | # however, since both channel and batch dimension has same size 1, it is same to insert at position 1 |
| 509 | # for simplicity, we insert a dimension of size 1 at position 1 for both cases |
| 510 | image = image.unsqueeze(1) |
| 511 | else: |
| 512 | # if it is a numpy array, it could have 2 possible shapes: |
| 513 | # 1. batch x height x width: insert channel dimension on last position |
| 514 | # 2. height x width x channel: insert batch dimension on first position |
| 515 | if image.shape[-1] == 1: |
| 516 | image = np.expand_dims(image, axis=0) |
| 517 | else: |
| 518 | image = np.expand_dims(image, axis=-1) |
| 519 | |
| 520 | if isinstance(image, list) and isinstance(image[0], np.ndarray) and image[0].ndim == 4: |
| 521 | warnings.warn( |
| 522 | "Passing `image` as a list of 4d np.ndarray is deprecated." |
| 523 | "Please concatenate the list along the batch dimension and pass it as a single 4d np.ndarray", |
| 524 | FutureWarning, |
| 525 | ) |
| 526 | image = np.concatenate(image, axis=0) |