annotate.py 2.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""Sorts a score file based on the scores!

Usage: %(prog)s [-v...] [options] [--] FILE...
       %(prog)s --help
       %(prog)s --version


Arguments:
    FILE   Path to the configuration file. It needs to contain  The sorting is
           done in place.

Options:
    -h, --help                  Shows this help message and exits
    -V, --version               Prints the version and exits
    -v, --verbose               Increases the output verbosity level
"""
from __future__ import division, print_function
from os.path import join, dirname, isfile
import json
from bob.core.log import setup, set_verbosity_level
from bob.io.base import create_directories_safe
from bob.bio.base import read_config_file

logger = setup(__name__)


def annotate(bio_files, load_data, output_dir, landmarks):
    logger.info("Processing %d files", len(bio_files))
    for i, f in enumerate(bio_files):
        logger.info("Processing file %d", i + 1)
34
        key = f.make_path("", "")
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
35
        outpath = join(output_dir, key + '.json')
36
37
38
        if isfile(outpath):
            logger.info("Skipping %s", outpath)
            continue
39
        frames, key = load_data(f)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        create_directories_safe(dirname(outpath))
        annotations = {}
        for frame_number, image, _ in frames:
            ldmarks = landmarks(image)
            if ldmarks is None:
                print('Failed for {} {}'.format(key, frame_number))
                continue
            annotations[frame_number] = ldmarks
        with open(outpath, 'wt') as f:
            json.dump(annotations, f)


def main():
    import os
    import sys
    from docopt import docopt
    import pkg_resources

    completions = dict(
        prog=os.path.basename(sys.argv[0]),
        version=pkg_resources.require(
61
            'bob.paper.btas2018_siliconemask')[0].version
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    )

    args = docopt(
        __doc__ % completions,
        version=completions['version'],
    )

    # Sets-up logging
    verbosity = int(args['--verbose'])
    set_verbosity_level(logger, verbosity)

    config = read_config_file(args['FILE'])
    annotate(config.bio_files, config.load_data, config.output_dir,
             config.landmarks)


if __name__ == '__main__':
    main()