test_spoofspeech.py 3.38 KB
Newer Older
1
2
3
4
5
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Pavel Korshunov <pavel.korshunov@idiap.ch>
# @date: Thu Apr 28 16:41:21 CEST 2016
#
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


from __future__ import print_function

import bob.measure

import os
import shutil
import tempfile
import numpy

import bob.io.base.test_utils
import bob.io.image
import bob.bio.base
import bob.pad.base
import bob.bio.spear
import bob.pad.voice

import pkg_resources

dummy_dir = pkg_resources.resource_filename('bob.pad.voice', 'test/dummy')
data_dir = pkg_resources.resource_filename('bob.pad.voice', 'test/data')


def _spoof(parameters, cur_test_dir, sub_dir, score_types=('dev-real',), scores_extension=''):
    from bob.pad.base.script.spoof import main
    try:
        main(parameters)

        # assert that the score file exists
        score_files = [os.path.join(cur_test_dir, sub_dir, 'Default', 'scores',
                                    'scores-%s%s' % (score_type, scores_extension)) for score_type in score_types]
        assert os.path.exists(score_files[0]), "Score file %s does not exist" % score_files[0]

        # also assert that the scores are still the same -- though they have no real meaning
        reference_files = [os.path.join(data_dir, sub_dir, 'scores-%s' % score_type) for score_type in score_types]

        # read reference and new data
        for i in range(0, len(score_types)):
            data2check = []
            for sfile in (score_files[i], reference_files[i]):
                f = bob.measure.load.open_file(sfile)
                d_ = []
                for line in f:
                    if isinstance(line, bytes): line = line.decode('utf-8')
                    d_.append(line.rstrip().split())
                data2check.append(numpy.array(d_))

            assert data2check[0].shape == data2check[1].shape
            # assert that the data order is still correct
            assert (data2check[0][:, 0:3] == data2check[1][:, 0:3]).all()
            # assert that the values are OK
Pavel KORSHUNOV's avatar
Pavel KORSHUNOV committed
59
            print (data2check)
Pavel KORSHUNOV's avatar
Pavel KORSHUNOV committed
60
            assert numpy.allclose(data2check[0][:, 3].astype(float), data2check[1][:, 3].astype(float), 1e-3)
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

    finally:
        shutil.rmtree(cur_test_dir)


def test_spoof_EnergyLBP1():
    test_dir = tempfile.mkdtemp(prefix='bobtest_')
    # define dummy parameters
    parameters = [
        '-d', 'dummy-speech',
        '-p', 'mod-4hz',
        '-e', 'lbp-hist',
        '-a', 'logregr',
        '-vs', 'test_energylbp',
        '--groups', ['dev', 'eval'],
        '--temp-directory', test_dir,
        '--result-directory', test_dir
    ]

    print(bob.pad.base.tools.command_line(parameters))

    _spoof(parameters, test_dir, 'test_energylbp', ('dev-real', 'dev-attack'))
    _spoof(parameters, test_dir, 'test_energylbp', ('eval-real', 'eval-attack'))


def test_spoof_EnergyLBP2():
    test_dir = tempfile.mkdtemp(prefix='bobtest_')
    # define dummy parameters
    parameters = [
        '-d', 'bob.pad.voice.test.dummy.database.DummyDatabaseSpeechSpoof()',
        '-p', 'bob.bio.spear.preprocessor.Mod_4Hz()',
        '-e', 'bob.pad.voice.extractor.LBPHistograms(features_processor=bob.pad.voice.extractor.SpectrogramExtended())',
        '-a', 'bob.pad.voice.algorithm.LogRegrAlgorithm()',
        '-vs', 'test_energylbp',
        '--temp-directory', test_dir,
        '--result-directory', test_dir
    ]

    print(bob.pad.base.tools.command_line(parameters))

    _spoof(parameters, test_dir, 'test_energylbp')