我想使用ImageDataGeneator增加(增强)我拥有的图像数量(50张)。
我找到了以下代码,但是每次只能增加一个图像(一个文件)。我是python的新手,所以,如果有其他更简单的方法可以一次自动增加50张图像并将结果保存在新文件夹中
import numpy as np
import keras
from keras.preprocessing.image import ImageDataGenerator, array_to_img,
img_to_array, load_img
datagen = ImageDataGenerator(rotation_range =15,
width_shift_range = 0.2,
height_shift_range = 0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip = True,
fill_mode = 'nearest',
data_format='channels_last',
brightness_range=[0.5, 1.5])
# This is my problem, It loads only one file, and i am searching for an
automated method to load number of files together and save them in
another folder.
img = load_img(r"C:\Users\user 1\Pictures\people_1\1.jpg")
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
i = 0
for batch in datagen.flow (x, batch_size=1, save_to_dir =r'C:\Users\user
1\Pictures\people_1\preview', save_prefix ='people2', save_format='jpg'):
i+=1
if i>10:
break
以上代码的预期结果是从1.jpg主文件中提取了10张增强图像,而我还有另外50个文件,我正在寻找一种更快的方法来在一个代码中将它们全部增强
答案 0 :(得分:0)
我认为可以完成工作:
dataset=ImageDataGenerator()
generator = dataset.flow_from_directory('/folder/of/photos',target_size=(110,110),save_to_dir='/folder/to/save/photos',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
for inputs,outputs in generator:
continue
答案 1 :(得分:0)
我认为您可以循环分析每个图像。每次只看一张图像,但是您只需要执行一次代码。 首先,您可以使用Python方法 os.listdir()。此方法将返回档案的所有名称和相应的扩展名。 之后,您可以构造一个循环,以便所有名称都已被读取。 看看:
import numpy as np
import keras
from keras.preprocessing.image import ImageDataGenerator, array_to_img,
img_to_array, load_img
import os
datagen = ImageDataGenerator(rotation_range =15,
width_shift_range = 0.2,
height_shift_range = 0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip = True,
fill_mode = 'nearest',
data_format='channels_last',
brightness_range=[0.5, 1.5])
imgs = os.listdir(<path_50_imgs>)
for img in imgs:
img=cv2.imread(<path_50_imgs>+"\\"+img)
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
i = 0
for batch in datagen.flow (x, batch_size=1, save_to_dir =r'C:\\Users\\user1\\Pictures\\people_1\\preview', save_prefix ='people2', save_format='jpg'):
i+=1
if i>10:
break