3.py 8.94 KB
Newer Older
1 2 3 4 5 6 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 79 80 81 82 83 84 85 86 87 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 121 122 123 124 125 126 127 128 129 130 131 132 133 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 161 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
###############################################################################
#                                                                             #
# Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.examples module of the BEAT platform.         #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################

import os
import numpy as np
import bob.io.base
import bob.io.image
import bob.db.casme2


#----------------------------------------------------------


def get_client_end_index(objs, client_id, client_start_index,
                         start_index, end_index):
    client_end_index = client_start_index

    while client_end_index + 1 <= end_index:
        obj = objs[client_end_index + 1 - start_index]

        if obj.client_id != client_id:
            return client_end_index

        client_end_index += 1

    return end_index


#----------------------------------------------------------


def get_emotion_end_index(objs, emotion, emotion_start_index,
                          start_index, end_index):
    emotion_end_index = emotion_start_index

    while emotion_end_index + 1 <= end_index:
        obj = objs[emotion_end_index + 1 - start_index]

        if obj.emotion != emotion:
            return emotion_end_index

        emotion_end_index += 1

    return end_index


#----------------------------------------------------------


class View:
    """Outputs:
        - image: "{{ system_user.username }}/array_4d_uint8/1"
        - file_id: "{{ system_user.username }}/uint64/1"
        - client_id: "{{ system_user.username }}/uint64/1"
        - emotion: "{{ system_user.username }}/text/1"

    One "file_id" is associated with a given "image".
    Several "image" are associated with a given "client_id".
    Several "client_id" are associated with a given "emotion".

    --------------- --------------- --------------- --------------- --------------- ---------------
    |    image    | |    image    | |    image    | |    image    | |    image    | |    image    |
    --------------- --------------- --------------- --------------- --------------- ---------------
    --------------- --------------- --------------- --------------- --------------- ---------------
    |   file_id   | |   file_id   | |   file_id   | |   file_id   | |   file_id   | |   file_id   |
    --------------- --------------- --------------- --------------- --------------- ---------------
    ----------------------------------------------- -----------------------------------------------
    |                   client_id                 | |                   client_id                 |
    ----------------------------------------------- -----------------------------------------------
    -----------------------------------------------------------------------------------------------
    |                                          emotion                                            |
    -----------------------------------------------------------------------------------------------
    """

    def setup(self, root_folder, outputs, parameters, force_start_index=None,
              force_end_index=None):

        # Initialisations
        self.root_folder = root_folder
        self.outputs     = outputs
        self.parameters  = parameters

        # Open the database and load the objects to provide via the outputs
        self.db = bob.db.casme2.Database()

        self.objs = sorted(self.db.objects(protocol=parameters['protocol'],
                                           groups=parameters['group']),
                           key=lambda x: (x.emotion, x.client_id, x.id))

        # Determine the range of indices that must be provided
        self.start_index = force_start_index if force_start_index is not None else 0
        self.end_index = force_end_index if force_end_index is not None else len(self.objs) - 1

        self.objs = self.objs[self.start_index : self.end_index + 1]

        self.next_index = self.start_index

        return True


    def done(self, last_data_index):
        return last_data_index >= self.end_index


    def next(self):
        obj = self.objs[self.next_index - self.start_index]

        # Output: emotion (only provide data when the emotion change)
        if self.outputs['emotion'].isConnected() and \
           self.outputs['emotion'].last_written_data_index < self.next_index:

            emotion_end_index = get_emotion_end_index(self.objs, obj.emotion,
                                                      self.next_index,
                                                      self.start_index,
                                                      self.end_index)

            self.outputs['emotion'].write(
                {
                    'text': obj.emotion
                },
                emotion_end_index
            )

        # Output: client_id (only provide data when the client_id change)
        if self.outputs['client_id'].isConnected() and \
           self.outputs['client_id'].last_written_data_index < self.next_index:

            client_end_index = get_client_end_index(self.objs, obj.client_id,
                                                    self.next_index,
                                                    self.start_index,
                                                    self.end_index)

            self.outputs['client_id'].write(
                {
                    'value': np.uint64(obj.client_id)
                },
                client_end_index
            )

        # Output: file_id (provide data at each iteration)
        if self.outputs['file_id'].isConnected():
            self.outputs['file_id'].write(
                {
                    'value': np.uint64(obj.id)
                },
                self.next_index
            )

        # Output: image (provide data at each iteration)
        if self.outputs['image'].isConnected():
            frames = obj.frames

            filename = str(os.path.join(obj.make_path(self.root_folder), frames[0].filename))
            frame = bob.io.base.load(filename)

            data = np.zeros(shape=(len(frames), frame.shape[0], frame.shape[1], frame.shape[2]), dtype="uint8")
            data[0] = frame

            for i in range(1, len(frames)):
                filename = str(os.path.join(obj.make_path(self.root_folder), frames[i].filename))
                data[i] = bob.io.base.load(filename)

            self.outputs['image'].write(
                {
                    'value': data
                },
                self.next_index
            )

        # Determine the next data index that must be provided
        self.next_index = 1 + min([ x.last_written_data_index for x in self.outputs
                                                              if x.isConnected() ]
        )


#----------------------------------------------------------


def setup_tests():
    # Install a mock load function for the images
    def mock_load(root_folder):
        return np.ndarray((3, 10, 20), dtype=np.uint8)

    bob.io.base.load = mock_load


#----------------------------------------------------------


# Test the behavior of the views (on fake data)
if __name__ == '__main__':

    setup_tests()

    from beat.backend.python.database import DatabaseTester

    DatabaseTester('View', View,
        [
            'emotion',
            'client_id',
            'file_id',
            'image',
        ],
        parameters=dict(
            protocol='fold_1',
            group='train',
        ),
        irregular_outputs=
        [
            'emotion',
            'client_id',
        ]
    )