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 @@ ...@@ -2,7 +2,6 @@
""" """
import logging import logging
import click import click
import json
import functools import functools
from bob.extension.scripts.click_helper import ( from bob.extension.scripts.click_helper import (
verbosity_option, verbosity_option,
...@@ -13,10 +12,6 @@ from bob.extension.scripts.click_helper import ( ...@@ -13,10 +12,6 @@ from bob.extension.scripts.click_helper import (
from bob.pipelines import wrap, ToDaskBag from bob.pipelines import wrap, ToDaskBag
logger = logging.getLogger(__name__) 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): def annotate_common_options(func):
@click.option( @click.option(
"--annotator", "--annotator",
...@@ -96,15 +91,14 @@ def annotate( ...@@ -96,15 +91,14 @@ def annotate(
log_parameters(logger) log_parameters(logger)
# Allows passing of Sample objects as parameters # 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 # Will save the annotations in the `data` fields to a json file
annotator = wrap( annotator = wrap(
bases=["checkpoint"], bases=["checkpoint_annotations"],
estimator=annotator, annotator=annotator,
features_dir=output_dir, annotations_dir=output_dir,
save_func=save_annotations_to_json, force=force,
extension=".json",
) )
# Allows reception of Dask Bags # Allows reception of Dask Bags
...@@ -159,6 +153,7 @@ You have to define ``samples`` in a python file (config.py) as in examples. ...@@ -159,6 +153,7 @@ You have to define ``samples`` in a python file (config.py) as in examples.
) )
@click.option( @click.option(
"--samples", "--samples",
entry_point_group="bob.bio.config",
required=True, required=True,
cls=ResourceOption, cls=ResourceOption,
help="A list of all samples that you want to annotate.", 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. ...@@ -166,7 +161,7 @@ You have to define ``samples`` in a python file (config.py) as in examples.
@annotate_common_options @annotate_common_options
@verbosity_option(cls=ResourceOption) @verbosity_option(cls=ResourceOption)
def annotate_samples( 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. """Annotates a list of samples.
...@@ -176,15 +171,14 @@ def annotate_samples( ...@@ -176,15 +171,14 @@ def annotate_samples(
log_parameters(logger, ignore=("samples",)) log_parameters(logger, ignore=("samples",))
# Allows passing of Sample objects as parameters # 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 # Will save the annotations in the `data` fields to a json file
annotator = wrap( annotator = wrap(
bases=["checkpoint"], bases=["checkpoint_annotations"],
estimator=annotator, annotator=annotator,
features_dir=output_dir, annotations_dir=output_dir,
save_func=save_annotations_to_json, force=force,
extension=".json",
) )
# Allows reception of Dask Bags # 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 ...@@ -2,7 +2,7 @@ import tempfile
import os import os
import shutil import shutil
from click.testing import CliRunner 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.bio.base.annotator import Callable, FailSafe
from bob.db.base import read_annotation_file from bob.db.base import read_annotation_file
...@@ -33,6 +33,31 @@ def test_annotate(): ...@@ -33,6 +33,31 @@ def test_annotate():
finally: finally:
shutil.rmtree(tmp_dir) 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): def dummy_extra_key_annotator(data_batch, **kwargs):
return [{'leye': 0, 'reye': 0, 'topleft': 0}] return [{'leye': 0, 'reye': 0, 'topleft': 0}]
......
...@@ -76,6 +76,7 @@ setup( ...@@ -76,6 +76,7 @@ setup(
'bob.bio.config': [ 'bob.bio.config': [
'dummy = bob.bio.base.test.dummy.config', # for test purposes only 'dummy = bob.bio.base.test.dummy.config', # for test purposes only
'dummy2 = bob.bio.base.test.dummy.config2', # 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': [ '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