tensorflow之读取jpg图像保存为tfrecord再读取

有了jpg读取的经验和tfrecord写入的经验之后,开始尝试把jpg图像写入到tfrecord,另外还想尝试从tfrecord文件读出jpg图像

上示例把jpg的图片的二进制以及长和宽的信息保存进tfrecord

decode_jpeg_data = tf.placeholder(dtype=tf.string)
decode_jpeg = tf.image.decode_jpeg(decode_jpeg_data, channels=3)

tfrecords_filename = './tfrecords'
writer = tf.python_io.TFRecordWriter(tfrecords_filename) # 创建.tfrecord文件,准备写入

image_data = tf.gfile.FastGFile("C:/Users/shenwei/Desktop/timg.jpg", 'rb').read()
print(type(image_data))
with tf.Session() as sess:
    image = sess.run(decode_jpeg,feed_dict={decode_jpeg_data: image_data})
    print(image.shape[0])
    print(image.shape[1])
    example = tf.train.Example(features=tf.train.Features(
            feature={
            'encoded': tf.train.Feature(bytes_list = tf.train.BytesList(value=[image_data])) ,
            'height': tf.train.Feature(int64_list = tf.train.Int64List(value = [image.shape[0]])),
            'width': tf.train.Feature(int64_list = tf.train.Int64List(value = [image.shape[1]])),
            }))
    writer.write(example.SerializeToString()) 
    writer.close()

猜你喜欢

转载自blog.csdn.net/g0415shenw/article/details/86489883
今日推荐