1.读取文件列表
#文件保存目录
path='./data/iamge'
file_names=os.listdir(path)
file_list=[os.path.join(path,file_name)for file_name in file_names]
2.创建文件读取队列
file_queue=tf.train.string_inpurt_producer(file_list)
3.创建图片阅读器读取图片
reader=tf.WholeFileReader()
key,value=reader.read(file_queue)
4.解析图片
image=tf.image.decode_jpeg(value)
5.设置图片大小,固定图片通道
image_resize=tf.image.image_resize(image,[200,200])
image_resize.set_shape([200,200,3])
6.批处理
tf.train.batch([image_resize],batch_size=20,num_threads=2,capacity=20)
7.开启会话处理
with tf.Session() as sess:
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess,coord=coord)
print(sess.run(batch_image))
coord.request_stop()
coord.join()
完整代码
import tensorflow as tf
import os
def Image_reader(image_list):
# 读取文件导队列
image_queue = tf.train.string_input_producer(image_list)
# 构建文件阅读器,使用tf.WholeFileReader()api
reader = tf.WholeFileReader()
key, value = reader.read(image_queue)
# 解码
image = tf.image.decode_jpeg(value)
# 处理图片大小
image_resize = tf.image.resize_images(image, [200, 200])
# 固定图片矩阵大小
image_resize.set_shape([200, 200, 3])
# 进行批处理
batch_image = tf.train.batch([image_resize], batch_size=20, num_threads=4, capacity=20)
return batch_image
if __name__ == '__main__':
# 读取文件
path = './data/image'
file_names = os.listdir(path)
file_list = [os.path.join(path, filename) for filename in file_names]
batch_image = Image_reader(file_list)
# 开启会话
with tf.Session() as sess:
# 开启线程协调器
coord = tf.train.Coordinator()
# 开启线程进行处理
threads = tf.train.start_queue_runners(sess, coord=coord)
print(sess.run(batch_image))
# 停止线程
coord.request_stop()
coord.join(threads)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。