Search code examples
pythonaffinetransformgaussianblurdata-augmentation

Data augmentation in python throws an error "int() argument must be a string, a bytes-like object or a number, not 'dict'"


I am trying to load all the images present in a folder, augment each of them and save it in a different repository

I am able to augment an by hard coding the path and image name however, when I am trying to store the path of all the images and then processing the loop, it is not working and throwing an error mentioned in the title (also : int() argument must be a string, a bytes-like object or a number, not 'dict' ). In the later part after augmenting the image, I am storing the outputs in a different folder. The code is also as follows:

It would be really helpful if anyone can provide a solution to this problem.

    import imgaug as ia
    from imgaug import augmenters as iaa
    import numpy as np
    import pandas as pd # data processing, CSV file I/O (e.g. d.read_csv)
    import os
    from glob import glob
    import imageio

    ia.seed(20)
    #os.chdir('C:/Users/Madhav/Desktop/RIC/data/images_001/images')

    img = {os.path.basename(x): x for x in glob(os.path.join('C:/Users/Madhav/Desktop/FinalSem/Data_Images/','images*','*', '*.png'))}

    #img = imageio.imread("00000001_000.png") #read you image
    images = np.array(
    [img for _ in range(32)], dtype=np.uint8)  # 32 means creat 32 enhanced images using following methods.

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  
            iaa.Crop(percent=(0, 0.1)),            
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),        
            iaa.ContrastNormalization((0.75, 1.5)),         
            iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),    
            iaa.Multiply((0.8, 1.2), per_channel=0.2),
            iaa.Affine(
                scale={
                    "x": (0.8, 1.2),
                    "y": (0.8, 1.2)
                },
                translate_percent={
                    "x": (-0.2, 0.2),
                    "y": (-0.2, 0.2)
                },
                rotate=(-25, 25),
                shear=(-8, 8))
        ],
        random_order=True)  # apply augmenters in random order
    images_aug = seq.augment_images(images)
    for i in range(32):
          imageio.imwrite(str(i)+'C:/Users/Madhav/Desktop/FinalSem/Augmented_Generated/Aug.png', images_aug[i])
#write all changed images

    TypeError
Traceback (most recent call last)
    <ipython-input-11-9a5de30a5337> in <module>
         18 #img = imageio.imread("00000001_000.png") #read you image
         19 images = np.array(
    ---> 20     [img for _ in range(32)], dtype=np.uint8)  # 32 means creat 32         enhanced images using following methods.
         21 
         22 seq = iaa.Sequential(

    TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'

Solution

  • I'm not familiar with imgaug, but I think this should work:

    from os import path
    from glob import glob
    from scipy.ndimage import imread
    import numpy as np
    
    # Here i'm actually opening each image, and putting its pixel data in
    # numpy arrays
    images = [ imread(imgpath) for imgpath in glob(path.join('images', '*.png')) ]
    # 'images' doesn't need to be a numpy array, it can be a regular
    # python list (array). In fact, it can't be if the images are of
    # different sizes
    

    From then on, you can continue with your original code.

    Note that if you have a lot of images, you might run into memory problems. In that case, you'll need to break up your list into smaller batches (kind of like what you did with your 'range(32)'). Add a comment if you need help with that.