reading.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from os.path import join
  2. from . import BaseMixin
  3. from ..image import ImageWrapper
  4. class AnnotationsReadMixin(BaseMixin):
  5. def __init__(self, uuids, annotations, mode="RGB"):
  6. super(AnnotationsReadMixin, self).__init__()
  7. self.uuids = uuids
  8. self._annot = annotations
  9. self.mode = mode
  10. def __len__(self):
  11. return len(self.uuids)
  12. def _get(self, method, i):
  13. return getattr(self._annot, method)(self.uuids[i])
  14. def get_example(self, i):
  15. res = super(AnnotationsReadMixin, self).get_example(i)
  16. # if the super class returns something, then the class inheritance is wrong
  17. assert res is None, "AnnotationsReadMixin should be the last class in the hierarchy!"
  18. methods = ["image", "parts", "label"]
  19. im_path, parts, label = [self._get(m, i) for m in methods]
  20. return ImageWrapper(im_path, int(label), parts, mode=self.mode)
  21. class ImageListReadingMixin(BaseMixin):
  22. def __init__(self, pairs, root="."):
  23. super(ImageListReadingMixin, self).__init__()
  24. with open(pairs) as f:
  25. self._pairs = [line.strip().split() for line in f]
  26. assert all([len(pair) == 2 for pair in self._pairs]), \
  27. "Invalid format of the pairs file!"
  28. self._root = root
  29. def __len__(self):
  30. return len(self._pairs)
  31. def get_example(self, i):
  32. im_file, label = self._pairs[i]
  33. im_path = join(self._root, im_file)
  34. return ImageWrapper(im_path, int(label))