|
| 1 | +# Lint as: python3 |
| 2 | +# Copyright 2021 The TensorFlow Authors All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | +"""Supporting functions for data loading.""" |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +from PIL import Image |
| 20 | + |
| 21 | +import tensorflow as tf |
| 22 | +from delf.python import utils as image_loading_utils |
| 23 | + |
| 24 | + |
| 25 | +def pil_imagenet_loader(path, imsize, bounding_box=None, normalize=True): |
| 26 | + """Pillow loader for the images. |
| 27 | +
|
| 28 | + Args: |
| 29 | + path: Path to image to be loaded. |
| 30 | + imsize: Integer, defines the maximum size of longer image side. |
| 31 | + bounding_box: (x1,y1,x2,y2) tuple to crop the query image. |
| 32 | + normalize: Bool, whether to normalize the image. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + image: `Tensor`, image in ImageNet suitable format. |
| 36 | + """ |
| 37 | + img = image_loading_utils.RgbLoader(path) |
| 38 | + |
| 39 | + if bounding_box is not None: |
| 40 | + imfullsize = max(img.size) |
| 41 | + img = img.crop(bounding_box) |
| 42 | + imsize = imsize * max(img.size) / imfullsize |
| 43 | + |
| 44 | + # Unlike `resize`, `thumbnail` resizes to the largest size that preserves |
| 45 | + # the aspect ratio, making sure that the output image does not exceed the |
| 46 | + # original image size and the size specified in the arguments of thumbnail. |
| 47 | + img.thumbnail((imsize, imsize), Image.ANTIALIAS) |
| 48 | + img = np.array(img) |
| 49 | + |
| 50 | + if normalize: |
| 51 | + # Preprocessing for ImageNet data. Converts the images from RGB to BGR, |
| 52 | + # then zero-centers each color channel with respect to the ImageNet |
| 53 | + # dataset, without scaling. |
| 54 | + tf.keras.applications.imagenet_utils.preprocess_input(img, mode='caffe') |
| 55 | + |
| 56 | + return img |
| 57 | + |
| 58 | + |
| 59 | +def default_loader(path, imsize, bounding_box=None, normalize=True): |
| 60 | + """Default loader for the images is using Pillow. |
| 61 | +
|
| 62 | + Args: |
| 63 | + path: Path to image to be loaded. |
| 64 | + imsize: Integer, defines the maximum size of longer image side. |
| 65 | + bounding_box: (x1,y1,x2,y2) tuple to crop the query image. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + image: `Tensor`, image in ImageNet suitable format. |
| 69 | + """ |
| 70 | + img = pil_imagenet_loader(path, imsize, bounding_box, normalize) |
| 71 | + return img |
0 commit comments