utils.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 random_idxs(idxs, rnd=None, n_parts=None):
  25. if rnd is None or isinstance(rnd, int):
  26. rnd = np.random.RandomState(rnd)
  27. else:
  28. assert isinstance(rnd, np.random.RandomState), \
  29. "'rnd' should be either a random seed or a RandomState instance!"
  30. n_parts = n_parts or rnd.randint(1, len(idxs))
  31. res = rnd.choice(idxs, n_parts, replace=False)
  32. res.sort()
  33. return res
  34. ##### DEPRECATED #####
  35. # def __expand_parts(p):
  36. # return p[:, 0], p[:, 1:3], p[:, 3].astype(bool)
  37. def selection_mask(idxs, n):
  38. raise DeprecationWarning("Do not use me!")
  39. # return np.bincount(idxs, minlength=n).astype(bool)
  40. def select_crops(crops, mask):
  41. raise DeprecationWarning("Do not use me!")
  42. # selected = np.zeros_like(crops)
  43. # selected[mask] = crops[mask]
  44. # return selected
  45. def random_select(idxs, xy, part_crops, *args, **kw):
  46. raise DeprecationWarning("Do not use me!")
  47. # rnd_idxs = random_idxs(np.arange(len(idxs)), *args, **kw)
  48. # idxs = idxs[rnd_idxs]
  49. # xy = xy[:, rnd_idxs]
  50. # mask = selection_mask(idxs, len(part_crops))
  51. # selected_crops = select_crops(part_crops, mask)
  52. # return idxs, xy, selected_crops
  53. def uniform_parts(im, ratio=DEFAULT_RATIO, round_op=np.floor):
  54. raise DeprecationWarning("Do not use me!")
  55. # h, w, c = dimensions(im)
  56. # part_w = round_op(w * ratio).astype(np.int32)
  57. # part_h = round_op(h * ratio).astype(np.int32)
  58. # n, m = w // part_w, h // part_h
  59. # parts = np.ones((n*m, 4), dtype=int)
  60. # parts[:, 0] = np.arange(n*m)
  61. # for x in range(n):
  62. # for y in range(m):
  63. # i = y * n + x
  64. # x0, y0 = x * part_w, y * part_h
  65. # parts[i, 1:3] = [x0 + part_w // 2, y0 + part_h // 2]
  66. # return parts
  67. def rescale_parts(im, parts, part_rescale_size):
  68. raise DeprecationWarning("Do not use me!")
  69. # if part_rescale_size is None or part_rescale_size < 0:
  70. # return parts
  71. # h, w, c = dimensions(im)
  72. # scale = np.array([w, h]) / part_rescale_size
  73. # xy = parts[:, 1:3]
  74. # xy = xy * scale
  75. # parts[:, 1:3] = xy
  76. # if parts.shape[1] == 5:
  77. # wh = parts[:, 3:5]
  78. # wh = wh * scale
  79. # parts[:, 3:5] = wh
  80. # return parts
  81. def visible_part_locs(p):
  82. raise DeprecationWarning("Do not use me!")
  83. # res = p.visible_locs()
  84. # return res
  85. # idxs, locs, vis = __expand_parts(p)
  86. # return idxs[vis], locs[vis].T
  87. def crop(im, xy, w, h, padding_mode="edge", is_location=True):
  88. raise DeprecationWarning("Do not use me!")
  89. # x, y = xy
  90. # pad_h, pad_w = h // 2, w // 2
  91. # padded_im = np.pad(im, [(pad_h, pad_h), (pad_w, pad_w), [0,0]], mode=padding_mode)
  92. # x0, y0 = x + pad_w, y + pad_h
  93. # if is_location:
  94. # x0, y0 = x0 - w // 2, y0 - h // 2
  95. # return padded_im[y0:y0+h, x0:x0+w]
  96. def crops(im, xys, ratio=DEFAULT_RATIO, padding_mode="edge"):
  97. raise DeprecationWarning("Do not use me!")
  98. # h, w, c = dimensions(im)
  99. # crop_h, crop_w = int(h * ratio), int(w * ratio)
  100. # return np.stack([crop(im, xy, crop_w, crop_h, padding_mode)
  101. # for xy in xys.T])
  102. def visible_crops(im, p, *args, **kw):
  103. raise DeprecationWarning("Do not use me!")
  104. # res = p.visible_crops(*args, **kw)
  105. # return res
  106. # idxs, locs, vis = __expand_parts(p)
  107. # parts = crops(asarray(im), locs[vis].T, *args, **kw)
  108. # res = np.zeros((len(idxs),) + parts.shape[1:], dtype=parts.dtype)
  109. # res[vis] = parts
  110. # return res
  111. def reveal_parts(im, xy, ratio=DEFAULT_RATIO):
  112. raise DeprecationWarning("Do not use me!")
  113. # h, w, c = dimensions(im)
  114. # crop_h, crop_w = int(h * ratio), int(w * ratio)
  115. # im = asarray(im)
  116. # res = np.zeros_like(im)
  117. # for x, y in xy.T:
  118. # x0, y0 = max(x - crop_w // 2, 0), max(y - crop_h // 2, 0)
  119. # res[y0:y0+crop_h, x0:x0+crop_w] = im[y0:y0+crop_h, x0:x0+crop_w]
  120. # return res