backstopmedia / tensorflowbook

457 stars 296 forks source link

H Image - Chapter 5, Page 175 #24

Closed michael-GitHub-0 closed 7 years ago

michael-GitHub-0 commented 7 years ago

I have copied the H image on page 175 to my computer (right click, "Save Image As") but when I run the code, Python just runs forever, and I can't interrupt it. I have the latest version of TensorFlow. Am I doing something wrong?

import tensorflow as tf

image_filename = "my_dir/H.jpg" # Location of the H image on my computer. filename_queue = tf.train.string_input_producer([image_filename])

imagereader = tf.WholeFileReader() , image_file = image_reader.read(filename_queue) image = tf.image.decode_jpeg(image_file)

sess = tf.Session()

sess.run(image)

danijar commented 7 years ago

You need to start the queue runners for filename_queue to get populated. Here are some examples: https://www.tensorflow.org/programmers_guide/threading_and_queues

michael-GitHub-0 commented 7 years ago

Thanks for your answer!! I used BGPark's code and got it to work. Sorry, I am new to Python (and GitHub) so I struggle a bit with the coding. I also played around a bit in case anyone is interested for themselves:

# Create H image.
import numpy as np
h=np.array([[[  0,   0,   0],
        [255, 255, 255],
        [254,   0,   0]],

       [[  0, 191,   0],
        [  3, 108, 233],
        [  0, 191,   0]],

       [[254,   0,   0],
        [255, 255, 255],
        [  0,   0,   0]]], dtype='uint8')
h.shape

# Display created H image.
from matplotlib import pyplot as plt
plt.imshow(h, interpolation=None)
plt.show()

# Export H image as a jpg.
import matplotlib
matplotlib.image.imsave('my_dir/H.jpg', h)

# Import H image into TensorFlow.
import tensorflow as tf

image_filename = "my_dir/H.jpg"
filename_queue = tf.train.string_input_producer([image_filename])

image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
image = tf.image.decode_jpeg(image_file)

sess = tf.Session()

sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord, sess=sess)

a = sess.run(image)
a.shape

# Display imported H image.
from matplotlib import pyplot as plt
plt.imshow(a, interpolation=None)
plt.show()