query.py 4.51 KB
Newer Older
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
1
from pkg_resources import resource_filename
2
3
from bob.pad.base.database import FileListPadDatabase
from bob.pad.face.database import VideoPadFile
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
4
from bob.bio.video import FrameSelector
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
5
from bob.extension import rc
6
from bob.io.video import reader
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
7
from . import OULUNPU_FRAME_SHAPE
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
8
9


10
class File(VideoPadFile):
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
11
    """The file objects of the OULU-NPU dataset."""
12
13
14
15
16
17
18
19
20
21

    @property
    def frames(self):
        """Yields the frames of the biofile one by one.

        Yields
        ------
        :any:`numpy.array`
            A frame of the video. The size is (3, 1920, 1080).
        """
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
22
23
        vfilename = self.make_path(
            directory=self.original_directory, extension='.avi')
24
25
26
27
28
29
30
31
32
33
34
        return iter(reader(vfilename))

    @property
    def number_of_frames(self):
        """Returns the number of frames in a video file.

        Returns
        -------
        int
            The number of frames.
        """
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
35
36
        vfilename = self.make_path(
            directory=self.original_directory, extension='.avi')
37
38
39
40
41
42
43
44
45
46
47
48
        return reader(vfilename).number_of_frames

    @property
    def frame_shape(self):
        """Returns the size of each frame in this database.

        Returns
        -------
        (int, int, int)
            The (#Channels, Height, Width) which is :any:`OULUNPU_FRAME_SHAPE`.
        """
        return OULUNPU_FRAME_SHAPE
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
49

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    @property
    def annotations(self):
        """Reads the annotations

        Returns
        -------
        dict
            The annotations as a dictionary, e.g.:
            ``{'0': {'reye':(re_y,re_x), 'leye':(le_y,le_x)}, ...}``
        """
        path = self.make_path(
            directory=self.original_directory, extension='.txt')
        annotations = {}
        with open(path) as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                num_frame, x_eye_left, y_eye_left, x_eye_right, y_eye_right = \
                    line.split(',')
                annotations[num_frame] = {
                    'reye': (int(y_eye_right), int(x_eye_right)),
                    'leye': (int(y_eye_left), int(x_eye_left)),
                }
        return annotations

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    def load(self, directory=None, extension='.avi',
             frame_selector=FrameSelector(selection_style='all')):
        """Loads the video file and returns in a
        :any:`bob.bio.video.FrameContainer`.

        Parameters
        ----------
        directory : :obj:`str`, optional
            The directory to load the data from.
        extension : :obj:`str`, optional
            The extension of the file to load.
        frame_selector : :any:`bob.bio.video.FrameSelector`, optional
            Which frames to select.

        Returns
        -------
        :any:`bob.bio.video.FrameContainer`
            The loaded frames inside a frame container.
        """
        directory = directory or self.original_directory
        extension = extension or self.original_extension
        return frame_selector(self.make_path(directory, extension))

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
99
100
101
102

class Database(FileListPadDatabase):
    """The database interface for the OULU-NPU dataset."""

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
103
    def __init__(self, original_directory=rc['bob.db.oulunpu.directory'],
104
                 name='oulunpu', pad_file_class=None,
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
105
                 original_extension=".avi", **kwargs):
106
107
        if pad_file_class is None:
            pad_file_class = File
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
108
109
110
111
        filelists_directory = resource_filename(__name__, 'lists')
        super(Database, self).__init__(
            filelists_directory=filelists_directory, name=name,
            original_directory=original_directory,
112
            pad_file_class=pad_file_class,
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
113
            original_extension=original_extension,
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
114
            training_depends_on_protocol=True,
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
115
            **kwargs)
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
116

117
118
    def objects(self, groups=None, protocol=None, purposes=None,
                model_ids=None, classes=None, **kwargs):
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
119
        """Returns the requested samples."""
120
121
122
123
124
        files = super(Database, self).objects(
            groups=groups, protocol=protocol, purposes=purposes,
            model_ids=model_ids, classes=classes, **kwargs)
        for f in files:
            f.original_directory = self.original_directory
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
125
            f.original_extension = self.original_extension
126
127
        return files

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
128
    def frames(self, padfile):
129
        return padfile.frames
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
130

131
132
    def number_of_frames(self, padfile):
        return padfile.number_of_frames
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
133

134
135
136
    @property
    def frame_shape(self):
        return OULUNPU_FRAME_SHAPE
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
137

138
    def annotations(self, padfile):
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
139
        return padfile.annotations