AI笔记-2-tensorflow读取图片的方法

本文介绍了使用TensorFlow进行图片读取的四种方法,包括使用gfile读取、WholeFileReader从队列中读取、使用read_file读取以及TFRecords格式的读取方法,并给出了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文记录一下TensorFLow的几种图片读取方法,官方文档有较为全面的介绍。

1.使用gfile读图片,decode输出是Tensor,eval后是ndarray

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

print(tf.__version__)

image_raw = tf.gfile.FastGFile('test/a.jpg','rb').read()   #bytes
img = tf.image.decode_jpeg(image_raw)  #Tensor
#img2 = tf.image.convert_image_dtype(img, dtype = tf.uint8)

with tf.Session() as sess:
    print(type(image_raw)) # bytes
    print(type(img)) # Tensor
    #print(type(img2))

    print(type(img.eval())) # ndarray !!!
    print(img.eval().shape)
    print(img.eval().dtype)

#    print(type(img2.eval()))
#    print(img2.eval().shape)
#    print(img2.eval().dtype)
    plt.figure(1)
    plt.imshow(img.eval())
    plt.show()
 
  • 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

输出为:

1.3.0
<class 'bytes'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
图片显示(略)
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.使用WholeFileReader输入queue,decode输出是Tensor,eval后是ndarray

import tensorflow as tf
import os
import matplotlib.pyplot as plt

def file_name(file_dir):   #来自http://blog.youkuaiyun.com/lsq2902101015/article/details/51305825
    for root, dirs, files in os.walk(file_dir):  #模块os中的walk()函数遍历文件夹下所有的文件
        print(root) #当前目录路径  
        print(dirs) #当前路径下所有子目录  
        print(files) #当前路径下所有非目录子文件  

def file_name2(file_dir):   #特定类型的文件
    L=[]   
    for root, dirs, files in os.walk(file_dir):  
        for file in files:  
            if os.path.splitext(file)[1] == '.jpg':   
                L.append(os.path.join(root, file))  
    return L 

path = file_name2('test')


#以下参考http://blog.youkuaiyun.com/buptgshengod/article/details/72956846 (十图详解TensorFlow数据读取机制)
#以及http://blog.youkuaiyun.com/uestc_c2_403/article/details/74435286

#path2 = tf.train.match_filenames_once(path)
file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #创建输入队列  
image_reader = tf.WholeFileReader()  
key, image = image_reader.read(file_queue)  
image = tf.image.decode_jpeg(image)  

with tf.Session() as sess:  
#    coord = tf.train.Coordinator() #协同启动的线程  
#    threads = tf.train.start_queue_runners(sess=sess, coord=coord) #启动线程运行队列  
#    coord.request_stop() #停止所有的线程  
#    coord.join(threads)  

    tf.local_variables_initializer().run()
    threads = tf.train.start_queue_runners(sess=sess)

    #print (type(image))  
    #print (type(image.eval()))  
    #print(image.eval().shape)
    for _ in path+path:
        plt.figure
        plt.imshow(image.eval())
        plt.show()
 
  • 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

3.使用read_file,decode输出是Tensor,eval后是ndarray

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

print(tf.__version__)

image_value = tf.read_file('test/a.jpg')
img = tf.image.decode_jpeg(image_value, channels=3)

with tf.Session() as sess:
    print(type(image_value)) # bytes
    print(type(img)) # Tensor
    #print(type(img2))

    print(type(img.eval())) # ndarray !!!
    print(img.eval().shape)
    print(img.eval().dtype)

#    print(type(img2.eval()))
#    print(img2.eval().shape)
#    print(img2.eval().dtype)
    plt.figure(1)
    plt.imshow(img.eval())
    plt.show()
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

输出是:

1.3.0
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
显示图片(略)
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.TFRecords: 
有空再看。 
如果图片是根据分类放在不同的文件夹下,那么可以直接使用如下代码: 
http://blog.youkuaiyun.com/u012759136/article/details/52232266 
https://www.2cto.com/kf/201702/604326.html 
需要稍微整理的话可以参考: 
http://blog.youkuaiyun.com/hjxu2016/article/details/76165559


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值