cars.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import numpy as np
  2. from os.path import join
  3. from nabirds.utils import _MetaInfo
  4. from .base import BaseAnnotations
  5. class CARS_Annotations(BaseAnnotations):
  6. name="CARS"
  7. @property
  8. def meta(self):
  9. info = _MetaInfo(
  10. images_folder="images",
  11. images_file="images.txt",
  12. labels_file="labels.txt",
  13. split_file="tr_ID.txt",
  14. bounding_boxes="bounding_boxes.txt",
  15. bounding_box_dtype=np.dtype([(v, np.int32) for v in "xywh"]),
  16. )
  17. info.structure = [
  18. [info.images_file, "_images"],
  19. [info.labels_file, "labels"],
  20. [info.split_file, "_split"],
  21. [info.bounding_boxes, "_bounding_boxes"],
  22. ]
  23. return info
  24. def __init__(self, *args, **kwargs):
  25. super(CARS_Annotations, self).__init__(*args, **kwargs)
  26. # set labels from [1..N] to [0..N-1]
  27. self.labels -= 1
  28. def _load_split(self):
  29. assert self._split is not None, "Train-test split was not loaded!"
  30. uuid_to_split = {uuid: int(split) for uuid, split in zip(self.uuids, self._split)}
  31. self.train_split = np.array([uuid_to_split[uuid] for uuid in self.uuids], dtype=bool)
  32. self.test_split = np.logical_not(self.train_split)
  33. def _load_parts(self):
  34. self.part_names = {}
  35. self._load_bounding_boxes()
  36. def _load_bounding_boxes(self):
  37. assert self._bounding_boxes is not None, "Bouding boxes were not loaded!"
  38. uuid_to_bbox = {}
  39. for content in [i.split() for i in self._bounding_boxes]:
  40. uuid, bbox = content[0], content[1:]
  41. uuid_to_bbox[uuid] = [float(i) for i in bbox]
  42. self.bounding_boxes = np.array(
  43. [tuple(uuid_to_bbox[uuid]) for uuid in self.uuids],
  44. dtype=self.meta.bounding_box_dtype)
  45. def parts(self, *args, **kwargs):
  46. return None
  47. def bounding_box(self, uuid):
  48. return self.bounding_boxes[self.uuid_to_idx[uuid]].copy()