query.py 6.99 KB
Newer Older
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
1
2
3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
4
# Tue Aug 14 14:28:00 CEST 2015
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# Copyright (C) 2012-2014 Idiap Research Institute, Martigny, Switzerland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

20
21
22
23
import os
import six
from bob.db.base import utils
from .models import *
24
25
from .models import PROTOCOLS, GROUPS, PURPOSES

26
27
28
29
30
31
32
33
from .driver import Interface

import bob.db.verification.utils

SQLITE_FILE = Interface().files()[0]

class Database(bob.db.verification.utils.SQLiteDatabase, bob.db.verification.utils.ZTDatabase):

34
  """Wrapper class for the CUHK-CUFS database for Heterogeneous face recognition recognition (http://mmlab.ie.cuhk_cufs.edu.hk/archive/facesketch.html).
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
35
36
37

  """

38
  def __init__(self, original_directory = None, original_extension = None, arface_directory="", xm2vts_directory=""):
39
40
41
42
    # call base class constructors to open a session to the database
    bob.db.verification.utils.SQLiteDatabase.__init__(self, SQLITE_FILE, File)
    bob.db.verification.utils.ZTDatabase.__init__(self, original_directory=original_directory, original_extension=original_extension)

43
44
45
    self.arface_directory = arface_directory
    self.xm2vts_directory = xm2vts_directory

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
46
47
48
49
50
51
52
  
  def protocols(self):
    return PROTOCOLS

  def purposes(self):
    return PURPOSES

53

54
55
56
57
58
59
  def original_file_name(self, file, check_existence = True):
    """This function returns the original file name for the given File object.
    Keyword parameters:
    file : :py:class:`File` or a derivative
      The File objects for which the file name should be retrieved
    check_existence : bool
60
      Check if the original file exists? IGNORED: ALWAYS CHECK
61
62
63
64
    Return value : str
      The original file name for the given File object
    """

65
    # check if directory is set
66
    original_directory = self.original_directory
67
68
    if file.modality=="photo": 
      if file.client.original_database=="xm2vts":
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
69
        original_directory = self.xm2vts_directory
70
      elif file.client.original_database=="arface":
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
71
        original_directory = self.arface_directory 
72
73
74
75
    

    if not original_directory or not self.original_extension:
      raise ValueError("The original_directory and/or the original_extension were not specified in the constructor.")
76

77
    # extract file name
78
    file_name=""
79
80
81
    if type(self.original_extension) is list:
      for e in self.original_extension:
        file_name = file.make_path(original_directory, e)
82
        if os.path.exists(file_name):
83
84
85
86
87
          return file_name
    else:
      file_name = file.make_path(original_directory, self.original_extension)
      if os.path.exists(file_name):
        return file_name      
88
    
89
    raise ValueError("The file '%s' was not found. Please check the original directory '%s' and extension '%s'?" % (file_name, original_directory, self.original_extension))
90
91


92
93
94
95
96
97
98
99
100
101
102
103
  def annotations(self, file, annotation_type="eyes_center"):
    """This function returns the annotations for the given file id as a dictionary.
    Keyword parameters:
    file : :py:class:`bob.db.verification.utils.File` or one of its derivatives
      The File object you want to retrieve the annotations for,
    Return value:
      A dictionary of annotations, for face images usually something like {'leye':(le_y,le_x), 'reye':(re_y,re_x), ...},
      or None if there are no annotations for the given file ID (which is the case in this base class implementation).
    """    
    return file.annotations(annotation_type=annotation_type)


104
  def objects(self, groups = None, protocol = None, purposes = None, model_ids = None, **kwargs):
105
106
    """
      This function returns lists of File objects, which fulfill the given restrictions.
107

108
    """
109

110
111
112
113
114
115
116
117
118
119
    #Checking inputs
    groups    = self.check_parameters_for_validity(groups, "group", GROUPS)
    protocols = self.check_parameters_for_validity(protocol, "protocol", PROTOCOLS) 
    purposes  = self.check_parameters_for_validity(purposes, "purpose", PURPOSES)

    #You need to select only one protocol
    if (len(protocols) > 1):
      raise ValueError("Please, select only one of the following protocols {0}".format(protocols))
 
    #Querying
120
    query = self.query(bob.db.cuhk_cufs.File, bob.db.cuhk_cufs.Protocol_File_Association).join(bob.db.cuhk_cufs.Protocol_File_Association).join(bob.db.cuhk_cufs.Client)
121
122

    #filtering
123
124
125
    query = query.filter(bob.db.cuhk_cufs.Protocol_File_Association.group.in_(groups))
    query = query.filter(bob.db.cuhk_cufs.Protocol_File_Association.protocol.in_(protocols))
    query = query.filter(bob.db.cuhk_cufs.Protocol_File_Association.purpose.in_(purposes))
126
127

    if model_ids is not None:     
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
128
129
130
131
132
133
134
135
136
137
138
      if type(model_ids) is not list and type(model_ids) is not tuple:
        model_ids = [model_ids]
     
      #if you provide a client object as input and not the ids    
      if type(model_ids[0]) is bob.db.cuhk_cufs.Client:
        model_aux = []
        for m in model_ids:
          model_aux.append(m.id)
        model_ids = model_aux       

      query = query.filter(bob.db.cuhk_cufs.Client.id.in_(model_ids))
139

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
140
141
142
143
144
145
146
147
148
    raw_files = query.all()
    files     = []
    for f in raw_files:
      f[0].group    = f[1].group
      f[0].purpose  = f[1].purpose
      f[0].protocol = f[1].protocol
      files.append(f[0])

    return files
149
150
151
152
153
154
155
156
157
158
159
160
161
    

  def model_ids(self, protocol=None, groups=None):

    #Checking inputs
    groups    = self.check_parameters_for_validity(groups, "group", GROUPS)
    protocols = self.check_parameters_for_validity(protocol, "protocol", PROTOCOLS) 

    #You need to select only one protocol
    if (len(protocols) > 1):
      raise ValueError("Please, select only one of the following protocols {0}".format(protocols))
 
    #Querying
162
    query = self.query(bob.db.cuhk_cufs.Client).join(bob.db.cuhk_cufs.File).join(bob.db.cuhk_cufs.Protocol_File_Association)
163
164

    #filtering
165
166
    query = query.filter(bob.db.cuhk_cufs.Protocol_File_Association.group.in_(groups))
    query = query.filter(bob.db.cuhk_cufs.Protocol_File_Association.protocol.in_(protocols))
167

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
168
    return [c.id for c in query.all()]
169
170
171
172


  def groups(self, protocol = None, **kwargs):
    """This function returns the list of groups for this database."""
173
174
    return GROUPS

175
176
177
178


  def tmodel_ids(self, groups = None, protocol = None, **kwargs):
    """This function returns the ids of the T-Norm models of the given groups for the given protocol."""
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
179

180
181
    return []

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
182
183
184
185
186
187
188
189
190
191

  def tobjects(self, protocol=None, model_ids=None, groups=None):
    #No TObjects    
    return []


  def zobjects(self, protocol=None, groups=None):
    #No TObjects    
    return []