COCO dataset class.
| 9 | |
| 10 | |
| 11 | class MOTDataset(Dataset): |
| 12 | """ |
| 13 | COCO dataset class. |
| 14 | """ |
| 15 | |
| 16 | def __init__( |
| 17 | self, |
| 18 | data_dir=None, |
| 19 | json_file="train_half.json", |
| 20 | name="train", |
| 21 | img_size=(608, 1088), |
| 22 | preproc=None, |
| 23 | ): |
| 24 | """ |
| 25 | COCO dataset initialization. Annotation data are read into memory by COCO API. |
| 26 | Args: |
| 27 | data_dir (str): dataset root directory |
| 28 | json_file (str): COCO json file name |
| 29 | name (str): COCO data name (e.g. 'train2017' or 'val2017') |
| 30 | img_size (int): target image size after pre-processing |
| 31 | preproc: data augmentation strategy |
| 32 | """ |
| 33 | super().__init__(img_size) |
| 34 | if data_dir is None: |
| 35 | data_dir = os.path.join(get_yolox_datadir(), "mot") |
| 36 | self.data_dir = data_dir |
| 37 | self.json_file = json_file |
| 38 | |
| 39 | self.coco = COCO(os.path.join(self.data_dir, "annotations", self.json_file)) |
| 40 | self.ids = self.coco.getImgIds() |
| 41 | self.class_ids = sorted(self.coco.getCatIds()) |
| 42 | cats = self.coco.loadCats(self.coco.getCatIds()) |
| 43 | self._classes = tuple([c["name"] for c in cats]) |
| 44 | self.annotations = self._load_coco_annotations() |
| 45 | self.name = name |
| 46 | self.img_size = img_size |
| 47 | self.preproc = preproc |
| 48 | |
| 49 | def __len__(self): |
| 50 | return len(self.ids) |
| 51 | |
| 52 | def _load_coco_annotations(self): |
| 53 | return [self.load_anno_from_ids(_ids) for _ids in self.ids] |
| 54 | |
| 55 | def load_anno_from_ids(self, id_): |
| 56 | im_ann = self.coco.loadImgs(id_)[0] |
| 57 | width = im_ann["width"] |
| 58 | height = im_ann["height"] |
| 59 | frame_id = im_ann["frame_id"] |
| 60 | video_id = im_ann["video_id"] |
| 61 | anno_ids = self.coco.getAnnIds(imgIds=[int(id_)], iscrowd=False) |
| 62 | annotations = self.coco.loadAnns(anno_ids) |
| 63 | objs = [] |
| 64 | for obj in annotations: |
| 65 | x1 = obj["bbox"][0] |
| 66 | y1 = obj["bbox"][1] |
| 67 | x2 = x1 + obj["bbox"][2] |
| 68 | y2 = y1 + obj["bbox"][3] |
no outgoing calls
no test coverage detected