TFrecords读、写图片文件
2024-08-31 21:13:11
参考: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)
最新文章
- TypeScript 素描 - 接口
- 【实战Java高并发程序设计 7】让线程之间互相帮助--SynchronousQueue的实现
- 8.2 使用Fluent API进行实体映射【Code-First系列】
- java 用 jxl poi 进行excel 解析 *** 最爱那水货
- php部分--session的三种用法
- python学习笔记-socket
- SQLSERVER 表名数据库名作为变量 必须使用动态SQL(源自网络)
- Android_layout_note
- [Audio processing] wav音频文件合并
- 异常处理 - PHP手册笔记
- 获取当前url并指定url中的字符 效果
- 019 关联映射文件中集合标签中的lazy(懒加载)属性
- udl文件测试数据库连接
- nodejs01--什么是nodejs,nodejs的基本使用
- 电脑上的安卓系统——PhoenixOS浅度体验
- 20175229张智敏 Arrays和String单元测试
- Windows两个网卡配置路由规则 同时访问内网和外网
- Linux五种IO模型(同步 阻塞概念)
- Kaggle:Home Credit Default Risk 特征工程构建及可视化(2)
- python 搜索引擎Whoosh中文文档和代码 以及jieba的使用
热门文章
- ceph-pve英语
- PHP模拟请求和操作响应
- sqlalchemy批量添加数据-数据源是json(小算法)
- .Net Core 使用 System.Drawing.Common 在CentOS下报错
- Android加载网络图片报android.os.NetworkOnMainThreadException异常
- Good teachers,they inspire you, they entertain you,and you end up learning a ton even when you don't know it.
- python 安装成linux中的systemd守护运行
- [19/05/14-星期二] HTML_body标签(列表标签和图片标签)
- 如何在Web工程中实现任务计划调度
- webpack打包html里的img图片