簡體   English   中英

擴充 keras 文件夾中的所有圖像

[英]Augment all the images in a folder in keras

我正在嘗試一次增強所有衛星圖像(.TIFF 格式),但一直遇到錯誤錯誤: “(' NumpyArrayIterator中的輸入數據應該具有等級 4。您傳遞了一個形狀為'的數組',(0,))”當我一次做一個沒有錯誤但是當我在一個文件夾中運行它時它給了我錯誤。有人可以告訴我我做錯了什么

謝謝

from keras.preprocessing.image import ImageDataGenerator
from skimage import io
datagen = ImageDataGenerator(
        rotation_range=45,     
        width_shift_range=0.2,   
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='constant', cval=125)

image_directory = 'E:\\opencv'
SIZE = 150
dataset = []

my_images = os.listdir(image_directory)
for i, image_name in enumerate(my_images):
    if (image_name.split('.')[1] == 'TIFF'):
        image = io.imread(image_directory + image_name)
        image = Image.fromarray(image, 'RGB')
        image = image.resize((SIZE,SIZE))
        dataset.append(np.array(image))

x = np.array(dataset)
i = 0
for batch in datagen.flow(x, batch_size=16,  
                          save_to_dir='E:\\opencv', 
                          save_prefix='a', 
                          save_format='TIFF'):
    i += 1
    if i > 20:
        break

修改代碼如下

for i, image_name in enumerate(my_images):
    if (image_name.split('.')[1] == 'TIFF'):
        fpath=os.path.join(image_directory, image_name)
        print (fpath) # if this does not execute your if statement is incorrect
        image = io.imread(fpath)
        print (image.shape) # verify the image was read correctly
        image = Image.fromarray(image, 'RGB')
        image = image.resize((SIZE,SIZE))
        print (image.size) # verify image was resized
        dataset.append(np.array(image))

或者

for i, image_name in enumerate(my_images):
    if (image_name.split('.')[1] == 'TIFF'):        
        image = io.imread(image_directory + '/' + image_name)
        image = Image.fromarray(image, 'RGB')
        image = image.resize((SIZE,SIZE))
        dataset.append(np.array(image))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM