/
test_classifier.py
77 lines (61 loc) · 2.3 KB
/
test_classifier.py
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
'''
This file tests the classifier. It uses the generates testcases from
generate_samples.py.
'''
from database import Database
from soundfiles import load_signal, find_files
from fingerprint import get_tokens
import matplotlib.pyplot as plt
from random import shuffle
from sys import exit
database = Database("pokemon", replace=False)
classifier = database.as_classifier()
filenames = find_files("audio/pokemon/*.wav")
if not filenames:
print "No files found"
exit()
print "Start testing classifier with {} files\n".format(len(filenames))
shuffle(filenames)
correct_match = 0
wrong_match = 0
no_match = 0
wrong_matches = ""
correct_matches = ""
no_matches = ""
for filename in filenames:
print "Classifying {}".format(filename)
signal = load_signal(filename)
tokens = get_tokens(signal)
match = classifier.classify(tokens)
if match is None:
print "-> No match found"
no_matches += filename.split("/")[2][:-4]
no_match += 1
elif match.split("/")[2][:3] == filename.split("/")[2][:3]:
print "-> Correct match found"
correct_match += 1
correct_matches += filename.split("/")[2][:-4]
else:
print "-> Wrong match found"
wrong_matches += filename.split("/")[2][:-4]
wrong_match += 1
print "\nDone testing {} files".format(len(filenames))
print "{} not matched".format(no_match)
print "{} wrong".format(wrong_match)
print "{} correct".format(correct_match)
"""
# This block prints the number of correct/wrong for each possible distortion
print "\n{} offset not matched".format(no_matches.count("offset"))
print "{} offset wrong".format(wrong_matches.count("offset"))
print "{} offset correct".format(correct_matches.count("offset"))
print "\n{} noise not matched".format(no_matches.count("noise"))
print "{} noise wrong".format(wrong_matches.count("noise"))
print "{} noise correct".format(correct_matches.count("noise"))
print "\n{} amp not matched".format(no_matches.count("amp"))
print "{} amp wrong".format(wrong_matches.count("amp"))
print "{} amp correct".format(correct_matches.count("amp"))
"""
""" Plot a pie chart. """
plt.pie([correct_match, no_match, wrong_match], labels=["Correct match", "No match", "Wrong match"], colors=["yellowgreen", "yellow", "red"], startangle=90, shadow=False)
plt.axis('equal')
plt.show()