File size: 1,015 Bytes
aab9871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def img_scaler(image, max_dim = 512):

  #Casts tensor to a new data type
  original_shape = tf.cast(tf.shape(image)[:-1], tf.float32)

  #Creates scale constant for the image based on imput max_dim
  scale_ratio = max_dim / max(original_shape)

  #Casts tensor to a new data type
  new_shape = tf.cast(original_shape * scale_ratio, tf.int32)

  #Resizes image
  return tf.image.resize(image, new_shape)

def load_img(image_path, max_dim = 512):

  #Read contents of the input filename
  img = tf.io.read_file(image_path)

  #Detect whether an image is a BMP, GIF, JPEG, or PNG, 
  #performs the appropriate operation
  #convert the input bytes string into a Tensor of type dtype 
  img = tf.image.decode_image(img, channels=3)

  #Convert image to dtype
  img = tf.image.convert_image_dtype(img, tf.float32)

  #Scale the image using the created scaler function
  img = img_scaler(img, max_dim)

  #Adds a fourth dimension to the Tensor because the model requires a 4-dimensional Tensor
  return img[tf.newaxis, :]