mifs.py 3.98 KB
Newer Older
1
2
3
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

David GEISSBUHLER's avatar
David GEISSBUHLER committed
4
#!/usr/bin/env python
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#==============================================================================

import bob.bio.video # Used in MIFSPadFile class
import bob.io.base
import numpy as np

from bob.pad.base.database import PadFile # Used in ReplayPadFile class

from bob.pad.base.database import FileListPadDatabase


#==============================================================================

class MIFSPadFile(PadFile):
    """
    A high level implementation of the File class for the MIFS database.
    """

    def __init__(self, client_id, path, attack_type=None, file_id=None):
        super(MIFSPadFile, self).__init__(client_id, path, attack_type, file_id)

    #==========================================================================
    def load(self, directory=None, extension=None):
David GEISSBUHLER's avatar
David GEISSBUHLER committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        """
        Overridden version of the load method defined in the ``PadFile``.

        **Parameters:**

        ``directory`` : :py:class:`str`
            String containing the path to the MIFS database.
            Default: None

        ``extension`` : :py:class:`str`
            Extension of the video files in the MIFS database.
            Default: None

        **Returns:**

        ``video_data`` : FrameContainer
            Video data stored in the FrameContainer, see ``bob.bio.video.utils.FrameContainer``
            for further details.
        """

49
50
51
52
        path = self.make_path(directory=directory, extension=extension) # path to the file
        frame_selector = bob.bio.video.FrameSelector(selection_style = 'all') # this frame_selector will select all frames from the video file

        data = bob.io.base.load(path)
David GEISSBUHLER's avatar
David GEISSBUHLER committed
53
        data = np.expand_dims(data, axis=0) # upgrade to 4D (video)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        video_data = frame_selector(data) # video data

        return video_data # video data


#==============================================================================
class MIFSPadDatabase(FileListPadDatabase):
    """
    A high level implementation of the Database class for the MIFS database.
    """

    def __init__(
        self,
        protocol='grandtest', # grandtest is the default protocol for this database
68
        original_directory='[YOUR_MIFS_DATABASE_DIRECTORY]',
69
70
71
72
        original_extension='.jpg',
        **kwargs):

        from pkg_resources import resource_filename
73
        folder = resource_filename(__name__, '../lists/mifs/')
74
75
76
77
78
79
80
81
82
83
        super(MIFSPadDatabase, self).__init__(folder, 'mifs',
                                            pad_file_class=MIFSPadFile,
                                            protocol = protocol,
                                            original_directory=original_directory,
                                            original_extension=original_extension)

    #==========================================================================
    def annotations(self, f):
        """
        Return annotations for a given file object ``f``, which is an instance
David GEISSBUHLER's avatar
David GEISSBUHLER committed
84
        of ``MIFSPadFile``.
85
86
87
88

        **Parameters:**

        ``f`` : :py:class:`object`
David GEISSBUHLER's avatar
David GEISSBUHLER committed
89
            An instance of ``MIFSPadFile`` defined above.
90
91
92
93
94
95
96
97
98
99

        **Returns:**

        ``annotations`` : :py:class:`dict`
            A dictionary containing the annotations for each frame in the video.
            Dictionary structure: ``annotations = {'1': frame1_dict, '2': frame1_dict, ...}``.
            Where ``frameN_dict = {'topleft': (row, col), 'bottomright': (row, col)}``
            is the dictionary defining the coordinates of the face bounding box in frame N.
        """

100
101
        path_to_file    = self.m_base_dir + '/annotations/' + f.path[:-4] + '.face'
        file_handle     = open(path_to_file, 'r')
102
103
104
105
106
107
108
109
110
111
112
        line            = file_handle.readline()
        bbox            = [int(x) for x in line.split()]

        annotations = {} # dictionary to return

        topleft = (bbox[0], bbox[1])
        bottomright = (bbox[0] + bbox[2], bbox[1] + bbox[3])

        annotations['0'] = {'topleft': topleft, 'bottomright': bottomright}

        return annotations