【问题标题】:Tensorflow v0.12 image not displying after resized调整大小后不显示 Tensorflow v0.12 图像
【发布时间】:2017-03-18 04:48:44
【问题描述】:

我设法使用以下代码显示图像:

import os
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
import numpy as np
import glob
import fnmatch
import matplotlib.pyplot as plt
from PIL import Image

def test1(path):

    filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once(path))

    image_reader = tf.WholeFileReader()

    label, image_file = image_reader.read(filename_queue)

    image = tf.image.decode_jpeg(image_file,3)

    print(image)    


    with tf.Session() as sess:

        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for i in range(1):
            img = image.eval()      

        Image.fromarray(np.asarray(img)).show()


        coord.request_stop()
        coord.join(threads)

    sess.close()

if __name__== "__main__":

    test1("./data/test1/1099.jpg")

但是,当我使用以下代码调整图像大小时:

import os
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
import numpy as np
import glob
import fnmatch
import matplotlib.pyplot as plt
from PIL import Image

def test1(path):

    filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once(path))

    image_reader = tf.WholeFileReader()

    label, image_file = image_reader.read(filename_queue)

    image = tf.image.decode_jpeg(image_file,3)

    print(image)    

    image = tf.image.resize_images(image, [224 , 224])

    print(image)

    with tf.Session() as sess:

        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for i in range(1):
            img = image.eval()      

        Image.fromarray(np.asarray(img)).show()


        coord.request_stop()
        coord.join(threads)

    sess.close()

if __name__== "__main__":

    test1("./data/test1/1099.jpg")

它给出以下错误信息:

raise TypeError("无法处理此数据类型") TypeError: 无法处理此数据类型

谢谢

【问题讨论】:

  • 那么图像 dtype 是什么? print(image.dtype) 或 print(tf.DType(image))
  • @Steven 感谢您回答我的问题。 print(image.dtype) 产生 Tensor("DecodeJpeg:0", shape=(?, ?, 3), dtype=uint8) ,同时 print(tf.DType(image)) 产生 TypeError: int() 参数必须是字符串或数字,而不是 'Tensor'
  • 很高兴你把它修好了。您应该接受您的回答,以便关闭问题。

标签: python image tensorflow


【解决方案1】:

设法修复它

import os
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
import numpy as np
import glob
import fnmatch
import matplotlib.pyplot as plt
from PIL import Image

def test1(path):

    filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once(path))

    image_reader = tf.WholeFileReader()

    label, image_file = image_reader.read(filename_queue)

    image = tf.image.decode_jpeg(image_file,3)

    print(image)    

    image = tf.image.resize_images(image, [224 , 224])

    print(image)

    with tf.Session() as sess:

        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for i in range(1):
            img = image.eval()      

        Image.fromarray(np.asarray(img.astype(np.uint8))).show()


        coord.request_stop()
        coord.join(threads)

    sess.close()

if __name__== "__main__":

    test1("./data/test1/1099.jpg")

谢谢大家 :)

【讨论】:

    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2011-06-16
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多