1

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'
num3ri
  • 822
  • 16
  • 20
  • The constructor `np.array()` with a `dtype` of `np.uint8` needs a list of 8-bit integers as input, but you passed it a list of dictionaries instead. Why did you do that? – jjramsey Aug 02 '19 at 15:21
  • I was trying if passing the path of the images can help here. Is there any alternative? or a way to solve this problem? – madhav lavania Aug 02 '19 at 15:33
  • Does 'images' really need to be a numpy array ? – bastien girschig Aug 02 '19 at 16:08
  • @bastiengirschig Judging from the [documentation for `augment_images`](https://imgaug.readthedocs.io/en/latest/source/api_augmenters_meta.html?highlight=augment_images#imgaug.augmenters.meta.Augmenter.augment_images), `images` can just be a list of numpy arrays. – jjramsey Aug 02 '19 at 16:29
  • Yes, that's what I expected. I've still provided a solution that creates a single numpy array. This could be helpful in some cases (cropping, masking, arithmetic, etc... on all images) – bastien girschig Aug 02 '19 at 16:32

2 Answers2

2

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.

bastien girschig
  • 663
  • 6
  • 26
0

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])