test_utils.py 2.49 KB
Newer Older
1
2
import time
from bob.bio.base.test.utils import is_library_available
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
3
from bob.io.base.test_utils import datafile
4
from bob.io.video import reader
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
5
import numpy as np
6
7
8
9
10
import bob.bio.video

regenerate_refs = False


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
11
12
def test_video_as_array():
    path = datafile("testvideo.avi", "bob.bio.video.test")
13

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
14
15
16
    video = bob.bio.video.VideoAsArray(path, selection_style="all")
    assert len(video) == 83, len(video)
    assert video.indices == range(83), video.indices
17
    assert video.shape == (83, 3, 480, 640), video.shape
18

19
20
21
22
23
24
25
26
27
28
29
    video_slice = video[1:2, 1:-1, 1:-1, 1:-1]
    assert video_slice.shape == (1, 1, 478, 638), video_slice.shape
    # test the slice against the video loaded by bob.io.video directly
    video = reader(path)[1]
    video = video[1:-1, 1:-1, 1:-1]
    video = video[None, ...]
    np.testing.assert_allclose(video, video_slice)

    video = bob.bio.video.VideoAsArray(
        path, max_number_of_frames=3
    )
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
30
31
    assert len(video) == 3, len(video)
    assert video.indices == [13, 41, 69], video.indices
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    assert video.shape == (3, 3, 480, 640), video.shape


@is_library_available("dask")
def test_video_as_array_vs_dask():
    import dask

    path = datafile("testvideo.avi", "bob.bio.video.test")
    start = time.time()
    video = bob.bio.video.VideoAsArray(path, selection_style="all")
    video = dask.array.from_array(video, (20, 1, 480, 640))
    video = video.compute()
    load_time = time.time() - start

    start = time.time()
    reference = reader(path).load()
    load_time2 = time.time() - start
    # Here, we're also chunking each frame, but normally we would only chunk the first axis.
    print(
        f"FYI: It took {load_time:.2f} s to load the video with dask and {load_time2:.2f} s "
        "to load directly. The slower loading with dask is expected."
    )
    np.testing.assert_allclose(reference, video)
55
56


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
57
58
def test_video_like_container():
    path = datafile("testvideo.avi", "bob.bio.video.test")
59

60
61
62
    video = bob.bio.video.VideoAsArray(
        path, selection_style="spread", max_number_of_frames=3
    )
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
63
    container = bob.bio.video.VideoLikeContainer(video, video.indices)
64

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
65
    container_path = datafile("video_like.hdf5", "bob.bio.video.test")
66

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
67
68
    if regenerate_refs:
        container.save(container_path)
69

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
70
    loaded_container = bob.bio.video.VideoLikeContainer.load(container_path)
71

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
72
    np.testing.assert_equal(np.array(container.data), np.array(loaded_container.data))
73
74
75
    np.testing.assert_equal(
        np.array(container.indices), np.array(loaded_container.indices)
    )