0

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

我可以通过硬编码路径和图像名称来增加一个但是,当我尝试存储所有图像的路径然后处理循环时,它不起作用并抛出标题中提到的错误(也: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'
4

2 回答 2

1

我对 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)'所做的那样)。如果您需要帮助,请添加评论。

于 2019-08-02T16:20:12.520 回答
0

下面是代码,对不起,如果这是我自己的一个非常愚蠢的错误

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])
于 2019-08-02T18:49:36.253 回答