create.py 6.87 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Laurent El Shafey <Laurent.El-Shafey@idiap.ch>
# Fri 20 May 17:00:50 2011 

"""This script creates the BANCA database in a single pass.
"""

import os

from .models import *

def add_files(session, imagedir):
  """Add files (and clients) to the BANCA database."""
 
  def add_file(session, filename, client_dict):
    """Parse a single filename and add it to the list.
       Also add a client entry if not already in the database."""

    v = os.path.splitext(os.path.basename(filename))[0].split('_')
    if not (v[0] in client_dict):
      if (v[2] == 'wm'):
        v[2] = 'world'
      session.add(Client(int(v[0]), v[1], v[2], v[5]))
      client_dict[v[0]] = True
    session_id = int(v[3].split('s')[1])
    session.add(File(int(v[0]), os.path.basename(filename).split('.')[0], v[4], v[6], session_id))
  
  file_list = os.listdir(imagedir)
  client_dict = {} 
  for filename in file_list:
    add_file(session, os.path.join(imagedir, filename), client_dict)

def add_subworlds(session):
  """Adds splits in the world set, based on the client ids"""
  # one third
  session.add(Subworld("onethird", 9003))
  session.add(Subworld("onethird", 9005))
  session.add(Subworld("onethird", 9027))
  session.add(Subworld("onethird", 9033))
  session.add(Subworld("onethird", 9035))
  session.add(Subworld("onethird", 9043))
  session.add(Subworld("onethird", 9049))
  session.add(Subworld("onethird", 9053))
  session.add(Subworld("onethird", 9055))
  session.add(Subworld("onethird", 9057))
  
  # two thirds
  session.add(Subworld("twothirds", 9001))
  session.add(Subworld("twothirds", 9007))
  session.add(Subworld("twothirds", 9009))
  session.add(Subworld("twothirds", 9011))
  session.add(Subworld("twothirds", 9013))
  session.add(Subworld("twothirds", 9015))
  session.add(Subworld("twothirds", 9017))
  session.add(Subworld("twothirds", 9019))
  session.add(Subworld("twothirds", 9021))
  session.add(Subworld("twothirds", 9023))
  session.add(Subworld("twothirds", 9025))
  session.add(Subworld("twothirds", 9029))
  session.add(Subworld("twothirds", 9031))
  session.add(Subworld("twothirds", 9037))
  session.add(Subworld("twothirds", 9039))
  session.add(Subworld("twothirds", 9041))
  session.add(Subworld("twothirds", 9045))
  session.add(Subworld("twothirds", 9047))
  session.add(Subworld("twothirds", 9051))
  session.add(Subworld("twothirds", 9059))

def add_sessions(session):
  """Adds relations between sessions and scenarios"""

  for i in range(1,5):
    session.add(Session(i,'controlled'))
  for i in range(5,9):
    session.add(Session(i,'degraded'))
  for i in range(9,13):
    session.add(Session(i,'adverse'))

def add_protocols(session):
  """Adds protocols"""
  # Protocol Mc
  session.add(Protocol(1, 'Mc', 'enrol'))
  session.add(Protocol(1, 'Mc', 'probeImpostor'))
  session.add(Protocol(2, 'Mc', 'probe'))
  session.add(Protocol(3, 'Mc', 'probe'))
  session.add(Protocol(4, 'Mc', 'probe'))
  
  # Protocol Md
  session.add(Protocol(5, 'Md', 'enrol'))
  session.add(Protocol(5, 'Md', 'probeImpostor'))
  session.add(Protocol(6, 'Md', 'probe'))
  session.add(Protocol(7, 'Md', 'probe'))
  session.add(Protocol(8, 'Md', 'probe'))
  
  # Protocol Ma
  session.add(Protocol(9, 'Ma', 'enrol'))
  session.add(Protocol(9, 'Ma', 'probeImpostor'))
  session.add(Protocol(10, 'Ma', 'probe'))
  session.add(Protocol(11, 'Ma', 'probe'))
  session.add(Protocol(12, 'Ma', 'probe'))
  
  # Protocol Ud
  session.add(Protocol(1, 'Ud', 'enrol'))
  session.add(Protocol(5, 'Ud', 'probeImpostor'))
  session.add(Protocol(6, 'Ud', 'probe'))
  session.add(Protocol(7, 'Ud', 'probe'))
  session.add(Protocol(8, 'Ud', 'probe'))
  
  # Protocol Ma
  session.add(Protocol(1, 'Ua', 'enrol'))
  session.add(Protocol(9, 'Ua', 'probeImpostor'))
  session.add(Protocol(10, 'Ua', 'probe'))
  session.add(Protocol(11, 'Ua', 'probe'))
  session.add(Protocol(12, 'Ua', 'probe'))
  
  # Protocol P
  session.add(Protocol(1, 'P', 'enrol'))
  session.add(Protocol(1, 'P', 'probeImpostor'))
  session.add(Protocol(2, 'P', 'probe'))
  session.add(Protocol(3, 'P', 'probe'))
  session.add(Protocol(4, 'P', 'probe'))
  session.add(Protocol(5, 'P', 'probeImpostor'))
  session.add(Protocol(6, 'P', 'probe'))
  session.add(Protocol(7, 'P', 'probe'))
  session.add(Protocol(8, 'P', 'probe'))
  session.add(Protocol(9, 'P', 'probeImpostor'))
  session.add(Protocol(10, 'P', 'probe'))
  session.add(Protocol(11, 'P', 'probe'))
  session.add(Protocol(12, 'P', 'probe'))
  
  # Protocol G
  session.add(Protocol(1, 'G', 'enrol'))
  session.add(Protocol(5, 'G', 'enrol'))
  session.add(Protocol(9, 'G', 'enrol'))
  session.add(Protocol(1, 'G', 'probeImpostor'))
  session.add(Protocol(2, 'G', 'probe'))
  session.add(Protocol(3, 'G', 'probe'))
  session.add(Protocol(4, 'G', 'probe'))
  session.add(Protocol(5, 'G', 'probeImpostor'))
  session.add(Protocol(6, 'G', 'probe'))
  session.add(Protocol(7, 'G', 'probe'))
  session.add(Protocol(8, 'G', 'probe'))
  session.add(Protocol(9, 'G', 'probeImpostor'))
  session.add(Protocol(10, 'G', 'probe'))
  session.add(Protocol(11, 'G', 'probe'))
  session.add(Protocol(12, 'G', 'probe'))


def create_tables(args):
  """Creates all necessary tables (only to be used at the first time)"""

  from bob.db.utils import connection_string

  from sqlalchemy import create_engine
André Anjos's avatar
André Anjos committed
156
157
  engine = create_engine(connection_string(args.type, args.files[0]), 
      echo=(args.verbose >= 2))
158
159
160
161
162
163
164
165
166
167
168
169
  File.metadata.create_all(engine)
  Client.metadata.create_all(engine)
  Subworld.metadata.create_all(engine)
  Session.metadata.create_all(engine)
  Protocol.metadata.create_all(engine)

# Driver API
# ==========

def create(args):
  """Creates or re-creates this database"""

170
  from bob.db.utils import session_try_nolock
171
172
173
174
175
176
177
178
179
180
181
182
183

  dbfile = args.files[0]

  if args.recreate: 
    if args.verbose and os.path.exists(dbfile):
      print('unlinking %s...' % dbfile)
    if os.path.exists(dbfile): os.unlink(dbfile)

  if not os.path.exists(os.path.dirname(dbfile)):
    os.makedirs(os.path.dirname(dbfile))

  # the real work...
  create_tables(args)
184
  s = session_try_nolock(args.type, args.files[0], echo=(args.verbose >= 2))
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  add_files(s, args.imagedir)
  add_subworlds(s)
  add_sessions(s)
  add_protocols(s)
  s.commit()
  s.close()

def add_command(subparsers):
  """Add specific subcommands that the action "create" can use"""

  parser = subparsers.add_parser('create', help=create.__doc__)

  parser.add_argument('-R', '--recreate', action='store_true', default=False,
      help="If set, I'll first erase the current database")
André Anjos's avatar
André Anjos committed
199
  parser.add_argument('-v', '--verbose', action='count',
200
      help="Do SQL operations in a verbose way")
201
  parser.add_argument('-D', '--imagedir', action='store', metavar='DIR',
202
203
204
205
      default='/idiap/group/vision/visidiap/databases/banca/english/images_gray',
      help="Change the relative path to the directory containing the images of the BANCA database (defaults to %(default)s)")
  
  parser.set_defaults(func=create) #action