def extract_command(args): project = load_project(args.project_dir) if not args.dry_run: dst_dir = args.dst_dir if dst_dir: if not args.overwrite and osp.isdir(dst_dir) and os.listdir(dst_dir): raise CliException("Directory '%s' already exists " "(pass --overwrite to overwrite)" % dst_dir) else: dst_dir = generate_next_file_name('%s-filter' % \ project.config.project_name) dst_dir = osp.abspath(dst_dir) dataset = project.make_dataset() filter_args = FilterModes.make_filter_args(args.mode) if args.dry_run: dataset = dataset.extract(filter_expr=args.filter, **filter_args) for item in dataset: encoded_item = DatasetItemEncoder.encode(item, dataset.categories()) xml_item = DatasetItemEncoder.to_string(encoded_item) print(xml_item) return 0 if not args.filter: raise CliException("Expected a filter expression ('-e' argument)") dataset.extract_project(save_dir=dst_dir, filter_expr=args.filter, **filter_args) log.info("Subproject has been extracted to '%s'" % dst_dir) return 0
def test_item_representations(): item = DatasetItem(id=1, subset='subset', path=['a', 'b'], image=np.ones((5, 4, 3)), annotations=[ Label(0, attributes={'a1': 1, 'a2': '2'}, id=1, group=2), Caption('hello', id=1), Caption('world', group=5), Label(2, id=3, attributes={ 'x': 1, 'y': '2' }), Bbox(1, 2, 3, 4, label=4, id=4, attributes={ 'a': 1.0 }), Bbox(5, 6, 7, 8, id=5, group=5), Points([1, 2, 2, 0, 1, 1], label=0, id=5), Mask(id=5, image=np.ones((3, 2))), Mask(label=3, id=5, image=np.ones((2, 3))), PolyLine([1, 2, 3, 4, 5, 6, 7, 8], id=11), Polygon([1, 2, 3, 4, 5, 6, 7, 8]), ] ) encoded = DatasetItemEncoder.encode(item) DatasetItemEncoder.to_string(encoded)
def filter_command(args): project = None try: project = scope_add(load_project(args.project_dir)) except ProjectNotFoundError: if args.project_dir: raise filter_args = FilterModes.make_filter_args(args.mode) filter_expr = args.filter if args.dry_run: dataset, _project = parse_full_revpath(args.target, project) if _project: scope_add(_project) dataset = dataset.filter(expr=filter_expr, **filter_args) for item in dataset: encoded_item = DatasetItemEncoder.encode(item, dataset.categories()) xml_item = DatasetItemEncoder.to_string(encoded_item) print(xml_item) return 0 if not args.filter: raise CliException("Expected a filter expression ('-e' argument)") is_target = project is not None and \ args.target in project.working_tree.build_targets if is_target: if not args.dst_dir and args.stage and (args.target != \ ProjectBuildTargets.strip_target_name(args.target)): raise CliException("Adding a stage is only allowed for " "project targets, not their stages.") if args.target == ProjectBuildTargets.MAIN_TARGET: targets = list(project.working_tree.sources) else: targets = [args.target] build_tree = project.working_tree.clone() for target in targets: build_tree.build_targets.add_filter_stage(target, expr=filter_expr, params=filter_args) if args.apply: log.info("Filtering...") if is_target and not args.dst_dir: for target in targets: dataset = project.working_tree.make_dataset( build_tree.make_pipeline(target)) # Source might be missing in the working dir, so we specify # the output directory. # We specify save_images here as a heuristic. It can probably # be improved by checking if there are images in the dataset # directory. dataset.save(project.source_data_dir(target), save_images=True) log.info("Finished") else: dataset, _project = parse_full_revpath(args.target, project) if _project: scope_add(_project) dst_dir = args.dst_dir or dataset.data_path if not args.overwrite and osp.isdir(dst_dir) and os.listdir( dst_dir): raise CliException("Directory '%s' already exists " "(pass --overwrite to overwrite)" % dst_dir) dst_dir = osp.abspath(dst_dir) dataset.filter(filter_expr, *filter_args) dataset.save(dst_dir, save_images=True) log.info("Results have been saved to '%s'" % dst_dir) if is_target and args.stage: project.working_tree.config.update(build_tree.config) project.working_tree.save() return 0