def get_annotations(self): """ Returns the 'annotations' key of the COCO dataset annotation """ if not self._annotations: ann_id = 1 ret = [] label_to_category_id_map = self.get_label_to_category_id_map() for f in self.list_annotations(): data = util.load_json(f) for i, shape in enumerate(data['shapes']): if i % 2 == 0: d = { 'area': None, 'bbox': self.get_annotation_bbox(shape['points']), 'category_id': label_to_category_id_map[shape['label']], 'id': ann_id, 'image_id': self.get_image_id(f), 'iscrowd': 0, } else: # every 2nd loop the annotation is complete, so add it # to the return and increment the annotation id d['segmentation'] = self.get_segmentation( shape['points']) ret.append(d) ann_id += 1 self._annotations = ret return self._annotations
def get_image_ann(self, path): """ Takes the path of a "labelme" style annotation and returns a COCO dataset syle image annotation """ ann = util.load_json(path) filename, _ = os.path.splitext(os.path.basename(ann['imagePath'])) return { 'id': int(filename), 'height': ann['imageHeight'], 'width': ann['imageWidth'], 'file_name': os.path.basename(ann['imagePath']) }
def test_generate(self): # rm file file if present output_path = os.path.join(config.OUTPUT_DIR, 'annotations.json') if os.path.isfile(output_path): os.remove(output_path) assert not os.path.isfile(output_path) self.ann.generate(output_path) assert os.path.isfile(output_path) data = util.load_json(output_path) assert data == self.ann.all() # cleanup file when done os.remove(output_path)
def get_categories(self): """ Returns the 'categories' key of the COCO dataset annotation """ if not self._categories: cats = {} i = 1 for file in self.list_annotations(): data = util.load_json(file) for shape in data['shapes']: label = shape['label'] if label not in cats: cats[label] = { 'id': i, 'supercategory': label, 'name': label } i += 1 self._categories = list(cats.values()) return self._categories
def test_load_json(self): ret = util.load_json(os.path.join(config.ANNOTATIONS_DIR, '1.json')) assert isinstance(ret, dict)