Tensorflow image reading & display

前端 未结 8 1137
轮回少年
轮回少年 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

    Just to give a complete answer:

    filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) #  list of files to read
    
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    
    my_img = tf.image.decode_png(value) # use png or jpg decoder based on your files.
    
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
      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 :) 
    
      print(image.shape)
      Image.fromarray(np.asarray(image)).show()
    
      coord.request_stop()
      coord.join(threads)
    

    Or if you have a directory of images you can add them all via this Github source file

    @mttk and @salvador-dali: I hope it is what you need

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-30 21:54

    You can use tf.keras API.

    import tensorflow as tf
    import numpy as np
    from tensorflow.keras.preprocessing.image import load_img, array_to_img
    
    tf.enable_eager_execution()
    
    img = load_img("example.png")
    img = tf.convert_to_tensor(np.asarray(img))
    image = tf.image.resize_images(img, (800, 800))
    to_img = array_to_img(image)
    to_img.show()
    
    0 讨论(0)
  • 2020-11-30 22:05

    According to the documentation you can decode JPEG/PNG images.

    It should be something like this:

    import tensorflow as tf
    
    filenames = ['/image_dir/img.jpg']
    filename_queue = tf.train.string_input_producer(filenames)
    
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    
    images = tf.image.decode_jpeg(value, channels=3)
    

    You can find a bit more info here

    0 讨论(0)
  • 2020-11-30 22:06

    Load names with tf.train.match_filenames_once get the number of files to iterate over with tf.size open session and enjoy ;-)

    import tensorflow as tf
    import numpy as np
    import matplotlib;
    from PIL import Image
    
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    
    
    filenames = tf.train.match_filenames_once('./images/*.jpg')
    count_num_files = tf.size(filenames)
    filename_queue = tf.train.string_input_producer(filenames)
    
    reader=tf.WholeFileReader()
    key,value=reader.read(filename_queue)
    img = tf.image.decode_jpeg(value)
    
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        num_files = sess.run(count_num_files)
        for i in range(num_files):
            image=img.eval()
            print(image.shape)
            Image.fromarray(np.asarray(image)).save('te.jpeg')
    
    0 讨论(0)
  • 2020-11-30 22:08

    I used CIFAR10 format instead of STL10 and code came out like

    filename_queue = tf.train.string_input_producer(filenames)
    read_input = read_cifar10(filename_queue)
    with tf.Session() as sess:       
        tf.train.start_queue_runners(sess=sess)
        result = sess.run(read_input.uint8image)        
    img = Image.fromarray(result, "RGB")    
    img.save('my.jpg')
    

    The snippet is identical with mttk and Rosa Gronchi, but Somehow I wasn't able to show the image during run-time, so I saved as the JPG file.

    0 讨论(0)
提交回复
热议问题