qshng
qshng

Reputation: 887

how to read image into tensor from url directly

I'm new to tensorflow, so please bear with me.

I want to convert the read_tensor_from_image_file function from tensorflow tutorial to be able to read a image from url.

So instead of passing in the file location as file_name, I can pass in a url as file_name such as https://sample-videos.com/img/Sample-jpg-image-500kb.jpg, and do the same resizing and converts it to an nparray of the same dimension. I'm hoping to do this without having to first save the image locally if possible.

But I'm not sure how to read the image into the format that the tf.image.decode_jpeg expects.

I have tried to read the image into bytes and pass it to tf.read_file and tf.image but can't get it to work.

response = requests.get(url)
img = Image.open(BytesIO(response.content))
file_reader = tf.read_file(file_name, input_name)

Any help is appreciated.

def read_tensor_from_image_file(
        file_name, input_height=299, input_width=299, input_mean=0,
        input_std=255
):
    input_name = "file_reader"
    file_reader = tf.read_file(file_name, input_name)
    if file_name.endswith(".png"):
        image_reader = tf.image.decode_png(
            file_reader, channels=3, name="png_reader")
    elif file_name.endswith(".gif"):
        image_reader = tf.squeeze(
            tf.image.decode_gif(file_reader, name="gif_reader"))
    elif file_name.endswith(".bmp"):
        image_reader = tf.image.decode_bmp(file_reader, name="bmp_reader")
    else:
        image_reader = tf.image.decode_jpeg(
            file_reader, channels=3, name="jpeg_reader")
    float_caster = tf.cast(image_reader, tf.float32)
    dims_expander = tf.expand_dims(float_caster, 0)
    resized = tf.image.resize_bilinear(dims_expander,
                                       [input_height, input_width])
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
    sess = tf.Session()
    result = sess.run(normalized)

    return result

Upvotes: 1

Views: 4361

Answers (1)

Y. Luo
Y. Luo

Reputation: 5732

You can use response.content directly with tf.image.decode_jpeg. The following code proves that you will get the same array:

import numpy as np
import requests
import tensorflow as tf


def read_tensor_from_image_url(url,
                               input_height=299,
                               input_width=299,
                               input_mean=0,
                               input_std=255):
    image_reader = tf.image.decode_jpeg(
        requests.get(url).content, channels=3, name="jpeg_reader")
    float_caster = tf.cast(image_reader, tf.float32)
    dims_expander = tf.expand_dims(float_caster, 0)
    resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])

    with tf.Session() as sess:
        return sess.run(normalized)


def read_tensor_from_image_file(file_name,
                                input_height=299,
                                input_width=299,
                                input_mean=0,
                                input_std=255):
    input_name = "file_reader"
    file_reader = tf.read_file(file_name, input_name)
    image_reader = tf.image.decode_jpeg(
        file_reader, channels=3, name="jpeg_reader")
    float_caster = tf.cast(image_reader, tf.float32)
    dims_expander = tf.expand_dims(float_caster, 0)
    resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])

    with tf.Session() as sess:
        return sess.run(normalized)


local_img = read_tensor_from_image_file(
    r'Sample-jpg-image-500kb.jpg'
)
online_img = read_tensor_from_image_url(
    r'https://sample-videos.com/img/Sample-jpg-image-500kb.jpg'
)
print(np.all(local_img == online_img))

Upvotes: 2

Related Questions