-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4_image_resize_queue.py
60 lines (44 loc) · 1.78 KB
/
m4_image_resize_queue.py
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
50
51
52
53
54
55
56
57
58
59
60
import tensorflow as tf
from PIL import Image
original_image_list = [
'./image1.jpg',
'./image2.jpg',
'./image3.jpg',
'./image4.jpg'
]
# Make a que of file names including all the images specified
filename_queue = tf.train.string_input_producer(original_image_list)
# Read an entire image file
image_reader = tf.WholeFileReader()
with tf.Session() as sess:
# Coordinate the loading of image files
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
image_list = []
for i in range(len(original_image_list)):
# Read the whole file from the queue, the 1st returned value in the tuple is the
# filename which we are ignoring
_, image_file = image_reader.read(filename_queue)
# Decode the image as a JPEG file, this will turn it into a Tesnor which we can
# then use in training.
image = tf.image.decode_jpeg(image_file)
# Get a tensor of resized images.
image = tf.image.resize_images(image, [224, 224])
image.set_shape((224, 224, 3))
# Get an image tensor and print its value
image_array = sess.run(image)
print(image_array.shape)
Image.fromarray(image_array.astype('uint8'), 'RGB').show()
# The expand dims adds a new dimension
image_list.append(tf.expand_dims(image_array, 0))
# Finish off the filename queue coordinator
coord.request_stop()
coord.join(threads)
index = 0
# Write image summary
summary_writer = tf.summary.FileWriter('./m4_example2', graph=sess.graph)
for image_tensor in image_list:
summary_str = sess.run(tf.summary.image('image-' + str(index), image_tensor))
summary_writer.add_summary(summary_str)
index += 1
summary_writer.close()