TensorFLow如何從文件讀取圖片-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關TensorFLow如何從文件讀取圖片的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

十年的金秀網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整金秀建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“金秀網(wǎng)站設計”,“金秀網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

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://www.jb51.net/article/134543.htm
  for root, dirs, files in os.walk(file_dir): #模塊os中的walk()函數(shù)遍歷文件夾下所有的文件
    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數(shù)據(jù)讀取機制)
#path3 = tf.train.match_filenames_once(path)
file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #創(chuàng)建輸入隊列 
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() #協(xié)同啟動的線程 
#  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
顯示圖片(略)

感謝各位的閱讀!關于“TensorFLow如何從文件讀取圖片”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網(wǎng)站標題:TensorFLow如何從文件讀取圖片-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://muchs.cn/article12/dsgsgc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內鏈、服務器托管、網(wǎng)站設計公司、Google、外貿建站移動網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司