aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.42k stars 2.44k forks source link

AttributeError: 'list' object has no attribute 'ndim' #558

Closed purvikpatel closed 4 years ago

purvikpatel commented 4 years ago

I am using the below given code to augment a list of images but It is giving me error mention below the code . Can anyone check my code and tell me where I am making the mistake?

import os
import cv2 
import glob
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa

def GetImage():
    images = []
    for imgFile in glob.glob("images/*.jpg"):
        img = cv2.imread(imgFile)
        images.append(img)

    return images

def augmentation():

    images = GetImage()

    seq = iaa.Sequential([
    iaa.Fliplr(0.5), 
    iaa.Crop(percent=(0, 0.1)),
    iaa.Sometimes(0.5,
        iaa.GaussianBlur(sigma=(0, 0.5))
    ),
    iaa.LinearContrast((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)

    image_aug = seq(image=images)

    i = 1

    for images  in image_aug:
        path = "/home/purvik/Desktop/AUV/Augmentation/newImages/" 
        cv2.imwrite(os.path.join(path + str(i) + ".jpg"),image_aug)
        i = i + 1

if __name__ == "__main__":
    augmentation()
  File "aug.py", line 125, in <module>
    augmentation()
  File "aug.py", line 80, in augmentation
    image_aug = seq(image=images)
  File "/home/purvik/.local/lib/python2.7/site-packages/imgaug/augmenters/meta.py", line 1913, in __call__
    return self.augment(*args, **kwargs)
  File "/home/purvik/.local/lib/python2.7/site-packages/imgaug/augmenters/meta.py", line 1884, in augment
    batch_aug = self.augment_batch(batch, hooks=hooks)
  File "/home/purvik/.local/lib/python2.7/site-packages/imgaug/augmenters/meta.py", line 511, in augment_batch
    batch = batch.to_normalized_batch()
  File "/home/purvik/.local/lib/python2.7/site-packages/imgaug/augmentables/batches.py", line 147, in to_normalized_batch
    images_unaug = nlib.normalize_images(self.images_unaug)
  File "/home/purvik/.local/lib/python2.7/site-packages/imgaug/augmentables/normalization.py", line 140, in normalize_images
    assert image.ndim in [2, 3], (
AttributeError: 'list' object has no attribute 'ndim'
aleju commented 4 years ago

Your GetImage() function actually returns multiple images instead of a single one. Either change it to return a single (H,W,C) numpy array or change image_aug = seq(image=images) to images_aug = seq(images=images) (i.e. singular to plural). Otherwise the augmentation method will assume that you provided only a single image (image=...) given as a numpy array, which hence should have an ndim attribute. list doesn't have that attribute and that results in a crash.