def _load_polygons(self, items): polygons_dir = osp.join(self._annotations_dir, MapillaryVistasPath.POLYGON_DIR) for item_path in glob.glob(osp.join(polygons_dir, '**', '*.json'), recursive=True): item_id = osp.splitext(osp.relpath(item_path, polygons_dir))[0] item = items.get(item_id) item_info = {} item_info = parse_json_file(item_path) image_size = self._get_image_size(item_info) if image_size and item.has_image: item.image = Image(path=item.image.path, size=image_size) polygons = item_info['objects'] annotations = [] for polygon in polygons: label = polygon['label'] label_id = self._categories[AnnotationType.label].find(label)[0] if label_id is None: label_id = self._categories[AnnotationType.label].add(label) points = [coord for point in polygon['polygon'] for coord in point] annotations.append(Polygon(label=label_id, points=points)) if item is None: items[item_id] = DatasetItem(id=item_id, subset=self._subset, annotations=annotations) else: item.annotations.extend(annotations)
def parse_meta_file(path): # Uses custom format with extra fields meta_file = path if osp.isdir(path): meta_file = get_meta_file(path) dataset_meta = parse_json_file(meta_file) label_map = OrderedDict() parts = dataset_meta.get('parts', {}) actions = dataset_meta.get('actions', {}) for i, label in enumerate(dataset_meta.get('labels', [])): label_map[label] = [ None, parts.get(str(i), []), actions.get(str(i), []) ] colors = dataset_meta.get('segmentation_colors', []) for i, label in enumerate(dataset_meta.get('label_map', {}).values()): if label not in label_map: label_map[label] = [None, [], []] if any(colors) and colors[i] is not None: label_map[label][0] = tuple(colors[i]) return label_map
def __init__(self, path): assert osp.isfile(path), path rootpath = '' if path.endswith( osp.join(DatumaroPath.ANNOTATIONS_DIR, osp.basename(path))): rootpath = path.rsplit(DatumaroPath.ANNOTATIONS_DIR, maxsplit=1)[0] images_dir = '' if rootpath and osp.isdir(osp.join(rootpath, DatumaroPath.IMAGES_DIR)): images_dir = osp.join(rootpath, DatumaroPath.IMAGES_DIR) self._images_dir = images_dir pcd_dir = '' if rootpath and osp.isdir(osp.join(rootpath, DatumaroPath.PCD_DIR)): pcd_dir = osp.join(rootpath, DatumaroPath.PCD_DIR) self._pcd_dir = pcd_dir related_images_dir = '' if rootpath and osp.isdir( osp.join(rootpath, DatumaroPath.RELATED_IMAGES_DIR)): related_images_dir = osp.join(rootpath, DatumaroPath.RELATED_IMAGES_DIR) self._related_images_dir = related_images_dir super().__init__(subset=osp.splitext(osp.basename(path))[0]) parsed_anns = parse_json_file(path) self._categories = self._load_categories(parsed_anns) self._items = self._load_items(parsed_anns)
def _load_panoptic_config(path): panoptic_config_path = osp.join(path, MapillaryVistasPath.PANOPTIC_DIR, MapillaryVistasPath.PANOPTIC_CONFIG) if not osp.isfile(panoptic_config_path): raise FileNotFoundError("Can't find panoptic config file: '%s' at '%s'" % (MapillaryVistasPath.PANOPTIC_CONFIG, panoptic_config_path)) return parse_json_file(panoptic_config_path)
def _load_items(self, path): anno_dict = parse_json_file(path) label_categories = self._categories[AnnotationType.label] tags = anno_dict.get('tags', []) for label in tags: label_name = label.get('name') label_idx = label_categories.find(label_name)[0] if label_idx is None: label_idx = label_categories.add(label_name) items = {} for id, asset in anno_dict.get('assets', {}).items(): item_id = osp.splitext(asset.get('asset', {}).get('name'))[0] annotations = [] for region in asset.get('regions', []): tags = region.get('tags', []) if not tags: bbox = region.get('boundingBox', {}) if bbox: annotations.append( Bbox(float(bbox['left']), float(bbox['top']), float(bbox['width']), float(bbox['height']), attributes={'id': region.get('id')})) for tag in region.get('tags', []): label_idx = label_categories.find(tag)[0] if label_idx is None: label_idx = label_categories.add(tag) bbox = region.get('boundingBox', {}) if bbox: annotations.append( Bbox(float(bbox['left']), float(bbox['top']), float(bbox['width']), float(bbox['height']), label=label_idx, attributes={'id': region.get('id')})) items[item_id] = DatasetItem( id=item_id, subset=self._subset, attributes={'id': id}, image=Image(path=osp.join(osp.dirname(path), asset.get('asset', {}).get('path'))), annotations=annotations) return items
def __init__(self, path, task, *, merge_instance_polygons=False, subset=None, keep_original_category_ids=False, **kwargs): assert osp.isfile(path), path if not subset: parts = osp.splitext(osp.basename(path))[0].split(task.name + '_', maxsplit=1) subset = parts[1] if len(parts) == 2 else None super().__init__(subset=subset, **kwargs) rootpath = '' if path.endswith(osp.join(CocoPath.ANNOTATIONS_DIR, osp.basename(path))): rootpath = path.rsplit(CocoPath.ANNOTATIONS_DIR, maxsplit=1)[0] images_dir = '' if rootpath and osp.isdir(osp.join(rootpath, CocoPath.IMAGES_DIR)): images_dir = osp.join(rootpath, CocoPath.IMAGES_DIR) if osp.isdir(osp.join(images_dir, subset or DEFAULT_SUBSET_NAME)): images_dir = osp.join(images_dir, subset or DEFAULT_SUBSET_NAME) self._images_dir = images_dir self._task = task self._rootpath = rootpath self._merge_instance_polygons = merge_instance_polygons json_data = parse_json_file(path) self._label_map = {} # coco_id -> dm_id self._load_categories( json_data, keep_original_ids=keep_original_category_ids, ) if self._task == CocoTask.panoptic: self._mask_dir = osp.splitext(path)[0] self._items = self._load_items(json_data)
def parse_meta_file(path): meta_file = path if osp.isdir(path): meta_file = get_meta_file(path) dataset_meta = parse_json_file(meta_file) label_map = OrderedDict() for label in dataset_meta.get('labels', []): label_map[label] = None colors = dataset_meta.get('segmentation_colors', []) for i, label in dataset_meta.get('label_map', {}).items(): label_map[label] = None if any(colors) and colors[int(i)] is not None: label_map[label] = tuple(colors[int(i)]) return label_map
def _load_label_category_parents(self): label_categories = self._categories[AnnotationType.label] hierarchy_path = osp.join(self._dataset_dir, OpenImagesPath.ANNOTATIONS_DIR, OpenImagesPath.HIERARCHY_FILE_NAME) try: root_node = parse_json_file(hierarchy_path) except FileNotFoundError: return def set_parents_from_node(node, category): for child_node in node.get('Subcategory', []): _, child_category = label_categories.find(child_node['LabelName']) if category is not None and child_category is not None: child_category.parent = category.name set_parents_from_node(child_node, child_category) _, root_category = label_categories.find(root_node['LabelName']) set_parents_from_node(root_node, root_category)
def _parse(cls, rootpath): mapping = parse_json_file( osp.join(rootpath, PointCloudPath.KEY_ID_FILE)) meta = parse_json_file(osp.join(rootpath, PointCloudPath.META_FILE)) label_cat = LabelCategories() for label in meta.get('classes', []): label_cat.add(label['title']) tags = {} for tag in meta.get('tags', []): # See reference at: # https://github.com/supervisely/supervisely/blob/047e52ebe407cfee61464c1bd0beb9c906892253/supervisely_lib/annotation/tag_meta.py#L139 tags[tag['name']] = tag applicable_to = tag.get('applicable_type', 'all') if applicable_to == 'imagesOnly': continue # an image attribute elif applicable_to not in {'all', 'objectsOnly'}: raise Exception("Unexpected tag 'applicable_type' value '%s'" % \ applicable_to) applicable_classes = tag.get('classes', []) if not applicable_classes: label_cat.attributes.add(tag['name']) else: for label_name in applicable_classes: _, label = label_cat.find(label_name) if label is None: raise Exception("Unknown class for tag '%s'" % \ label_name) label.attributes.add(tag['name']) categories = {AnnotationType.label: label_cat} def _get_label_attrs(label_id): attrs = set(label_cat.attributes) attrs.update(label_cat[label_id].attributes) return attrs def _parse_tag(tag): if tag['value'] == 'true': value = True elif tag['value'] == 'false': value = False else: value = tag['value'] return value ann_dir = osp.join(rootpath, PointCloudPath.BASE_DIR, PointCloudPath.ANNNOTATION_DIR) items = {} for ann_file in iglob(osp.join(ann_dir, '**', '*.json'), recursive=True): ann_data = parse_json_file(ann_file) objects = {} for obj in ann_data['objects']: obj['id'] = mapping['objects'][obj['key']] objects[obj['key']] = obj frame_attributes = {'description': ann_data.get('description', '')} for tag in ann_data['tags']: frame_attributes[tag['name']] = _parse_tag(tag) frame = mapping['videos'][ann_data['key']] frame_desc = items.setdefault( frame, { 'name': osp.splitext(osp.relpath(ann_file, ann_dir))[0], 'annotations': [], 'attributes': frame_attributes, }) for figure in ann_data['figures']: geometry = { dst_field: [ float(figure['geometry'][src_field][axis]) for axis in ['x', 'y', 'z'] ] for src_field, dst_field in { 'position': 'position', 'rotation': 'rotation', 'dimensions': 'scale' }.items() } ann_id = mapping['figures'][figure['key']] obj = objects[figure['objectKey']] label = categories[AnnotationType.label].find( obj['classTitle'])[0] attributes = {} attributes['track_id'] = obj['id'] for tag in obj.get('tags', []): attributes[tag['name']] = _parse_tag(tag) for attr in _get_label_attrs(label): if attr in attributes: continue if tags[attr]['value_type'] == 'any_string': value = '' elif tags[attr]['value_type'] == 'oneof_string': value = (tags[attr]['values'] or [''])[0] elif tags[attr]['value_type'] == 'any_number': value = 0 else: value = None attributes[attr] = value shape = Cuboid3d(**geometry, label=label, id=ann_id, attributes=attributes) frame_desc['annotations'].append(shape) return items, categories
def parse_config_file(config_path): label_map = OrderedDict() config = parse_json_file(config_path) for label in config['labels']: label_map[label['name']] = tuple(map(int, label['color'])) return label_map
def _load_items(self, path): items = {} root_dir = osp.dirname(path) hb_path = osp.join(root_dir, MpiiJsonPath.HEADBOXES_FILE) if osp.isfile(hb_path): headboxes = np.load(hb_path) else: headboxes = np.array([[[]]]) vis_path = osp.join(root_dir, MpiiJsonPath.VISIBILITY_FILE) if osp.isfile(vis_path): visibility = np.load(vis_path).T else: visibility = np.array([]) pos_gt_path = osp.join(root_dir, MpiiJsonPath.POS_GT_FILE) if osp.isfile(pos_gt_path): gt_pose = np.transpose(np.load(pos_gt_path), (2, 0, 1)) else: gt_pose = np.array([]) for i, ann in enumerate(parse_json_file(path)): item_id = osp.splitext(ann.get('img_paths', ''))[0] center = ann.get('objpos', []) scale = float(ann.get('scale_provided', 0)) if i < gt_pose.shape[0]: points = gt_pose[i].ravel() if i < visibility.shape[0]: vis = visibility[i] else: vis = np.ones(len(points) // 2, dtype=np.int8) else: keypoints = np.array(ann.get('joint_self', [])) points = keypoints[:, 0:2].ravel() vis = keypoints[:, 2] if i < visibility.shape[0]: vis = visibility[i] vis = [int(val) for val in vis] group_num = 1 annotations = [ Points(points, vis, label=0, group=group_num, attributes={ 'center': center, 'scale': scale }) ] if i < headboxes.shape[2]: bbox = headboxes[:, :, i] annotations.append( Bbox(bbox[0][0], bbox[0][1], bbox[1][0] - bbox[0][0], bbox[1][1] - bbox[0][1], label=0, group=group_num)) group_num += 1 joint_others = ann.get('joint_others') if joint_others: num_others = int(ann.get('numOtherPeople', 1)) center = ann.get('objpos_other', []) scale = ann.get('scale_provided_other', 0) if num_others == 1: center = [center] scale = [scale] joint_others = [joint_others] for i in range(num_others): keypoints = np.array(joint_others[i]) points = keypoints[:, 0:2].ravel() vis = keypoints[:, 2] vis = [int(val) for val in vis] attributes = {} if i < len(center): attributes['center'] = center[i] if i < len(scale): attributes['scale'] = scale[i] annotations.append( Points(points, vis, label=0, group=group_num, attributes=attributes)) group_num += 1 items[item_id] = DatasetItem( id=item_id, subset=self._subset, image=Image(path=osp.join(root_dir, ann.get('img_paths', ''))), annotations=annotations) return items