Commit 9c150bee authored by Guillaume HEUSCH's avatar Guillaume HEUSCH

added db python files, create is ok with files

parent ea27debe
This diff is collapsed.
#!/usr/bin/env python
# encoding: utf-8
"""Commands the BRSU database can respond to.
"""
import os
import sys
from bob.db.base.driver import Interface as BaseInterface
def dumplist(args):
"""Dumps lists of files based on your criteria"""
from .query import Database
db = Database()
r = db.objects(
protocol=args.protocol,
purposes=args.purpose,
groups=args.group,
)
output = sys.stdout
if args.selftest:
from bob.db.base.utils import null
output = null()
for f in r:
output.write('%s\n' % f.make_path(directory=args.directory,extension=args.extension))
return 0
def checkfiles(args):
"""Checks existence of files based on your criteria"""
from .query import Database
db = Database()
r = db.objects()
# go through all files, check if they are available on the filesystem
good = []
bad = []
for f in r:
if os.path.exists(f.make_path(args.directory, args.extension)): good.append(f)
else: bad.append(f)
# report
output = sys.stdout
if args.selftest:
from bob.db.base.utils import null
output = null()
if bad:
for f in bad:
output.write('Cannot find file "%s"\n' % f.make_path(args.directory, args.extension))
output.write('%d files (out of %d) were not found at "%s"\n' % \
(len(bad), len(r), args.directory))
return 0
class Interface(BaseInterface):
def name(self):
return 'brsu'
def version(self):
import pkg_resources # part of setuptools
return pkg_resources.require('bob.db.%s' % self.name())[0].version
def files(self):
from pkg_resources import resource_filename
raw_files = ('db.sql3',)
return [resource_filename(__name__, k) for k in raw_files]
def type(self):
return 'sqlite'
def add_commands(self, parser):
from . import __doc__ as docs
subparsers = self.setup_parser(parser, "BRSU database", docs)
import argparse
# example: get the "create" action from a submodule
from .create import add_command as create_command
create_command(subparsers)
# the "dumplist" action
parser = subparsers.add_parser('dumplist', help=dumplist.__doc__)
parser.add_argument('-d', '--directory', default='', help="if given, this path will be prepended to every entry returned.")
parser.add_argument('-e', '--extension', default='', help="if given, this extension will be appended to every entry returned.")
parser.add_argument('-p', '--protocol', help="the protocol", choices=('default'))
parser.add_argument('-P', '--purpose', help="if given, this value will limit the output files to those designed for the given purposes.", choices=('real', 'attack'))
parser.add_argument('-g', '--group', help="if given, this value will limit the output files to those belonging to a particular protocolar group.", choices=('dev', 'eval', 'world'))
parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
parser.set_defaults(func=dumplist) #action
# the "checkfiles" action
parser = subparsers.add_parser('checkfiles', help=checkfiles.__doc__)
parser.add_argument('-l', '--list-directory', required=True, help="The directory which contains the file lists.")
parser.add_argument('-d', '--directory', dest="directory", default='', help="if given, this path will be prepended to every entry returned.")
parser.add_argument('-e', '--extension', dest="extension", default='', help="if given, this extension will be appended to every entry returned.")
parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
parser.set_defaults(func=checkfiles) #action
#!/usr/bin/env python
# encoding: utf-8
import os
from bob.db.base import utils
from .models import *
from .driver import Interface
INFO = Interface()
SQLITE_FILE = INFO.files()[0]
import bob.db.base
class Database(bob.db.base.SQLiteDatabase):
""" Class representing the database
See parent class `:py:class:bob.db.base.SQLiteDatabase` for more details ...
Attributes
----------
original_directory: str
Path where the database is stored
original_extension: str
Extension of files in the database
annotation_directory: str
Path where the annotations are stored
annotation_extension: str
Extension of anootation files
"""
def __init__(self,
original_directory=None,
original_extension=None,
annotation_directory=None,
annotation_extension=None,
protocol='all'):
""" Init function
Parameters
----------
original_directory: str
Path where the database is stored
original_extension: str
Extension of files in the database
annotation_directory: str
Path where the annotations are stored
annotation_extension: str
Extension of annotation files
"""
super(Database, self).__init__(SQLITE_FILE, ImageFile, original_directory, original_extension)
self.annotation_directory = annotation_directory
self.annotation_extension = annotation_extension
self.protocol = protocol
def groups(self, protocol=None):
"""Returns the names of all registered groups
Parameters
----------
protocol: str
ignored, since the group are the same across protocols.
"""
return ProtocolPurpose.group_choices
def purposes(self):
"""Returns purposes
"""
return ProtocolPurpose.purpose_choices
def objects(self, purposes=None, groups=None):
"""Returns a set of Samples for the specific query by the user.
Note that a sample may contain up to 3 modalities (color, infrared and depth)
The protocol specifies which modality(ies) should be loaded
Parameters
----------
purposes: str or tuple of str
The purposes required to be retrieved ('real', 'attack') or a tuple
with several of them. If 'None' is given (this is the default), it is
considered the same as a tuple with all possible values.
groups: str or tuple of str
One of the groups ('dev', 'eval', 'train') or a tuple with several of them.
If 'None' is given (this is the default), it is considered the same as a
tuple with all possible values.
Returns
-------
lst:
A list of samples which have the given properties.
"""
from sqlalchemy import and_
purposes = self.check_parameters_for_validity(purposes, "purpose", self.purposes())
groups = self.check_parameters_for_validity(groups, "group", self.groups())
q = self.query(Sample)\
.join((ProtocolPurpose, Sample.protocolPurposes))\
.filter(ProtocolPurpose.group.in_(groups))\
.filter(ProtocolPurpose.purpose.in_(purposes))\
.order_by(Sample.id)
retval = list(set(q))
return list(set(retval))
#!/usr/bin/env python
# encoding: utf-8
"""A few checks on CASIA-SURF database
"""
import os, sys
import bob.db.base
import bob.db.casiasurf
def db_available(test):
"""Decorator for detecting if the database file is available"""
from bob.io.base.test_utils import datafile
from nose.plugins.skip import SkipTest
import functools
@functools.wraps(test)
def wrapper(*args, **kwargs):
dbfile = datafile("db.sql3", __name__, None)
if os.path.exists(dbfile):
return test(*args, **kwargs)
else:
raise SkipTest("The database file '%s' is not available; did you forget to run 'bob_dbmanage.py %s create' ?" % (dbfile, 'casiasurf'))
return wrapper
def test_objects():
# tests if the right number of sample objects is returned
db = bob.db.casiasurf.Database()
assert len(db.objects(groups=('train',), purposes=('real',))) == 8942
assert len(db.objects(groups=('train',), purposes=('attack',))) == 20324
assert len(db.objects(groups=('validation',), purposes=('real',))) == 2994
assert len(db.objects(groups=('validation',), purposes=('attack',))) == 6614
assert len(db.objects(groups=('validation',), purposes=('real','attack'))) == 9608
assert len(db.objects(groups=('test',), purposes=('unknown',))) == 57710
......@@ -12,7 +12,7 @@ setup(
name='bob.db.brsu',
version=open("version.txt").read().rstrip(),
description='Bob's interface for the BRSU Multispectral Face/Skin Database',
description="Bob's interface for the BRSU Multispectral Face/Skin Database",
url='https://gitlab.idiap.ch/bob/bob.db.brsu',
license='GPLv3',
......@@ -38,7 +38,11 @@ setup(
install_requires=install_requires,
entry_points={
# add entry points (scripts, bob resources here, if any)
'bob.db': [
'brsu = bob.db.brsu.driver:Interface',
],
},
# check classifiers, add and remove as you see fit
......@@ -56,4 +60,4 @@ setup(
'Topic :: Software Development :: Libraries :: Python Modules',
],
)
\ No newline at end of file
)
Markdown is supported
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