verafinger.py 2.43 KB
Newer Older
1 2
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
3
# Tue 27 Sep 2016 16:48:57 CEST
4 5


6 7
from bob.bio.base.database import BioFile, BioDatabase

8
from . import AnnotatedArray
André Anjos's avatar
André Anjos committed
9
from ..preprocessor.utils import poly_to_mask
10

11

André Anjos's avatar
André Anjos committed
12
class File(BioFile):
Amir Mohammadi's avatar
Amir Mohammadi committed
13 14
    """
    Implements extra properties of vein files for the Vera Fingervein database
15 16


Amir Mohammadi's avatar
Amir Mohammadi committed
17
    Parameters:
18

Amir Mohammadi's avatar
Amir Mohammadi committed
19
        f (object): Low-level file (or sample) object that is kept inside
20

Amir Mohammadi's avatar
Amir Mohammadi committed
21
    """
22

Amir Mohammadi's avatar
Amir Mohammadi committed
23
    def __init__(self, f):
24

Amir Mohammadi's avatar
Amir Mohammadi committed
25
        super(File, self).__init__(client_id=f.unique_finger_name, path=f.path,
26
            file_id=f.id)
Amir Mohammadi's avatar
Amir Mohammadi committed
27
        self.__f = f
28

29

Amir Mohammadi's avatar
Amir Mohammadi committed
30 31
    def load(self, *args, **kwargs):
        """(Overrides base method) Loads both image and mask"""
32

Amir Mohammadi's avatar
Amir Mohammadi committed
33
        image = super(File, self).load(*args, **kwargs)
34 35 36
        roi = self.__f.roi()
        mask = poly_to_mask(image.shape, roi)
        return AnnotatedArray(image, metadata=dict(mask=mask, roi=roi))
37 38


André Anjos's avatar
André Anjos committed
39
class Database(BioDatabase):
Amir Mohammadi's avatar
Amir Mohammadi committed
40 41 42
    """
    Implements verification API for querying Vera Fingervein database.
    """
43

Amir Mohammadi's avatar
Amir Mohammadi committed
44
    def __init__(self, **kwargs):
André Anjos's avatar
André Anjos committed
45

Amir Mohammadi's avatar
Amir Mohammadi committed
46 47 48
        super(Database, self).__init__(name='verafinger', **kwargs)
        from bob.db.verafinger.query import Database as LowLevelDatabase
        self._db = LowLevelDatabase()
49

Amir Mohammadi's avatar
Amir Mohammadi committed
50 51
        self.low_level_group_names = ('train', 'dev')
        self.high_level_group_names = ('world', 'dev')
52

Amir Mohammadi's avatar
Amir Mohammadi committed
53
    def groups(self):
André Anjos's avatar
André Anjos committed
54

Amir Mohammadi's avatar
Amir Mohammadi committed
55
        return self.convert_names_to_highlevel(self._db.groups(),
56
            self.low_level_group_names, self.high_level_group_names)
57

Amir Mohammadi's avatar
Amir Mohammadi committed
58 59
    def client_id_from_model_id(self, model_id, group='dev'):
        """Required as ``model_id != client_id`` on this database"""
60

Amir Mohammadi's avatar
Amir Mohammadi committed
61
        return self._db.finger_name_from_model_id(model_id)
André Anjos's avatar
André Anjos committed
62

63

Amir Mohammadi's avatar
Amir Mohammadi committed
64
    def model_ids_with_protocol(self, groups=None, protocol=None, **kwargs):
65

Amir Mohammadi's avatar
Amir Mohammadi committed
66
        groups = self.convert_names_to_lowlevel(groups,
67
            self.low_level_group_names, self.high_level_group_names)
Amir Mohammadi's avatar
Amir Mohammadi committed
68
        return self._db.model_ids(groups=groups, protocol=protocol)
69

70

Amir Mohammadi's avatar
Amir Mohammadi committed
71 72
    def objects(self, groups=None, protocol=None, purposes=None,
                model_ids=None, **kwargs):
André Anjos's avatar
André Anjos committed
73

Amir Mohammadi's avatar
Amir Mohammadi committed
74
        groups = self.convert_names_to_lowlevel(groups,
75
            self.low_level_group_names, self.high_level_group_names)
Amir Mohammadi's avatar
Amir Mohammadi committed
76
        retval = self._db.objects(groups=groups, protocol=protocol,
77 78
                                  purposes=purposes, model_ids=model_ids,
                                  **kwargs)
79

Amir Mohammadi's avatar
Amir Mohammadi committed
80
        return [File(f) for f in retval]
André Anjos's avatar
André Anjos committed
81

82

Amir Mohammadi's avatar
Amir Mohammadi committed
83 84
    def annotations(self, file):
            return None