1.读取文件列表api
#文件保存目录 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.建立图片阅读器读取图片code
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.批处理ci
tf.train.batch([image_resize],batch_size=20,num_threads=2,capacity=20)
7.开启会话处理input
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()
完整代码string
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)