image.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from imageio import imread
  2. from os.path import isfile
  3. import copy
  4. import numpy as np
  5. from . import utils
  6. def should_have_parts(func):
  7. def inner(self, *args, **kwargs):
  8. assert self.has_parts, "parts are not present!"
  9. return func(self, *args, **kwargs)
  10. return inner
  11. class ImageWrapper(object):
  12. def __init__(self, im_path, label, parts=None, mode="RGB"):
  13. if isinstance(im_path, str):
  14. assert isfile(im_path), "Image \"{}\" does not exist!".format(im_path)
  15. self.im = imread(im_path, pilmode=mode)
  16. else:
  17. self.im = im_path
  18. self.label = label
  19. self.parts = parts
  20. self.parent = None
  21. self._feature = None
  22. def as_tuple(self):
  23. return self.im, self.parts, self.label
  24. def copy(self):
  25. new = copy.deepcopy(self)
  26. new.parent = self
  27. return new
  28. @property
  29. def feature(self):
  30. return self._feature
  31. @feature.setter
  32. def feature(self, im_feature):
  33. self._feature = im_feature
  34. def crop(self, x, y, w, h):
  35. result = self.copy()
  36. result.im = self.im[y:y+h, x:x+w]
  37. if self.has_parts:
  38. result.parts[:, 1] -= x
  39. result.parts[:, 2] -= y
  40. return result
  41. @should_have_parts
  42. def hide_parts_outside_bb(self, x, y, w, h):
  43. idxs, (xs,ys) = self.visible_part_locs()
  44. f = np.logical_and
  45. mask = f(f(x <= xs, xs <= x+w), f(y <= ys, ys <= y+h))
  46. result = self.copy()
  47. result.parts[:, -1] = mask.astype(self.parts.dtype)
  48. return result
  49. def uniform_parts(self, ratio):
  50. result = self.copy()
  51. result.parts = utils.uniform_parts(self.im, ratio=ratio)
  52. return result
  53. @should_have_parts
  54. def select_parts(self, idxs):
  55. result = self.copy()
  56. result.parts[:, -1] = 0
  57. result.parts[idxs, -1] = 1
  58. return result
  59. @should_have_parts
  60. def select_random_parts(self, rnd, n_parts):
  61. idxs, xy = self.visible_part_locs()
  62. rnd_idxs = utils.random_idxs(idxs, rnd=rnd, n_parts=n_parts)
  63. return self.select_parts(rnd_idxs)
  64. @should_have_parts
  65. def visible_crops(self, ratio):
  66. return utils.visible_crops(self.im, self.parts, ratio=ratio)
  67. @should_have_parts
  68. def visible_part_locs(self):
  69. return utils.visible_part_locs(self.parts)
  70. @should_have_parts
  71. def reveal_visible(self, ratio):
  72. _, xy = self.visible_part_locs()
  73. result = self.copy()
  74. result.im = utils.reveal_parts(self.im, xy, ratio=ratio)
  75. return result
  76. @should_have_parts
  77. def part_crops(self, ratio):
  78. crops = self.visible_crops(ratio)
  79. idxs, _ = self.visible_part_locs()
  80. result = self.copy()
  81. result.im = crops[idxs]
  82. return result
  83. @property
  84. def has_parts(self):
  85. return self.parts is not None