import os
import shutil
import tensorflow as tf
import time
import sys
import cv2 # 图片存放位置
PATH_RES = [
r'data_tfrecords/integers_tfrecords/train.tfrecords',
r'data_tfrecords/integers_tfrecords/test.tfrecords',
r'data_tfrecords/alphabets_tfrecords/train.tfrecords',
r'data_tfrecords/alphabets_tfrecords/test.tfrecords',
r'data_tfrecords/Chinese_letters_tfrecords/train.tfrecords',
r'data_tfrecords/Chinese_letters_tfrecords/test.tfrecords'
]
PATH_DES = [
r'imgs_from_tfrecords/integers/train/',
r'imgs_from_tfrecords/integers/test/',
r'imgs_from_tfrecords/alphabets/train/',
r'imgs_from_tfrecords/alphabets/test/',
r'imgs_from_tfrecords/Chinese_letters/train/',
r'imgs_from_tfrecords/Chinese_letters/test/'
] PATH = list(zip(PATH_RES, PATH_DES)) def tfrecord2jpg(path_res, path_des):
print('tfrecords_files to be transformed:', path_res)
reader = tf.TFRecordReader()
start_time = int(time.time())
prev_time = start_time
idx = 0 filename_queue = tf.train.string_input_producer([path_res], num_epochs=1) # 从 TFRecord 读取内容并保存到 serialized_example 中
_, serialized_example = reader.read(filename_queue)
# 读取 serialized_example 的格式
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
}) # 解析从 serialized_example 读取到的内容
images = tf.decode_raw(features['image_raw'], tf.uint8)
labels = tf.cast(features['label'], tf.int64) print('Extracting {} has just started.'.format(path_res))
with tf.Session() as sess:
# 启动多线程
sess.run(tf.local_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
while not coord.should_stop():
try:
label, img = sess.run([labels, images])
except tf.errors.OutOfRangeError:
print("Turn to next folder.")
break
cv2.imwrite(path_des+"_"+str(idx)+"_"+str(label)+'.jpg', img)
idx += 1
current_time = int(time.time())
lasting_time = current_time - start_time
interval_time = current_time - prev_time
if interval_time >= 0.1:
sys.stdout.flush()
sys.stdout.write("\rGenerating the {}-th image: {},\
lasting {} seconds".format(
idx,
path_des +
str(idx) + '_' +
str(label) + '.jpg',
lasting_time))
prev_time = current_time
coord.request_stop()
coord.join(threads) def main():
# get empty directory
for i in range(len(PATH)):
if os.path.isdir(PATH_DES[i]):
if os.listdir(PATH_DES[i]):
shutil.rmtree(PATH_DES[i])
os.mkdir(PATH_DES[i])
else:
print(PATH_DES[i])
os.mkdir(PATH_DES[i])
tfrecord2jpg(PATH_RES[i], PATH_DES[i]) if __name__ == "__main__":
main()

tfrecords转图片存储的更多相关文章

  1. 免费图片存储和图话【提供demo下载】

    我们不管是做博客系统还是其他网站,图片是免不了要使用到的.但是,我们都知道图片的访问是很耗资源的,同时也是很占磁盘空间的,且还特别占带宽. 所以,我们一般都会用到特定的图片服务器.不过,像我等屌丝平时 ...

  2. python、matlab、c++的括号增加次序,以及图片存储方式

    1 增加次序: python:(同c++多维数组) np.zeros([2,3,4]),先是按照内存空间均分为2份,每份又均分3份,最终再细分4份            2最大份,先按左分 例子:re ...

  3. mongodb 文件,图片存储数据库

    mongodb  文件,图片存储数据库

  4. ECStore图片存储采用阿里云OSS(图片存储)服务

    主要功能:ECStore图片存储采用阿里云OSS(图片存储)服务   适用版本:ECStore 授权方式:授权域名使用,付费插件 联系方式: QQ 275553385  mail: jimingson ...

  5. 为图片存储而作——记一次UEditor源码的修改

    本文版权归博客园和作者吴双本人共同所有.  写在前面 这是一个数据爆发的网络时代,大家习惯于浏览图文直观带给我们的快速信息.大图片的存储和浏览经常会成为Web服务器的瓶颈.试想如果你的Web服务器依然 ...

  6. 腾讯云万象优图每个账户提供50G的图片存储(支持黄图检测)

    文章由GIT博客迁移过来 程序下载地址(源码也在):点我下载 设计说明 10月20号晚上,准备写这么一个程序. 腾讯云万象优图每个账户提供50G的图片存储(支持黄图检测) 可以在截图之后,直接点击上传 ...

  7. [TFRecord格式数据]利用TFRecords存储与读取带标签的图片

    利用TFRecords存储与读取带标签的图片 原创文章,转载请注明出处~ 觉得有用的话,欢迎一起讨论相互学习~Follow Me TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是 ...

  8. 图像转化成TFrecords格式并回转

    import os import tensorflow as tf from PIL import Image import numpy as np cat_image_path='D:/软件/pyc ...

  9. TensorFlow中数据读取之tfrecords

    关于Tensorflow读取数据,官网给出了三种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow ...

随机推荐

  1. python 编译EXE文件

    以labelme测试 标注工具labelimg和labelme 矩形标注工具:labelimg 多边形标准工具:labelme 前者官网发布了可执行文件,后者只有python源码,如果需要编译wind ...

  2. 【Azure 应用服务】App Service中,为Java应用配置自定义错误页面,禁用DELETE, PUT方法

    问题定义 使用Azure应用服务(App Service),部署Java应用,使用Tomcat容器,如何自定义错误页面呢?同时禁用DELETE, PUT方法 解决办法 如何自定义错误页面呢?需要在 J ...

  3. 【WPF】将DataGrid内容导出到Excel

    引言 在做项目时要求将datagrid的内容导出到Excel,以前做winform项目时遇到过,就把代码搬过来用,但wpf和winform还是有些不同,就修改了一些东西,使其能实现这个功能. 本文是导 ...

  4. Web自动化测试python环境中安装 --selenium安装、火狐和火狐驱动版本、谷歌和谷歌驱动版本、测试

    一.安装selenium Windows命令行(cmd)输入pip install selenium(无须指定版本默认最新)或 pip install selenium==3.141.0(可指定版本) ...

  5. jasper使用table组件设计复杂的表头

    1.1 设计报表模板 1.1.1 新建模板DemoReport5.jrxml,去掉不需要的Band,保留Title,Page Header,Detail 1 , PageFooter.将组件Table ...

  6. WS2812B彩灯详细讲解篇(STM32 PWM+DMA控制 STM32 HAL库编程 循环延时控制多种控制方式)

    一.效果展示 观看演示效果:https://www.bilibili.com/video/BV1JT4y1P72Q 二. 基础认识 (一)  小理论 WS2812B是一种智能控制LED光源,将控制电路 ...

  7. redis性能优化、内存分析及优化

    redis性能优化.内存分析及优化 1.优化网络延时 2.警惕执行时间长的操作 3.优化数据结构.使用正确的算法 4.考虑操作系统和硬件是否影响性能 5.考虑持久化带来的开销 5.1 RDB 全量持久 ...

  8. HBase协处理器加载过程(1.2.x)

    一.首先来看ObserverCoprocessor:1.观察者协处理器的静态加载的配置是在hbase-site.xml中配置如下属性:通过hbase.coprocessor.region.classe ...

  9. Linux 调整系统时间偏差

    在使用Linux系统部署项目,有时会出现时间跟当前时间不一致的情况,这个时候需要做些调整: 1.首先删除之前设置的时区 rm -rf /etc/localtime 2.创建上海时区 ln -s /us ...

  10. Python单元测试框架pytest常用测试报告类型

    先前博客有介绍pytest测试框架的安装及使用,现在来聊聊pytest可以生成哪些测试报告 1.allure测试报告 关于allure报告参见先前的一篇博文:https://www.cnblogs.c ...