Commit fca57746 authored by Yannick DAYER's avatar Yannick DAYER
Browse files

[py] Adapt to annotations wrappers

parent da6153af
Pipeline #44958 failed with stage
in 91 minutes and 49 seconds
......@@ -2,7 +2,6 @@
"""
import logging
import click
import json
import functools
from bob.extension.scripts.click_helper import (
verbosity_option,
......@@ -13,10 +12,6 @@ from bob.extension.scripts.click_helper import (
from bob.pipelines import wrap, ToDaskBag
logger = logging.getLogger(__name__)
def save_annotations_to_json(data, path):
with open(path, "w") as f:
json.dump(data, f)
def annotate_common_options(func):
@click.option(
"--annotator",
......@@ -96,15 +91,14 @@ def annotate(
log_parameters(logger)
# Allows passing of Sample objects as parameters
annotator = wrap(["sample"], annotator)
annotator = wrap(["annotated_sample"], annotator)
# Will save the annotations in the `data` fields to a json file
annotator = wrap(
bases=["checkpoint"],
estimator=annotator,
features_dir=output_dir,
save_func=save_annotations_to_json,
extension=".json",
bases=["checkpoint_annotations"],
annotator=annotator,
annotations_dir=output_dir,
force=force,
)
# Allows reception of Dask Bags
......@@ -159,6 +153,7 @@ You have to define ``samples`` in a python file (config.py) as in examples.
)
@click.option(
"--samples",
entry_point_group="bob.bio.config",
required=True,
cls=ResourceOption,
help="A list of all samples that you want to annotate.",
......@@ -166,7 +161,7 @@ You have to define ``samples`` in a python file (config.py) as in examples.
@annotate_common_options
@verbosity_option(cls=ResourceOption)
def annotate_samples(
samples, make_path, annotator, output_dir, force, dask_client, **kwargs
samples, annotator, output_dir, force, dask_client, **kwargs
):
"""Annotates a list of samples.
......@@ -176,15 +171,14 @@ def annotate_samples(
log_parameters(logger, ignore=("samples",))
# Allows passing of Sample objects as parameters
annotator = wrap(["sample"], annotator)
annotator = wrap(["annotated_sample"], annotator)
# Will save the annotations in the `data` fields to a json file
annotator = wrap(
bases=["checkpoint"],
estimator=annotator,
features_dir=output_dir,
save_func=save_annotations_to_json,
extension=".json",
bases=["checkpoint_annotations"],
annotator=annotator,
annotations_dir=output_dir,
force=force,
)
# Allows reception of Dask Bags
......
from bob.bio.base.test.dummy.database import database
samples = database.background_model_samples()
\ No newline at end of file
......@@ -2,7 +2,7 @@ import tempfile
import os
import shutil
from click.testing import CliRunner
from bob.bio.base.script.annotate import annotate
from bob.bio.base.script.annotate import annotate, annotate_samples
from bob.bio.base.annotator import Callable, FailSafe
from bob.db.base import read_annotation_file
......@@ -33,6 +33,31 @@ def test_annotate():
finally:
shutil.rmtree(tmp_dir)
def test_annotate_samples():
try:
tmp_dir = tempfile.mkdtemp(prefix="bobtest_")
runner = CliRunner()
result = runner.invoke(annotate_samples, args=(
'--samples','dummy_samples', '-a', 'dummy', '-o', tmp_dir))
assertion_error_message = (
'Command exited with this output: `{}\' \n'
'If the output is empty, you can run this script locally to see '
'what is wrong:\n'
'bin/bob bio annotate-samples -vvv --force --samples dummy -a dummy -o /tmp/temp_annotations'
''.format(result.output))
assert result.exit_code == 0, assertion_error_message
# test if annotations exist
for dirpath, dirnames, filenames in os.walk(tmp_dir):
for filename in filenames:
path = os.path.join(dirpath, filename)
annot = read_annotation_file(path, 'json')
assert annot['topleft'] == [0, 0]
# size of atnt images
assert annot['bottomright'] == [112, 92]
finally:
shutil.rmtree(tmp_dir)
def dummy_extra_key_annotator(data_batch, **kwargs):
return [{'leye': 0, 'reye': 0, 'topleft': 0}]
......
......@@ -76,6 +76,7 @@ setup(
'bob.bio.config': [
'dummy = bob.bio.base.test.dummy.config', # for test purposes only
'dummy2 = bob.bio.base.test.dummy.config2', # for test purposes only
'dummy_samples = bob.bio.base.test.dummy.samples_list', # for test purposes only
],
'bob.bio.database': [
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment