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()

最新文章

  1. 01@MySQL_Course_LabVIEW+MySQL程序开发
  2. sprig里的controller之间的跳转的问题
  3. SRC单一职责原则
  4. 2014年4月份第4周51Aspx源码发布详情
  5. 设置Android程序的默认安装位置
  6. golang(2):beego 环境搭建
  7. 我的Python成长之路---第三天---Python基础(11)---2016年1月16日(雾霾)
  8. solrcloud集群部署
  9. NOIP模拟赛---1.生气的LJJ (anger)
  10. 性能调优之SQL优化
  11. C#中回车出发事件(+收藏)
  12. 优雅地实现CSS Animation delay心得
  13. Android之Fragment详解
  14. mongodb安装建议
  15. The current state of generics in Delphi( 转载)
  16. ssh中的 Connection closed by ***
  17. SystemC_Basic
  18. Java虚拟机--虚拟机字节码执行引擎
  19. Linux下tomcat无法启动
  20. ArcGIS案例学习1_2

热门文章

  1. 一种优化递归算法的方法(javascript)
  2. 字节跳动在 Go 网络库上的实践
  3. http的响应码及含义
  4. P5686 和积和
  5. LOJ10067
  6. Maven环境搭建以及在IDEA中的配置与简单入门
  7. Java——I/O相关练习代码
  8. Prometheus 初探和配置(安装测试)
  9. Spring MVC——基础(简介,使用,地址映射)
  10. Spark练习之创建RDD(集合、本地文件),RDD持久化及RDD持久化策略