TFrecords读、写图片文件
参考: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)
TFrecords读、写图片文件的更多相关文章
- C++文件处理(一):读/写txt文件
C++文件处理与C语言不同,C++文件处理使用的是:流(stream) C++头文件fstream定义了三个类型来支持文件IO
- android 读,写图片sd网卡资源
<pre name="code" class="html"><!--<span style="font-family: Ari ...
- Python小实验——读&写Excel文件内容
安装xlrd模块和xlwt模块 读取Excel文件了内容需要额外的模块-- \(xlrd\),在官网上可以找到下载:https://pypi.python.org/pypi/xlrd#download ...
- 读/写xlsx文件
安装 pip install openpyxl 1.创建Excel电子表格 建立新文档需要调用Workbook对象的save方法,一个Workbook对象代表一个Excel工作簿,该方法的参数是保存的 ...
- 读/写docx文件
安装 pip install python-docx 1.建立新Word文档 建立新文档需要调用Document对象的save方法,一个Document对象代表一个Word文档,该方法的参数是保存的文 ...
- 【学习总结】GirlsInAI ML-diary day-15-读/写txt文件
[学习总结]GirlsInAI ML-diary 总 原博github链接-day15 认识读/写txt文件 路径: 绝对路径:文件在电脑中的位置 相对路径:下面会用到 1-准备 新建一个 pytho ...
- python读、写、修改、追写excel文件
三个工具包 python操作excel的三个工具包如下 xlrd: 对excel进行读相关操作 xlwt: 对excel进行写相关操作 xlutils: 对excel读写操作的整合 注意,只能操作.x ...
- PLSQL_PLSQL读和写XML文件方式(案例)
2012-05-01 Created By BaoXinjian
- PLSQL_PLSQL读和写CSV文件方式(案例)
2012-01-06 Created By BaoXinjin
随机推荐
- jmeter测试jdbc、mysql
1.打入jar包,在测试计划或者直接放在lib/ext下(第三方的包) 2.添加jdbc-connection-config,variable name 和后面的jdbc请求的参数保持一致 Datab ...
- VMware 虚拟化编程(10) — VMware 数据块修改跟踪技术 CBT
目录 目录 前文列表 数据块修改跟踪技术 CBT 为虚拟机开启 CBT CBT 修改数据块偏移量获取函数 QueryChangedDiskAreas changeId 一个 QueryChangedD ...
- VMware 虚拟化编程(2) — 虚拟磁盘文件类型详解
目录 目录 前文列表 虚拟磁盘文件 VMDK 用户可以创建的虚拟磁盘类型 VixDiskLib 中支持的虚拟磁盘类型 虚拟机文件类型 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/ ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_7_HashMap存储自定义类型键值
自定义类型做key值.必须要重写hashCode和equals方法 创建pserson类 有name个age两个成员变量.重写toString方法 key有重复,会被新的value值替换掉. key值 ...
- mooc-IDEA 列操作--005
十一.IntelliJ IDEA -列操作 实例:根据HTTP请求JSON文件,生成一个枚举类 Step1:创建一个枚举类,把要转换的JSON串粘贴进来. 最终要实现效果 Step2:选中第一个100 ...
- Struts2的核心配置文件
Struts2的详细配置: 配置的是struts2的核心配置文件:,在struts2的核心配置文件中主要有三个标签需要进行配置:package,action,result. 1. 配置package标 ...
- dfs(找环)
https://codeforces.com/problemset/problem/1249/B2 B2. Books Exchange (hard version) time limit per t ...
- 在react中用装饰器来装饰connect
假设我们在react中有如下header组件: import React, { PureComponent } from 'react'; import { connect } from 'react ...
- 回溯---Permutations II
47.Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) [1,1,2] hav ...
- hash和history
location.hash="aaa" history.pushState({},'', "home") history.replaceState() hist ...