test_utils.py 2.45 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
from bob.pad.face.test.dummy.database import DummyDatabase as Database
from bob.pad.face.utils import yield_frames, yield_faces, scale_face, blocks
from types import MethodType
from nose.tools import raises
import numpy

padfile = Database().all_files(('train', 'dev'))[0][0]
image = padfile.load(Database().original_directory,
                     Database().original_extension)[0][1]


def _annotations(self, padfile):
    return {'0': {'topleft': (0, 0), 'bottomright': self.frame_shape}}


def test_yield_frames():
    database = Database()
    assert database.number_of_frames(padfile) == 1
    for frame in yield_frames(database, padfile):
        assert frame.ndim == 2
        assert frame.shape == database.frame_shape


@raises(ValueError)
def test_yield_faces_1():
    database = Database()
    for face in yield_faces(database, padfile):
        pass


def test_yield_faces_2():
    database = Database()
    database.annotations = MethodType(
        _annotations, database, database.__class__)
    for face in yield_faces(database, padfile):
        assert face.ndim == 2
        assert face.shape == database.frame_shape


def test_scale_face():
    # gray-scale image
    face = image
    scaled_face = scale_face(face, 64)
    assert scaled_face.dtype == 'float64'
    assert scaled_face.shape == (64, 64)
    # color image
    scaled_face = scale_face(numpy.array([face, face, face]), 64)
    assert scaled_face.dtype == 'float64'
    assert scaled_face.shape == (3, 64, 64)
    assert (scaled_face[0] == scaled_face[1]).all()
    assert (scaled_face[0] == scaled_face[2]).all()


def test_blocks():
    # gray-scale image
    patches = blocks(image, (28, 28))
    assert patches.shape == (12, 28, 28), patches.shape
    # color image
    patches_gray = patches
    patches = blocks([image, image, image], (28, 28))
    assert patches.shape == (12, 3, 28, 28), patches.shape
    assert (patches_gray == patches[:, 0, ...]).all()
    assert (patches_gray == patches[:, 1, ...]).all()
    assert (patches_gray == patches[:, 2, ...]).all()
    # color video
    patches = blocks([[image, image, image]], (28, 28))
    assert patches.shape == (12, 3, 28, 28), patches.shape
    assert (patches_gray == patches[:, 0, ...]).all()
    assert (patches_gray == patches[:, 1, ...]).all()
    assert (patches_gray == patches[:, 2, ...]).all()


@raises(ValueError)
def test_block_raises1():
    blocks(image[0], (28, 28))


@raises(ValueError)
def test_block_raises2():
    blocks([[[image]]], (28, 28))