189 8069 5689

TensorFLow如何从文件读取图片-创新互联

这篇文章给大家分享的是有关TensorFLow如何从文件读取图片的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

十年的金秀网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整金秀建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“金秀网站设计”,“金秀网站推广”以来,每个客户项目都认真落实执行。

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



(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://www.jb51.net/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://www.jb51.net/article/134547.htm (十图详解TensorFlow数据读取机制)
#path3 = 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



(666, 1000, 3)
uint8
显示图片(略)

感谢各位的阅读!关于“TensorFLow如何从文件读取图片”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:TensorFLow如何从文件读取图片-创新互联
链接地址:http://cdxtjz.cn/article/dsgsgc.html

其他资讯