database.py 3.71 KB
Newer Older
1
from bob.pad.base.database import PadFile
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
2
import bob.bio.video
3
4
import bob.io.video
from bob.db.base.annotations import read_annotation_file
5
6


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
7
class VideoPadFile(PadFile):
8
    """A simple base class that defines basic properties of File object for the
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
9
    use in face PAD experiments.
10
11
    """

12
13
14
15
16
17
18
19
20
21
22
    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
23
24
25
        selection_style=None,
        max_number_of_frames=None,
        step_size=None,
26
27
28
29
30
31
32
33
34
35
36
    ):
        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,
37
        )
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
38
39
40
        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
41

42
43
44
    def load(
        self,
    ):
45
        """Loads the video file and returns in a `bob.bio.video.FrameContainer`.
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
46
47
48

        Returns
        -------
49
        :any:`bob.bio.video.VideoAsArray`
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
50
51
            The loaded frames inside a frame container.
        """
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
52
        path = self.make_path(self.original_directory, self.original_extension)
53
54
        video = bob.bio.video.VideoAsArray(
            path,
55
56
57
            selection_style=self.selection_style,
            max_number_of_frames=self.max_number_of_frames,
            step_size=self.step_size,
58
59
        )
        return video
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    @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.
87
        This implementation assumes all frames have the same shape.
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        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)