iaa_augment.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2019/12/4 18:06
  3. # @Author : zhoujun
  4. import numpy as np
  5. import imgaug
  6. import imgaug.augmenters as iaa
  7. class AugmenterBuilder(object):
  8. def __init__(self):
  9. pass
  10. def build(self, args, root=True):
  11. if args is None or len(args) == 0:
  12. return None
  13. elif isinstance(args, list):
  14. if root:
  15. sequence = [self.build(value, root=False) for value in args]
  16. return iaa.Sequential(sequence)
  17. else:
  18. return getattr(iaa, args[0])(*[self.to_tuple_if_list(a) for a in args[1:]])
  19. elif isinstance(args, dict):
  20. cls = getattr(iaa, args['type'])
  21. return cls(**{k: self.to_tuple_if_list(v) for k, v in args['args'].items()})
  22. else:
  23. raise RuntimeError('unknown augmenter arg: ' + str(args))
  24. def to_tuple_if_list(self, obj):
  25. if isinstance(obj, list):
  26. return tuple(obj)
  27. return obj
  28. class IaaAugment():
  29. def __init__(self, augmenter_args=None):
  30. if augmenter_args is None:
  31. augmenter_args = [{'type': 'Fliplr', 'args': {'p': 0.5}},
  32. {'type': 'Affine', 'args': {'rotate': [-10, 10]}},
  33. {'type': 'Resize', 'args': {'size': [0.5, 3]}}]
  34. self.augmenter = AugmenterBuilder().build(augmenter_args)
  35. def __call__(self, data):
  36. image = data['img']
  37. shape = image.shape
  38. if self.augmenter:
  39. aug = self.augmenter.to_deterministic()
  40. data['img'] = aug.augment_image(image)
  41. data = self.may_augment_annotation(aug, data, shape)
  42. return data
  43. def may_augment_annotation(self, aug, data, shape):
  44. if aug is None:
  45. return data
  46. line_polys = []
  47. for poly in data['text_polys']:
  48. new_poly = self.may_augment_poly(aug, shape, poly)
  49. line_polys.append(np.array(new_poly))
  50. data['text_polys'] = line_polys
  51. return data
  52. def may_augment_poly(self, aug, img_shape, poly):
  53. keypoints = [imgaug.Keypoint(p[0], p[1]) for p in poly]
  54. keypoints = aug.augment_keypoints(
  55. [imgaug.KeypointsOnImage(keypoints, shape=img_shape)])[0].keypoints
  56. poly = [(p.x, p.y) for p in keypoints]
  57. return poly