driver.py 6.59 KB
Newer Older
1
2
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
3

4
5
6
7
8

"""Commands the AT&T database can respond to.
"""

import os
9
import sys
10
import pkg_resources
Manuel Günther's avatar
Manuel Günther committed
11
from bob.db.base.driver import Interface as BaseInterface
12
from bob.extension.download import download_and_unzip
13

14

15
16
17
18
19
20
def dumplist(args):
  """Dumps lists of files based on your criteria."""

  from .__init__ import Database
  db = Database()

21
22
23
24
25
  r = db.objects(
      groups=args.group,
      purposes=args.purpose,
      model_ids=args.client
  )
26
27
28

  output = sys.stdout
  if args.selftest:
Manuel Günther's avatar
Manuel Günther committed
29
    from bob.db.base.utils import null
30
31
    output = null()

32
33
  for f in r:
    output.write('%s\n' % f.make_path(directory=args.directory, extension=args.extension))
34
35
36

  return 0

37

38
def checkfiles(args):
39
40
  """Checks the existence of the files based on your criteria."""

41
  from . import Database
42
43
44
  db = Database()

  # go through all files, check if they are available
45
  bad = [f for f in db.objects() if not os.path.exists(f.make_path(directory=args.directory, extension=args.extension))]
46
47
48
49

  # report
  output = sys.stdout
  if args.selftest:
Manuel Günther's avatar
Manuel Günther committed
50
    from bob.db.base.utils import null
51
52
53
    output = null()

  if bad:
54
    for f in bad:
55
      output.write('Cannot find file "%s"\n' % f.make_path(directory=args.directory, extension=args.extension))
56
    output.write('%d files (out of %d) were not found at "%s"\n' % \
57
        (len(bad), len(db.objects()), args.directory))
58
59
60

  return 0

61
62
63
64
65
66
67
68
69

def reverse(args):
  """Returns a list of file database identifiers given the path stems"""

  from .query import Database
  db = Database()

  output = sys.stdout
  if args.selftest:
Manuel Günther's avatar
Manuel Günther committed
70
    from bob.db.base.utils import null
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    output = null()

  r = db.reverse(args.path)
  for f in r: output.write('%s\n' % f.id)

  if not r: return 1

  return 0


def path(args):
  """Returns a list of fully formed paths or stems given some file id"""

  from .query import Database
  db = Database()

  output = sys.stdout
  if args.selftest:
Manuel Günther's avatar
Manuel Günther committed
89
    from bob.db.base.utils import null
90
91
92
93
94
95
96
97
98
99
    output = null()

  r = db.paths(args.id, prefix=args.directory, suffix=args.extension)
  for path in r: output.write('%s\n' % path)

  if not r: return 1

  return 0


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def download(arguments):
  """Downloads and uncompresses the AT&T database"""

  """
  Parameters:

    arguments (argparse.Namespace): A set of arguments passed by the
      command-line parser


  Returns:

    int: A POSIX compliant return value of ``0`` if the download is successful,
    or ``1`` in case it is not.


  Raises:

    urllib2.HTTPError: if the target resource does not exist on the webserver

  """

122
123
124
125
126
127
128
  source_url = ['http://bobconda.lab.idiap.ch/public/data/bob/att_faces.zip',
                'http://www.idiap.ch/software/bob/data/bob/att_faces.zip'
               ]

  if not os.path.exists(arguments.output_dir):
    os.mkdir(arguments.output_dir)
    download_and_unzip(source_url, os.path.join(arguments.output_dir, "att_faces.zip"))
129
130
131

  return 0

132

133
class Interface(BaseInterface):
134

135
136
  def name(self):
    return 'atnt'
137

138
  def version(self):
Manuel Günther's avatar
Manuel Günther committed
139
    return pkg_resources.require('bob.db.%s' % self.name())[0].version
140

141
  def files(self):
142
    return ()
143
144
145
146
147
148
149

  def type(self):
    return 'python_integrated'

  def add_commands(self, parser):

    from . import __doc__ as docs
150

151
152
153
154
    subparsers = self.setup_parser(parser,
        "AT&T/ORL Face database", docs)

    from . import Database
155
156
    from .models import Client
    import argparse
157
158
159
160
161

    db = Database()

    # add the dumplist command
    dump_parser = subparsers.add_parser('dumplist', help="Dumps list of files based on your criteria")
162
163
164
165
166
167
    dump_parser.add_argument('-d', '--directory', default='', help="if given, this path will be prepended to every entry returned.")
    dump_parser.add_argument('-e', '--extension', default='', help="if given, this extension will be appended to every entry returned.")
    dump_parser.add_argument('-C', '--client', type=int, help="if given, limits the dump to a particular client.", choices=Client.m_valid_client_ids)
    dump_parser.add_argument('-g', '--group', help="if given, this value will limit the output files to those belonging to a particular group.", choices=db.m_groups)
    dump_parser.add_argument('-p', '--purpose', help="if given, this value will limit the output files to those belonging to a particular purpose.", choices=db.m_purposes)
    dump_parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
168
169
170
    dump_parser.set_defaults(func=dumplist) #action

    # add the checkfiles command
171
    from .models import DEFAULT_DATADIR
172
    check_parser = subparsers.add_parser('checkfiles', help="Check if the files exist, based on your criteria")
173
174
    check_parser.add_argument('-d', '--directory', default=DEFAULT_DATADIR, help="the path to the AT&T images [default: %(default)s]")
    check_parser.add_argument('-e', '--extension', default=".pgm", help="the extension of the AT&T images [default: %(default)s]")
175
    check_parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
176
177
    check_parser.set_defaults(func=checkfiles) #action

178
179
180
181
182
183
184
185
186
187
188
189
190
191
    # adds the "reverse" command
    parser = subparsers.add_parser('reverse', help=reverse.__doc__)
    parser.add_argument('path', nargs='+', help="one or more path stems to look up. If you provide more than one, files which cannot be reversed will be omitted from the output.")
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=reverse) #action

    # adds the "path" command
    parser = subparsers.add_parser('path', help=path.__doc__)
    parser.add_argument('-d', '--directory', help="if given, this path will be prepended to every entry returned.")
    parser.add_argument('-e', '--extension', help="if given, this extension will be appended to every entry returned.")
    parser.add_argument('id', type=int, nargs='+', help="one or more file ids to look up. If you provide more than one, files which cannot be found will be omitted from the output. If you provide a single id to lookup, an error message will be printed if the id does not exist in the database. The exit status will be non-zero in such case.")
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=path) #action

192
193
194
195
196
    # adds the "download" command
    parser = subparsers.add_parser('download', help=download.__doc__)
    parser.add_argument('-o', "--output-dir", help='the directory where to extract the AT&T database at [default: %(default)s]', default=DEFAULT_DATADIR)
    parser.add_argument('-q', "--quiet", action='store_true', help='if set, do it quietly', default=False)
    parser.set_defaults(func=download)