File size: 5,554 Bytes
5fd3fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import absolute_import, division

import unittest

import numpy as np
import pytest
from PIL import Image

from davisinteractive.utils.visualization import (
    _pascal_color_map, draw_scribble, overlay_mask, plot_scribble)

import matplotlib  # isort:skip
matplotlib.use('Agg')
import matplotlib.pyplot as plt  # isort:skip


class TestPascalColormap(unittest.TestCase):

    def test_default(self):
        colormap = _pascal_color_map()
        assert colormap.shape == (256, 3)
        assert colormap.dtype == np.uint8

        assert np.all(colormap[0] == [0, 0, 0])
        assert np.all(colormap[1] == [128, 0, 0])
        assert np.all(colormap[2] == [0, 128, 0])
        assert np.all(colormap[3] == [128, 128, 0])
        assert np.all(colormap[255] == [224, 224, 192])

    def test_normalized(self):
        colormap = _pascal_color_map(normalized=True)
        assert colormap.shape == (256, 3)
        assert colormap.dtype == np.float32

        assert np.all(np.isclose(colormap[0], [0, 0, 0]))
        assert np.all(np.isclose(colormap[1], [128 / 255, 0, 0]))
        assert np.all(np.isclose(colormap[2], [0, 128 / 255, 0]))
        assert np.all(np.isclose(colormap[3], [128 / 255, 128 / 255, 0]))
        assert np.all(
            np.isclose(colormap[255], [224 / 255, 224 / 255, 192 / 255]))


class TestPlotScribble(unittest.TestCase):

    def test_plot(self):
        scribble = {
            'scribbles': [[], [{
                'path': [[0, 0], [0, 0.1]],
                'object_id': 1,
                'start_time': 0,
                'end_time': 1000
            }, {
                'path': [[0, 0], [0, 0.1]],
                'object_id': 1,
                'start_time': 0,
                'end_time': 1000
            }], [], [], [{
                'path': [[0, 0], [0, 0.1]],
                'object_id': 2,
                'start_time': 0,
                'end_time': 1000
            }, {
                'path': [[0, 0], [0, 0.1]],
                'object_id': 3,
                'start_time': 0,
                'end_time': 1000
            }], [{
                'path': [[0, 0], [0, 0.1]],
                'object_id': 2,
                'start_time': 0,
                'end_time': 1000
            }], []],
            'sequence':
            'test',
        }
        _, ax = plt.subplots(1)
        plot_scribble(ax, scribble, 1, output_size=(100, 200))

        plot_scribble(ax, scribble, 1)

        with pytest.raises(ValueError):
            plot_scribble(ax, scribble, 10)


class TestDrawScribble(unittest.TestCase):

    def test_draw(self):
        scribble = {
            'scribbles': [[], [{
                'path': [[0, 0], [0, 0.1]],
                'object_id': 1,
                'start_time': 0,
                'end_time': 1000
            }, {
                'path': [[0, 0], [0, 0.1]],
                'object_id': 1,
                'start_time': 0,
                'end_time': 1000
            }], [], [], [{
                'path': [[0, 0], [0, 0.1]],
                'object_id': 2,
                'start_time': 0,
                'end_time': 1000
            }, {
                'path': [[0, 0], [0, 0.1]],
                'object_id': 3,
                'start_time': 0,
                'end_time': 1000
            }], [{
                'path': [[0, 0], [0, 0.1]],
                'object_id': 2,
                'start_time': 0,
                'end_time': 1000
            }], []],
            'sequence':
            'test',
        }
        img = Image.fromarray(np.zeros((480, 854, 3), dtype=np.uint8))
        img_draw = draw_scribble(img, scribble, 1)

        img_draw = draw_scribble(img, scribble, 0)
        self.assertEqual(img_draw, img)


class TestOverlay(unittest.TestCase):

    def test_shape_mismatch(self):
        im = np.random.randint(0, 255, (100, 200, 3), dtype=np.uint8)
        ann = np.zeros((100, 201))

        with pytest.raises(ValueError):
            overlay_mask(im, ann)

    def test_wrong_channel_dimensions(self):
        im = np.random.randint(0, 255, (100, 200, 4), dtype=np.uint8)
        ann = np.zeros((100, 200))

        with pytest.raises(ValueError):
            overlay_mask(im, ann)

    def test_empty_mask(self):
        im = np.random.randint(0, 255, (100, 200, 3), dtype=np.uint8)
        ann = np.zeros((100, 200))

        img = overlay_mask(im, ann)

        assert img.shape == im.shape
        assert img.dtype == im.dtype
        assert np.all(img == im)

    def test_full_mask(self):
        im = np.random.randint(0, 255, (100, 200, 3), dtype=np.uint8)
        ann = np.ones((100, 200))

        img = overlay_mask(im, ann, alpha=1.)

        assert img.shape == im.shape
        assert img.dtype == im.dtype
        assert np.all(img == im)

        img = overlay_mask(im, ann, alpha=0.)

        assert img.shape == im.shape
        assert img.dtype == im.dtype
        assert np.all(img == [128, 0, 0])

        img = overlay_mask(im, ann, alpha=0., colors=[[45, 43, 76], [1, 2, 3]])

        assert img.shape == im.shape
        assert img.dtype == im.dtype
        assert np.all(img == [1, 2, 3])

    def test_mask(self):
        im = np.random.randint(0, 255, (100, 200, 3), dtype=np.uint8)
        ann = np.zeros((100, 200))
        ann[50:70, 50:150] = 1

        img = overlay_mask(im, ann, alpha=.5)

        assert img.shape == im.shape
        assert img.dtype == im.dtype
        assert np.all(img[ann == 0] == im[ann == 0])
        assert not np.all(img[ann != 0] == im[ann != 0])