database.py 5.84 KB
Newer Older
1
2
from functools import partial
import os
3
from bob.pad.base.database import PadFile
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
4
import bob.bio.video
5
6
import bob.io.video
from bob.db.base.annotations import read_annotation_file
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
76
77
78
from sklearn.preprocessing import FunctionTransformer
from bob.bio.video import VideoAsArray
from bob.pipelines import DelayedSample


def delayed_video_load(
    samples,
    original_directory,
    annotation_directory=None,
    selection_style=None,
    max_number_of_frames=None,
    step_size=None,
    get_transform=None,
):
    if get_transform is None:
        def get_transform(x):
            return None

    original_directory = original_directory or ""
    annotation_directory = annotation_directory or ""
    results = []
    for sample in samples:
        video_path = os.path.join(original_directory, sample.filename)
        data = partial(
            VideoAsArray,
            path=video_path,
            selection_style=selection_style,
            max_number_of_frames=max_number_of_frames,
            step_size=step_size,
            transform=get_transform(sample),
        )
        annotations, delayed_attributes = None, None
        if annotation_directory:
            path = os.path.splitext(sample.filename)[0]
            delayed_annotations = partial(
                read_annotation_file,
                file_name=f"{annotation_directory}:{path}.json",
                annotation_type="json",
            )
            delayed_attributes = {"annotations": delayed_annotations}

        results.append(
            DelayedSample(
                data,
                parent=sample,
                delayed_attributes=delayed_attributes,
                annotations=annotations,
            )
        )
    return results


def VideoPadSample(
    original_directory,
    annotation_directory=None,
    selection_style=None,
    max_number_of_frames=None,
    step_size=None,
    get_transform=None,
):
    return FunctionTransformer(
        delayed_video_load,
        validate=False,
        kw_args=dict(
            original_directory=original_directory,
            annotation_directory=annotation_directory,
            selection_style=selection_style,
            max_number_of_frames=max_number_of_frames,
            step_size=step_size,
            get_transform=get_transform,
        ),
    )
79
80


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
81
class VideoPadFile(PadFile):
82
    """A simple base class that defines basic properties of File object for the
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
83
    use in face PAD experiments.
84
85
    """

86
87
88
89
90
91
92
93
94
95
96
    def __init__(
        self,
        attack_type,
        client_id,
        path,
        file_id=None,
        original_directory=None,
        original_extension=".avi",
        annotation_directory=None,
        annotation_extension=None,
        annotation_type=None,
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
97
98
99
        selection_style=None,
        max_number_of_frames=None,
        step_size=None,
100
101
102
103
104
105
106
107
108
109
110
    ):
        super().__init__(
            attack_type=attack_type,
            client_id=client_id,
            path=path,
            file_id=file_id,
            original_directory=original_directory,
            original_extension=original_extension,
            annotation_directory=annotation_directory,
            annotation_extension=annotation_extension,
            annotation_type=annotation_type,
111
        )
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
112
113
114
        self.selection_style = selection_style or "all"
        self.max_number_of_frames = max_number_of_frames
        self.step_size = step_size
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
115

116
117
118
    def load(
        self,
    ):
119
        """Loads the video file and returns in a `bob.bio.video.FrameContainer`.
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
120
121
122

        Returns
        -------
123
        :any:`bob.bio.video.VideoAsArray`
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
124
125
            The loaded frames inside a frame container.
        """
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
126
        path = self.make_path(self.original_directory, self.original_extension)
127
128
        video = bob.bio.video.VideoAsArray(
            path,
129
130
131
            selection_style=self.selection_style,
            max_number_of_frames=self.max_number_of_frames,
            step_size=self.step_size,
132
133
        )
        return video
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

    @property
    def frames(self):
        """Returns an iterator of frames in the video.
        If your database video files need to be loaded in a special way, you need to
        override this property.

        Returns
        -------
        collection.Iterator
            An iterator returning frames of the video.
        """
        path = self.make_path(
            directory=self.original_directory, extension=self.original_extension
        )
        return iter(bob.io.video.reader(path))

    @property
    def number_of_frames(self):
        path = self.make_path(
            directory=self.original_directory, extension=self.original_extension
        )
        return bob.io.video.reader(path).number_of_frames

    @property
    def frame_shape(self):
        """Returns the size of each frame in this database.
161
        This implementation assumes all frames have the same shape.
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        It's best to override this method in your database implementation and return
        a constant.

        Returns
        -------
        (int, int, int)
            The (Channels, Height, Width) sizes.
        """
        path = self.make_path(
            directory=self.original_directory, extension=self.original_extension
        )
        frame = next(bob.io.video.reader(path))
        return frame.shape

    @property
    def annotations(self):
        """Reads the annotations
        For this property to work, you need to set ``annotation_directory``,
        ``annotation_extension``, and ``annotation_type`` attributes of the files when
        database's object method is called.

        Returns
        -------
        dict
            The annotations as a dictionary.
        """
        if self.annotation_directory is None:
            return None

        annotation_file = self.make_path(
            directory=self.annotation_directory, extension=self.annotation_extension
        )
        return read_annotation_file(annotation_file, self.annotation_type)