utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import numpy as np
  2. from PIL.Image import Image as PIL_Image
  3. DEFAULT_RATIO = np.sqrt(49 / 400)
  4. def dimensions(im):
  5. if isinstance(im, np.ndarray):
  6. if im.ndim != 3:
  7. import pdb; pdb.set_trace()
  8. assert im.ndim == 3, "Only RGB images are currently supported!"
  9. return im.shape
  10. elif isinstance(im, PIL_Image):
  11. w, h = im.size
  12. c = len(im.getbands())
  13. # assert c == 3, "Only RGB images are currently supported!"
  14. return h, w, c
  15. else:
  16. raise ValueError("Unknown image instance ({})!".format(type(im)))
  17. def asarray(im, dtype=np.uint8):
  18. if isinstance(im, np.ndarray):
  19. return im.astype(dtype)
  20. elif isinstance(im, PIL_Image):
  21. return np.asarray(im, dtype=dtype)
  22. else:
  23. raise ValueError("Unknown image instance ({})!".format(type(im)))
  24. def select_crops(crops, mask):
  25. selected = np.zeros_like(crops)
  26. selected[mask] = crops[mask]
  27. return selected
  28. def selection_mask(idxs, n):
  29. return np.bincount(idxs, minlength=n).astype(bool)
  30. def random_select(idxs, xy, part_crops, *args, **kw):
  31. rnd_idxs = random_idxs(np.arange(len(idxs)), *args, **kw)
  32. idxs = idxs[rnd_idxs]
  33. xy = xy[:, rnd_idxs]
  34. mask = selection_mask(idxs, len(part_crops))
  35. selected_crops = select_crops(part_crops, mask)
  36. return idxs, xy, selected_crops
  37. def random_idxs(idxs, rnd=None, n_parts=None):
  38. if rnd is None or isinstance(rnd, int):
  39. rnd = np.random.RandomState(rnd)
  40. else:
  41. assert isinstance(rnd, np.random.RandomState), \
  42. "'rnd' should be either a random seed or a RandomState instance!"
  43. n_parts = n_parts or rnd.randint(1, len(idxs))
  44. res = rnd.choice(idxs, n_parts, replace=False)
  45. res.sort()
  46. return res
  47. ##### DEPRECATED #####
  48. # def __expand_parts(p):
  49. # return p[:, 0], p[:, 1:3], p[:, 3].astype(bool)
  50. def uniform_parts(im, ratio=DEFAULT_RATIO, round_op=np.floor):
  51. raise DeprecationWarning("Do not use me!")
  52. # h, w, c = dimensions(im)
  53. # part_w = round_op(w * ratio).astype(np.int32)
  54. # part_h = round_op(h * ratio).astype(np.int32)
  55. # n, m = w // part_w, h // part_h
  56. # parts = np.ones((n*m, 4), dtype=int)
  57. # parts[:, 0] = np.arange(n*m)
  58. # for x in range(n):
  59. # for y in range(m):
  60. # i = y * n + x
  61. # x0, y0 = x * part_w, y * part_h
  62. # parts[i, 1:3] = [x0 + part_w // 2, y0 + part_h // 2]
  63. # return parts
  64. def rescale_parts(im, parts, part_rescale_size):
  65. raise DeprecationWarning("Do not use me!")
  66. # if part_rescale_size is None or part_rescale_size < 0:
  67. # return parts
  68. # h, w, c = dimensions(im)
  69. # scale = np.array([w, h]) / part_rescale_size
  70. # xy = parts[:, 1:3]
  71. # xy = xy * scale
  72. # parts[:, 1:3] = xy
  73. # if parts.shape[1] == 5:
  74. # wh = parts[:, 3:5]
  75. # wh = wh * scale
  76. # parts[:, 3:5] = wh
  77. # return parts
  78. def visible_part_locs(p):
  79. raise DeprecationWarning("Do not use me!")
  80. # res = p.visible_locs()
  81. # return res
  82. # idxs, locs, vis = __expand_parts(p)
  83. # return idxs[vis], locs[vis].T
  84. def crop(im, xy, w, h, padding_mode="edge", is_location=True):
  85. raise DeprecationWarning("Do not use me!")
  86. # x, y = xy
  87. # pad_h, pad_w = h // 2, w // 2
  88. # padded_im = np.pad(im, [(pad_h, pad_h), (pad_w, pad_w), [0,0]], mode=padding_mode)
  89. # x0, y0 = x + pad_w, y + pad_h
  90. # if is_location:
  91. # x0, y0 = x0 - w // 2, y0 - h // 2
  92. # return padded_im[y0:y0+h, x0:x0+w]
  93. def crops(im, xys, ratio=DEFAULT_RATIO, padding_mode="edge"):
  94. raise DeprecationWarning("Do not use me!")
  95. # h, w, c = dimensions(im)
  96. # crop_h, crop_w = int(h * ratio), int(w * ratio)
  97. # return np.stack([crop(im, xy, crop_w, crop_h, padding_mode)
  98. # for xy in xys.T])
  99. def visible_crops(im, p, *args, **kw):
  100. raise DeprecationWarning("Do not use me!")
  101. # res = p.visible_crops(*args, **kw)
  102. # return res
  103. # idxs, locs, vis = __expand_parts(p)
  104. # parts = crops(asarray(im), locs[vis].T, *args, **kw)
  105. # res = np.zeros((len(idxs),) + parts.shape[1:], dtype=parts.dtype)
  106. # res[vis] = parts
  107. # return res
  108. def reveal_parts(im, xy, ratio=DEFAULT_RATIO):
  109. raise DeprecationWarning("Do not use me!")
  110. # h, w, c = dimensions(im)
  111. # crop_h, crop_w = int(h * ratio), int(w * ratio)
  112. # im = asarray(im)
  113. # res = np.zeros_like(im)
  114. # for x, y in xy.T:
  115. # x0, y0 = max(x - crop_w // 2, 0), max(y - crop_h // 2, 0)
  116. # res[y0:y0+crop_h, x0:x0+crop_w] = im[y0:y0+crop_h, x0:x0+crop_w]
  117. # return res