Home > Software design >  Iterating through images for data augmentation
Iterating through images for data augmentation

Time:09-17

I'm trying to iterate through images in order to perform data augmentation. For this purpose I use the following:

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:\Users\my_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

But for some reason I have an error message: UnidentifiedImageError: cannot identify image file '36.jpg'

But 36.jpg exists there with the exact same title and extension.

What causes the problem?

Thank you in advance.

CodePudding user response:

There was a problem with the exact two images. In case to avoid more problems I converted every single one into RGB and put it inside a loop:

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:\Users\my_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
  • Related