File size: 678 Bytes
b6668e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np
import requests
import tensorflow as tf

_UINT8_MAX_F = float(np.iinfo(np.uint8).max)


def load_image(img_url: str):
    """Returns an image with shape [height, width, num_channels], with pixels in [0..1] range, and type np.float32."""

    if img_url.startswith("https"):
        user_agent = {'User-agent': 'Colab Sample (https://tensorflow.org)'}
        response = requests.get(img_url, headers=user_agent)
        image_data = response.content
    else:
        image_data = tf.io.read_file(img_url)

    image = tf.io.decode_image(image_data, channels=3)
    image_numpy = tf.cast(image, dtype=tf.float32).numpy()
    return image_numpy / _UINT8_MAX_F