test_preprocessor.py 4.45 KB
Newer Older
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
1 2 3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

4 5 6 7 8 9 10
import os
import numpy
import bob.io.base
import bob.io.image
import bob.io.video
import bob.bio.base
import bob.bio.video
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
11
import bob.db.base
12
import pkg_resources
13
from bob.bio.video.test.dummy.database import DummyBioFile
14 15 16 17

regenerate_refs = False

def test_annotations():
18 19 20 21 22 23
    original_path = pkg_resources.resource_filename("bob.bio.face.test", "")
    image_files = DummyBioFile(bob.bio.base.database.BioFile(client_id=1, file_id=1, path="data/testimage"))

    # use annotations to grep
    annotations = {os.path.basename(image_files.make_path(original_path, ".jpg")): bob.db.base.read_annotation_file(
        pkg_resources.resource_filename("bob.bio.face.test", "data/testimage.pos"), 'named')}
24

25 26 27
    # video preprocessor using a face crop preprocessor
    frame_selector = bob.bio.video.FrameSelector(selection_style="all")
    preprocessor = bob.bio.video.preprocessor.Wrapper('face-crop-eyes', frame_selector, compressed_io=False)
28

29 30
    # read original data
    original = preprocessor.read_original_data(image_files, original_path, ".jpg")
31

32 33 34
    assert isinstance(original, bob.bio.video.FrameContainer)
    assert len(original) == 1
    assert original[0][0] == os.path.basename(image_files.make_path(original_path, ".jpg"))
35

36 37 38 39 40 41 42 43
    # preprocess data including annotations
    preprocessed = preprocessor(original, annotations)
    assert isinstance(preprocessed, bob.bio.video.FrameContainer)
    assert len(preprocessed) == 1
    assert preprocessed[0][0] == os.path.basename(image_files.make_path(original_path, ".jpg"))
    assert preprocessed[0][2] is None
    assert numpy.allclose(preprocessed[0][1],
                          bob.io.base.load(pkg_resources.resource_filename("bob.bio.face.test", "data/cropped.hdf5")))
44 45 46

def test_detect():

47 48 49 50 51 52 53
    def read_original_data(video_object, original_path, original_extension):
        return frame_selector(video_object.make_path(original_path, original_extension))

    # load test video
    original_path = pkg_resources.resource_filename("bob.bio.video.test", "")
    video_object = bob.bio.video.database.VideoBioFile(bob.bio.base.database.BioFile(
                                                       client_id=1, file_id=1, path="data/testvideo"))
54

55 56 57
    frame_selector = bob.bio.video.FrameSelector(max_number_of_frames=3, selection_style="spread")
    preprocessor = bob.bio.video.preprocessor.Wrapper('face-detect', frame_selector, compressed_io=False,
                                                      read_original_data=read_original_data)
58

59 60
    video = preprocessor.read_original_data(video_object, original_path,".avi")
    assert isinstance(video, bob.bio.video.FrameContainer)
61

62 63 64 65 66 67 68 69 70
    preprocessed_video = preprocessor(video)
    assert isinstance(preprocessed_video, bob.bio.video.FrameContainer)

    reference_file = pkg_resources.resource_filename("bob.bio.video.test", "data/preprocessed.hdf5")
    if regenerate_refs:
        preprocessed_video.save(bob.io.base.HDF5File(reference_file, 'w'))
    reference_data = bob.bio.video.FrameContainer(bob.io.base.HDF5File(reference_file, 'r'))

    assert preprocessed_video.is_similar_to(reference_data)
71 72 73 74


def test_flandmark():

75 76 77 78 79 80 81
    def read_original_data(video_object, original_path, original_extension):
        return frame_selector(video_object.make_path(original_path, original_extension))

    original_path = pkg_resources.resource_filename("bob.bio.video.test", "")
    video_object = bob.bio.video.database.VideoBioFile(bob.bio.base.database.BioFile(
                                                       client_id=1, file_id=1, path="data/testvideo"))
    frame_selector = bob.bio.video.FrameSelector(max_number_of_frames=3, selection_style="spread")
82

83 84 85 86
    preprocessor = bob.bio.video.preprocessor.Wrapper('landmark-detect', frame_selector, compressed_io=False,
                                                      read_original_data=read_original_data)
    video = preprocessor.read_original_data(video_object, original_path, ".avi")
    assert isinstance(video, bob.bio.video.FrameContainer)
87

88 89
    preprocessed_video = preprocessor(video)
    assert isinstance(preprocessed_video, bob.bio.video.FrameContainer)
90

91 92 93 94
    reference_file = pkg_resources.resource_filename("bob.bio.video.test", "data/preprocessed-flandmark.hdf5")
    if regenerate_refs:
        preprocessed_video.save(bob.io.base.HDF5File(reference_file, 'w'))
    reference_data = bob.bio.video.FrameContainer(bob.io.base.HDF5File(reference_file, 'r'))
95

96
    assert preprocessed_video.is_similar_to(reference_data)