【问题标题】:Passing single image throwing error through imgaug通过 imgaug 传递单个图像抛出错误
【发布时间】:2020-03-30 11:39:21
【问题描述】:

我发现了一个例子:https://github.com/aleju/imgaug 您可以查看以下示例:https://github.com/aleju/imgaug#example-augment-images-and-bounding-boxes

我正在通过它传递这张图片:

这是代码:

import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
import cv2
images = cv2.imread("meter0008.jpg")  # two example images

bbs = 
    [ia.BoundingBox(x1=10.5, y1=15.5, x2=30.5, y2=50.5)],
    [ia.BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=50.5),
     ia.BoundingBox(x1=40.5, y1=75.5, x2=70.5, y2=100.5)]
]

seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=0.05*255),
    iaa.Affine(translate_px={"x": (1, 5)})
])

images_aug, bbs_aug = seq(images=images, bounding_boxes=bbs)
cv2.imwrite("hi.jpg", images_aug[0])

但它抛出了这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-e808c2922d9a> in <module>
     16 ])
     17 
---> 18 images_aug, bbs_aug = seq(images=images, bounding_boxes=bbs)
     19 cv2.imwrite("hi.jpg", images_aug[0])

c:\users\fatima.arshad\appdata\local\continuum\anaconda2\envs\web_scraping\lib\site-packages\imgaug\augmenters\meta.py in __call__(self, *args, **kwargs)
   2006     def __call__(self, *args, **kwargs):
   2007         """Alias for :func:`~imgaug.augmenters.meta.Augmenter.augment`."""
-> 2008         return self.augment(*args, **kwargs)
   2009 
   2010     def pool(self, processes=None, maxtasksperchild=None, seed=None):

c:\users\fatima.arshad\appdata\local\continuum\anaconda2\envs\web_scraping\lib\site-packages\imgaug\augmenters\meta.py in augment(self, return_batch, hooks, **kwargs)
   1977         )
   1978 
-> 1979         batch_aug = self.augment_batch_(batch, hooks=hooks)
   1980 
   1981         # return either batch or tuple of augmentables, depending on what

c:\users\fatima.arshad\appdata\local\continuum\anaconda2\envs\web_scraping\lib\site-packages\imgaug\augmenters\meta.py in augment_batch_(self, batch, parents, hooks)
    594         elif isinstance(batch, UnnormalizedBatch):
    595             batch_unnorm = batch
--> 596             batch_norm = batch.to_normalized_batch()
    597             batch_inaug = batch_norm.to_batch_in_augmentation()
    598         elif isinstance(batch, Batch):

c:\users\fatima.arshad\appdata\local\continuum\anaconda2\envs\web_scraping\lib\site-packages\imgaug\augmentables\batches.py in to_normalized_batch(self)
    208                 self.keypoints_unaug, shapes),
    209             bounding_boxes=nlib.normalize_bounding_boxes(
--> 210                 self.bounding_boxes_unaug, shapes),
    211             polygons=nlib.normalize_polygons(
    212                 self.polygons_unaug, shapes),

c:\users\fatima.arshad\appdata\local\continuum\anaconda2\envs\web_scraping\lib\site-packages\imgaug\augmentables\normalization.py in normalize_bounding_boxes(inputs, shapes)
    381     assert ntype == "iterable-iterable-BoundingBox", (
    382         "Got unknown normalization type '%s'." % (ntype,))
--> 383     _assert_exactly_n_shapes_partial(n=len(inputs))
    384     return [BoundingBoxesOnImage(attr_i, shape=shape)
    385             for attr_i, shape

c:\users\fatima.arshad\appdata\local\continuum\anaconda2\envs\web_scraping\lib\site-packages\imgaug\augmentables\normalization.py in _assert_exactly_n_shapes(shapes, n, from_ntype, to_ntype)
     58             "is recommended to provide imgaug standard classes, e.g. "
     59             "KeypointsOnImage for keypoints instead of lists of "
---> 60             "tuples." % (from_ntype, to_ntype, n, len(shapes)))
     61 
     62 

ValueError: Tried to convert data of form 'iterable-iterable-BoundingBox' to 'List[BoundingBoxesOnImage]'. This required exactly 2 corresponding image shapes, but instead 4160 were provided. This can happen e.g. if more images were provided than corresponding augmentables, e.g. 10 images but only 5 segmentation maps. It can also happen if there was a misunderstanding about how an augmentable input would be parsed. E.g. if a list of N (x,y)-tuples was provided as keypoints and the expectation was that this would be parsed as one keypoint per image for N images, but instead it was parsed as N keypoints on 1 image (i.e. 'shapes' would have to contain 1 shape, but N would be provided). To avoid this, it is recommended to provide imgaug standard classes, e.g. KeypointsOnImage for keypoints instead of lists of tuples.

说明: 我只更改了代码来读取此图像。但它似乎抛出了一些关于边界框的错误。我不确定如何解决这个问题

【问题讨论】:

    标签: python image data-augmentation


    【解决方案1】:

    我认为,BoundingBoxesOnImage 没有以正确的方式初始化。此外,在将增强图像写入本地文件夹时,您不应该 images_aug[0] 本身,因为您 在您的images 上有一张图片。这是您的代码的工作示例。

    import numpy as np
    import imgaug as ia
    import imgaug.augmenters as iaa
    from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage
    
    import cv2
    images = cv2.imread("meter0008.jpg")  # two example images
    
    bbs = BoundingBoxesOnImage([
        BoundingBox(x1=10.5, y1=15.5, x2=30.5, y2=50.5),
        BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=50.5),
        BoundingBox(x1=40.5, y1=75.5, x2=70.5, y2=100.5)],
        shape = images.shape)
    
    
    seq = iaa.Sequential([
        iaa.AdditiveGaussianNoise(scale=0.05*255),
        iaa.Affine(translate_px={"x": (1, 5)})
    ])
    
    images_aug, bbs_aug = seq(images=images, bounding_boxes=bbs)
    cv2.imwrite("hi.jpg", images_aug)
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2016-07-30
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 2018-12-26
      • 2020-05-08
      • 1970-01-01
      • 2020-07-31
      相关资源
      最近更新 更多