Tensorflow image reading & display

前端 未结 8 1174
轮回少年
轮回少年 2020-11-30 21:41

I\'ve got a bunch of images in a format similar to Cifar10 (binary file, size = 96*96*3 bytes per image), one image after another (STL-10 dataset). The file I\'

8条回答
  •  天命终不由人
    2020-11-30 21:54

    (Can't comment, not enough reputation, but here is a modified version that worked for me)

    To @HamedMP error about the No default session is registered you can use InteractiveSession to get rid of this error: https://www.tensorflow.org/versions/r0.8/api_docs/python/client.html#InteractiveSession

    And to @NumesSanguis issue with Image.show, you can use the regular PIL .show() method because fromarray returns an image object.

    I do both below (note I'm using JPEG instead of PNG):

    import tensorflow as tf
    import numpy as np
    from PIL import Image
    
    filename_queue = tf.train.string_input_producer(['my_img.jpg']) #  list of files to read
    
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    
    my_img = tf.image.decode_jpeg(value) # use png or jpg decoder based on your files.
    
    init_op = tf.initialize_all_variables()
    sess = tf.InteractiveSession()
    with sess.as_default():
        sess.run(init_op)
    
    # Start populating the filename queue.
    
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    
    for i in range(1): #length of your filename list
      image = my_img.eval() #here is your image Tensor :) 
    
    Image.fromarray(np.asarray(image)).show()
    
    coord.request_stop()
    coord.join(threads)
    

提交回复
热议问题