Patch.py 2.53 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
from ..utils import extract_patches
from bob.bio.base.preprocessor import Preprocessor
from bob.bio.video import FrameContainer
from bob.bio.video.preprocessor import Wrapper
from collections import OrderedDict


class ImagePatches(Preprocessor):
    """Extracts patches of images and returns it in a FrameContainer. You need
    to wrap the further blocks (extractor and algorithm) that come after this
    in bob.bio.video wrappers.
    """

    def __init__(self, block_size, block_overlap=(0, 0), n_random_patches=None,
                 **kwargs):
        super(ImagePatches, self).__init__(**kwargs)
        self.block_size = block_size
        self.block_overlap = block_overlap
        self.n_random_patches = n_random_patches

    def __call__(self, image, annotations=None):
        fc = FrameContainer()

        patches = extract_patches(image, self.block_size, self.block_overlap,
                                  self.n_random_patches)
        for i, patch in enumerate(patches):
            fc.add(i, patch)

        if not len(fc):
            return None

        return fc


class VideoPatches(Wrapper):
    """Extracts patches of images from video containers and returns it in a
    FrameContainer.
    """

    def __init__(self, block_size, block_overlap=(0, 0), n_random_patches=None,
                 normalizer=None,
                 **kwargs):
        super(VideoPatches, self).__init__(**kwargs)
        self.block_size = block_size
        self.block_overlap = block_overlap
        self.n_random_patches = n_random_patches
        self.normalizer = normalizer

    def __call__(self, frames, annotations=None):
        fc = FrameContainer()

        if self.normalizer is not None:
            annotations = OrderedDict(self.normalizer(annotations))

        for index, frame, _ in frames:

            # if annotations are given, and if particular frame annotations are
            # not missing we take them:
            annots = annotations[index] if annotations is not None and \
                index in annotations else None

            # preprocess image (by default: detect a face)
            preprocessed = self.preprocessor(frame, annots)
            if preprocessed is None:
                continue

            # extract patches
            patches = extract_patches(
                preprocessed, self.block_size, self.block_overlap,
                self.n_random_patches)
            for i, patch in enumerate(patches):
                fc.add('{}_{}'.format(index, i), patch)

        if not len(fc):
            return None

        return fc