初探TensorFLow从文件读取图片的四种方式


Posted in Python onFebruary 06, 2018

本文记录一下TensorFLow的几种图片读取方法,官方文档有较为全面的介绍。

1.使用gfile读图片,decode输出是Tensor,eval后是ndarray

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

print(tf.__version__)

image_raw = tf.gfile.FastGFile('test/a.jpg','rb').read()  #bytes
img = tf.image.decode_jpeg(image_raw) #Tensor
#img2 = tf.image.convert_image_dtype(img, dtype = tf.uint8)

with tf.Session() as sess:
  print(type(image_raw)) # bytes
  print(type(img)) # Tensor
  #print(type(img2))

  print(type(img.eval())) # ndarray !!!
  print(img.eval().shape)
  print(img.eval().dtype)

#  print(type(img2.eval()))
#  print(img2.eval().shape)
#  print(img2.eval().dtype)
  plt.figure(1)
  plt.imshow(img.eval())
  plt.show()

输出为:

1.3.0
<class 'bytes'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
图片显示(略)

2.使用WholeFileReader输入queue,decode输出是Tensor,eval后是ndarray

import tensorflow as tf
import os
import matplotlib.pyplot as plt
def file_name(file_dir):  #来自https://3water.com/article/134543.htm
  for root, dirs, files in os.walk(file_dir): #模块os中的walk()函数遍历文件夹下所有的文件
    print(root) #当前目录路径 
    print(dirs) #当前路径下所有子目录 
    print(files) #当前路径下所有非目录子文件 

def file_name2(file_dir):  #特定类型的文件
  L=[]  
  for root, dirs, files in os.walk(file_dir): 
    for file in files: 
      if os.path.splitext(file)[1] == '.jpg':  
        L.append(os.path.join(root, file)) 
  return L 

path = file_name2('test')


#以下参考https://3water.com/article/134547.htm (十图详解TensorFlow数据读取机制)
#path2 = tf.train.match_filenames_once(path)
file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #创建输入队列 
image_reader = tf.WholeFileReader() 
key, image = image_reader.read(file_queue) 
image = tf.image.decode_jpeg(image) 

with tf.Session() as sess: 
#  coord = tf.train.Coordinator() #协同启动的线程 
#  threads = tf.train.start_queue_runners(sess=sess, coord=coord) #启动线程运行队列 
#  coord.request_stop() #停止所有的线程 
#  coord.join(threads) 

  tf.local_variables_initializer().run()
  threads = tf.train.start_queue_runners(sess=sess)

  #print (type(image)) 
  #print (type(image.eval())) 
  #print(image.eval().shape)
  for _ in path+path:
    plt.figure
    plt.imshow(image.eval())
    plt.show()

3.使用read_file,decode输出是Tensor,eval后是ndarray

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

print(tf.__version__)

image_value = tf.read_file('test/a.jpg')
img = tf.image.decode_jpeg(image_value, channels=3)

with tf.Session() as sess:
  print(type(image_value)) # bytes
  print(type(img)) # Tensor
  #print(type(img2))

  print(type(img.eval())) # ndarray !!!
  print(img.eval().shape)
  print(img.eval().dtype)

#  print(type(img2.eval()))
#  print(img2.eval().shape)
#  print(img2.eval().dtype)
  plt.figure(1)
  plt.imshow(img.eval())
  plt.show()

输出是:

1.3.0
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
显示图片(略)

4.TFRecords:

有空再看。

如果图片是根据分类放在不同的文件夹下,那么可以直接使用如下代码:
https://3water.com/article/134532.htm
https://3water.com/article/134539.htm

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python open读写文件实现脚本
Sep 06 Python
python executemany的使用及注意事项
Mar 13 Python
python生成式的send()方法(详解)
May 08 Python
浅谈python socket函数中,send与sendall的区别与使用方法
May 09 Python
spyder常用快捷键(分享)
Jul 19 Python
python实现BackPropagation算法
Dec 14 Python
python实现简单的单变量线性回归方法
Nov 08 Python
Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解
Feb 18 Python
详解Python中pyautogui库的最全使用方法
Apr 01 Python
python调用API接口实现登陆短信验证
May 10 Python
基于Pytorch版yolov5的滑块验证码破解思路详解
Feb 25 Python
python使用openpyxl库读写Excel表格的方法(增删改查操作)
May 02 Python
用十张图详解TensorFlow数据读取机制(附代码)
Feb 06 #Python
Python实现matplotlib显示中文的方法详解
Feb 06 #Python
Python实现自动上京东抢手机
Feb 06 #Python
Python获取指定文件夹下的文件名的方法
Feb 06 #Python
TensorFlow如何实现反向传播
Feb 06 #Python
tensorflow TFRecords文件的生成和读取的方法
Feb 06 #Python
TensorFlow实现创建分类器
Feb 06 #Python
You might like
javascript 写类方式之六
2009/07/05 Javascript
JavaScript 错误处理与调试经验总结
2010/08/10 Javascript
Web 前端设计模式--Dom重构 提高显示性能
2010/10/22 Javascript
jquery如何把参数列严格转换成数组实现思路
2013/04/01 Javascript
js运动框架_包括图片的淡入淡出效果
2013/05/11 Javascript
一个检测表单数据的JavaScript实例
2014/10/31 Javascript
bootstrap手风琴制作方法详解
2017/01/11 Javascript
jquery插件ContextMenu设置右键菜单
2017/03/13 Javascript
JS查找英文文章中出现频率最高的单词
2017/03/20 Javascript
JS实现瀑布流布局
2017/10/21 Javascript
VueJs使用Amaze ui调整列表和内容页面
2017/11/30 Javascript
基于 Vue.js 2.0 酷炫自适应背景视频登录页面实现方式
2018/01/17 Javascript
vue源码学习之Object.defineProperty 对数组监听
2018/05/30 Javascript
Array.filter中如何正确使用Async
2020/11/04 Javascript
[01:05:56]Liquid vs VP Supermajor决赛 BO 第二场 6.10
2018/07/04 DOTA
[36:41]完美世界DOTA2联赛循环赛FTD vs Magma第一场 10月30日
2020/10/31 DOTA
布同自制Python函数帮助查询小工具
2011/03/13 Python
python模拟登录百度代码分享(获取百度贴吧等级)
2013/12/27 Python
Python中is与==判断的区别
2017/03/28 Python
python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解
2017/10/20 Python
浅谈Python2、Python3相对路径、绝对路径导入方法
2018/06/22 Python
Python实现base64编码的图片保存到本地功能示例
2018/06/22 Python
Python3多线程版TCP端口扫描器
2019/08/31 Python
python中wheel的用法整理
2020/06/15 Python
Python实现Word文档转换Markdown的示例
2020/12/22 Python
Myprotein俄罗斯官网:欧洲第一运动营养品牌
2019/05/05 全球购物
您附近的水疗和健康场所:Spafinder(美国)
2019/07/05 全球购物
澳大利亚家居用品零售商:Harris Scarfe
2020/10/10 全球购物
高中毕业生个人自我鉴定
2013/11/24 职场文书
安全口号大全
2014/06/21 职场文书
不同意离婚代理词
2015/05/23 职场文书
2015年毕业实习工作总结
2015/05/29 职场文书
开学第一周总结
2015/07/16 职场文书
辞职申请书范本
2019/05/20 职场文书
关于 Python json中load和loads区别
2021/11/07 Python
win sever 2022如何占用操作主机角色
2022/06/25 Servers