import tensorflow as tf
import os
"""
黑白照:单通道,灰度值[0~255],一个像素点一个值
彩色照:三通道,RGB,一个像素点三个值,同样像素,彩色是黑白数据量的3倍
图片的张量表示:[100, 300, 1],100表示长,300表示宽,1表示通道数
图形需要进行缩放:一方面减小开销,一方面是为了统一特征值的数量
"""
def picread(filelist):
"""图片读取"""
# 1.构造文件队列
file_queue = tf.train.string_input_producer(filelist)
# 2.构造阅读器读取图片内容(默认读取一张图片)
reader = tf.WholeFileReader()
key, value = reader.read(file_queue)
# print(value) # 返回Tensor("ReaderReadV2:1", shape=(), dtype=string)
# 3.对读取的图片数据进行解码,返回的image是uint8类型
image = tf.image.decode_jpeg(value)
# 处理图片,统一图片的大小,image_resize是float类型,shape=[200, 200, ?]即形状还没有固定
image_resize = tf.image.resize_images(image, [200, 200])
# 固定图片形状
image_resize.set_shape([200, 200, 3])
# 4.进行批处理
image_batch = tf.train.batch([image_resize], batch_size=3, num_threads=1, capacity=3)
return image_batch
if __name__ == "__main__":
# 构造文件列表
file_name = os.listdir("./data/dog")
filelist = [os.path.join("./data/dog", file) for file in file_name]
image_batch = picread(filelist)
# 开启会话
with tf.Session() as sess:
# 定义线程协调器
coord = tf.train.Coordinator()
# 开启读取文件的线程
thd = tf.train.start_queue_runners(sess, coord=coord, start=True)
# 打印读取内容
print(sess.run([image_batch]))
# 回收子线程
coord.request_stop()
coord.join(thd)