python 中的数据扩充引发错误 "int() argument must be a string, a bytes-like object or a number, not 'dict'"

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

我正在尝试加载文件夹中存在的所有图像,扩充每个图像并将其保存在不同的存储库中

我可以通过对路径和图像名称进行硬编码来增强 an 但是,当我尝试存储所有图像的路径然后处理循环时,它不起作用并抛出title (也 : int() 参数必须是一个字符串,一个类似字节的对象或一个数字,而不是 'dict' ).在增强图像后的后面部分,我将输出存储在不同的文件夹中。代码也如下:

如果有人能提供解决这个问题的方法,那将非常有帮助。

    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'

我不熟悉 imgaug,但我认为这应该可行:

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

从那时起,您可以继续使用您的原始代码。

请注意,如果您有很多图像,您可能 运行 会遇到内存问题。在这种情况下,您需要将列表分成更小的批次(有点像您对 'range(32)' 所做的)。如果您需要帮助,请添加评论。

下面是代码,如果这是我自己犯的一个非常愚蠢的错误,我深表歉意

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