collection.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import numpy as np
  2. from nabirds import utils
  3. from .base import BasePartCollection, BasePart
  4. from .annotation import BBoxPart
  5. class Parts(BasePartCollection):
  6. def __init__(self, image, part_annotations, rescale_size):
  7. super(Parts, self).__init__()
  8. if part_annotations is None:
  9. self._parts = []
  10. else:
  11. self._parts = [BasePart.new(image, a, rescale_size) for a in part_annotations]
  12. class UniformParts(BasePartCollection):
  13. def __init__(self, image, ratio):
  14. super(UniformParts, self).__init__()
  15. self._parts = list(self.generate_parts(image, ratio))
  16. def generate_parts(self, im, ratio, round_op=np.floor):
  17. h, w, c = utils.dimensions(im)
  18. part_w = round_op(w * ratio).astype(np.int32)
  19. part_h = round_op(h * ratio).astype(np.int32)
  20. n, m = w // part_w, h // part_h
  21. # fit best possible part_w and part_h
  22. part_w = int(w / n)
  23. part_h = int(h / m)
  24. for i in range(n*m):
  25. row, col = np.unravel_index(i, (n, m))
  26. x, y = col * part_w, row * part_h
  27. yield BBoxPart(im, [i, x, y, part_w, part_h])