迭代图像以增强数据



我试图通过图像进行迭代,以便执行数据增强。为此,我使用以下命令:

from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from skimage import io
gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
height_shift_range=0.1, shear_range=0.15,
zoom_range=0.1, channel_shift_range=10., horizontal_flip=True,
fill_mode='nearest')
image_path = r'X:Usersmy_path'
os.chdir(image_path)
for img in os.listdir(image_path):
if img.endswith(".jpg"):
image = io.imread(img, plugin='matplotlib')
image = image.reshape((1, ) + image.shape)
i = 0
for batch in gen.flow(image, batch_size=16,
save_to_dir=image_path,
save_prefix='aug',
save_format='jpg'):
i += 1
if i > 5:
break

但由于某种原因,我有一个错误消息:UnidentifiedImageError: cannot identify image file '36.jpg'

但是36.jpg存在,标题和扩展名完全相同。

问题的原因是什么?

提前谢谢你。

这两个图像有问题。为了避免更多的问题,我把每一个转换成RGB,并把它放在一个循环中:

gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
height_shift_range=0.15, shear_range=0.1,
zoom_range=0.1, channel_shift_range=10., horizontal_flip=True,
brightness_range=[0.3,0.9], fill_mode='nearest')
image_path = r'X:Usersmy_path'
os.chdir(image_path)
j = 1
for img in os.listdir(image_path):
im = Image.open(img)
rgb_im = im.convert('RGB')
rgb_im.save(img)
j += 1


# Obtaining image
for img in os.listdir(image_path):
#if img.endswith(".jpg"):
image = io.imread(img, plugin='matplotlib')
image = image.reshape((-1, ) + image.shape)

i = 0
for batch in gen.flow(image, batch_size=16,
save_to_dir=image_path,
save_prefix='aug',
save_format='jpg'):
i += 1
if i > 6:
break

最新更新