# 导入模块
import os
import tensorflow as tf
import time
import numpy as np
# 获取文件完整路径
start=time.time()
filename = os.listdir(r"F:/data_dog/dog")
print(filename)
filelist = [os.path.join(r"F:/data_dog/dog/", file) for file in filename]
print(filelist)
# 构造文件名队列
filequeue = tf.train.string_input_producer(filelist)
print("filequeue:\n",filequeue.size())
# 构造文件读取器
reader = tf.WholeFileReader()
# 读取数据
key, value = reader.read(filequeue)
print(key)
print(value)
# 对value进行解码
image = tf.image.decode_jpeg(value)
print("image:\n",image.shape)
# 统一形状
image_reshape = tf.image.resize_images(image, [200, 200])
print("image_reshape:\n", image_reshape)
image_reshape.set_shape([200,200,3])
print("image_reshape:\n",image_reshape)
# 修改数据类型dtype
image_reshape_newdata = tf.cast(image_reshape, tf.float32)
print("image_reshape_newdata:\n",image_reshape_newdata)
# 批处理
image_batch=tf.train.batch([image_reshape_newdata],batch_size=50,capacity=100)
print("image_batch:\n",image_batch)
# 开启会话
with tf.Session() as sess:
# 创建线程协调员
coord = tf.train.Coordinator()
# 创建队列的线程
threads = tf.train.start_queue_runners(sess, coord)
image_value = sess.run(image)
print("image_value:\n", image_value)
image_reshape_value = sess.run(image_reshape)
print("image_reshape_value:\n", image_reshape_value)
image_batch_value = sess.run(image_batch)
print("image_batch_value:\n", image_batch_value)
coord.request_stop()
coord.join(threads)
end=time.time()
print('时长:\n',end-start)
深度学习-读取图片代码(tensorflow1)
最新推荐文章于 2023-09-05 19:37:00 发布