Skip to content Skip to sidebar Skip to footer

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 ho

Solution 1:

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.


Solution 2:

Below is the code, sorry if it is a very silly mistake from my end itself

from PIL import Image
import imgaug as ia
from imgaug import augmenters as iaa

# Here i'm actually opening each image, and putting its pixel data in
# numpy arrays
images = [imageio.imread(imgpath) for imgpath in     glob(path.join('C:/Users/Madhav/Desktop/Final Sem/Data_Images/', '*.png')) ]
# This converts the list from before into a numpy array. If all images have the
# same size, 'images' will be a 'true' numpy array. Otherwise, it's going to be
# a numpy 'collection' (I don't know the real name)
images = np.array(images)

#print (images.shape)


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/Final     Sem/Augmented_Generated/*.png', images_aug[i])

Post a Comment for "Data Augmentation In Python Throws An Error "int() Argument Must Be A String, A Bytes-like Object Or A Number, Not 'dict'""