File size: 1,533 Bytes
b5cde6e
 
 
aab9871
 
 
 
 
 
 
 
 
 
 
 
 
 
dd061e8
 
 
4f449fa
dd061e8
aab9871
dd061e8
 
aab9871
dd061e8
 
aab9871
dd061e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import tensorflow as tf
from tensorflow import keras

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, content=True, max_dim = 512):

  if content:
    #content images come straight from the web app, so no opening or decoding
    img = image_path

    #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, :]
    
  else:
    #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, :]