参考:https://blog.csdn.net/u014802590/article/details/68495238
参考:https://www.2cto.com/kf/201709/680575.html
1、读取图片文件并写为TFRecords文件
import tensorflow as tf from PIL import Image import os file_path='C:/Users/1/Desktop/123/' file_name=os.listdir(file_path) writer=tf.python_io.TFRecordWriter('C:/Users/1/Desktop/fuck.tfrecords') for name in file_name: img_path=file_path+name img=Image.open(img_path) img=img.resize((10,10)) img_raw=img.tobytes() print(index) example=tf.train.Example(features=tf.train.Features(feature={'label':tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),'img_raw':tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))})) imgg=example.SerializeToString() writer.write(imgg) writer.close()
2、读取图片文件对应的tfrecords文件:
import scipy.misc as misc import matplotlib.pyplot as plt filename=tf.train.match_filenames_once('C:/Users/1/Desktop/fuck.*frecords') files=tf.train.string_input_producer(filename,shuffle=False) reader=tf.TFRecordReader() _,seri=reader.read(files) feature=tf.parse_single_example(seri,features={'label':tf.FixedLenFeature([],tf.int64),'img_raw':tf.FixedLenFeature([],tf.string)}) img=feature['img_raw'] img=tf.decode_raw(img,tf.uint8) img=tf.reshape(img,[256,256,3]) img=tf.cast(img,tf.float32) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) coord=tf.train.Coordinator() threads=tf.train.start_queue_runners(sess=sess,coord=coord) img=sess.run(img) plt.imshow(img) plt.show() #plot.show()展示的是黑白图 misc.imsave('C:/Users/1/Desktop/1111.jpg', img) #使用scipy.misc.imsave来保存np.array数组格式的图片,保存好的图片为彩色图片 coord.request_stop() coord.join(threads)