verafinger.py 2.61 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 8
from bob.bio.base.database import BioFile, BioDatabase


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


Amir Mohammadi's avatar
Amir Mohammadi committed
14
    Parameters:
15

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

Amir Mohammadi's avatar
Amir Mohammadi committed
18
    """
19

Amir Mohammadi's avatar
Amir Mohammadi committed
20
    def __init__(self, f):
21

Amir Mohammadi's avatar
Amir Mohammadi committed
22 23 24
        super(File, self).__init__(client_id=f.unique_finger_name, path=f.path,
                                   file_id=f.id)
        self.__f = f
25

Amir Mohammadi's avatar
Amir Mohammadi committed
26 27
    def mask(self):
        """Returns the binary mask from the ROI annotations available"""
28

Amir Mohammadi's avatar
Amir Mohammadi committed
29
        from ..preprocessor.utils import poly_to_mask
30

Amir Mohammadi's avatar
Amir Mohammadi committed
31 32
        # The size of images in this database is (250, 665) pixels (h, w)
        return poly_to_mask((250, 665), self.__f.roi())
33

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

Amir Mohammadi's avatar
Amir Mohammadi committed
37
        image = super(File, self).load(*args, **kwargs)
38

Amir Mohammadi's avatar
Amir Mohammadi committed
39
        return image, self.mask()
40 41


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

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

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

Amir Mohammadi's avatar
Amir Mohammadi committed
53 54
        self.low_level_group_names = ('train', 'dev')
        self.high_level_group_names = ('world', 'dev')
55

Amir Mohammadi's avatar
Amir Mohammadi committed
56
    def groups(self):
André Anjos's avatar
André Anjos committed
57

Amir Mohammadi's avatar
Amir Mohammadi committed
58 59
        return self.convert_names_to_highlevel(self._db.groups(),
                                               self.low_level_group_names, self.high_level_group_names)
60

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

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

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

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

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

Amir Mohammadi's avatar
Amir Mohammadi committed
75 76 77 78
        groups = self.convert_names_to_lowlevel(groups,
                                                self.low_level_group_names, self.high_level_group_names)
        retval = self._db.objects(groups=groups, protocol=protocol,
                                  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

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