-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
157 lines (135 loc) · 3.89 KB
/
utils.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import random
import librosa
import numpy as np
import string
def collate_fn_(batch_data, max_len=40000):
audio = batch_data[0]
audio_len = audio.size(1)
if audio_len > max_len:
idx = random.randint(0,audio_len - max_len)
return audio[:,idx:idx+max_len]
else:
return audio
class Data:
num_channel = 40
vocabulary = [' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '<EMP>']
decoder_vocabulary = [' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']
sample_rate = 16000
def read_wave(filepath):
# load wave file
wave, sr = librosa.load(filepath, mono=True, sr=None)
# re-sample ( 48K -> 16K )
wave = wave[::3]
# get mfcc feature
mfcc = librosa.feature.mfcc(wave, sr=16000, n_mfcc=Data.num_channel)
# wave, sr = librosa.load(filepath, mono=True, sr=Data.sample_rate)
# mfcc = librosa.feature.mfcc(wave, sr=sr, n_mfcc=Data.num_channel)
return mfcc
def read_txt(filepath):
txt = open(filepath).read()
txt = ' '.join(txt.split())
txt = txt.translate(string.punctuation).lower()
reval = []
for ch in txt:
try:
if ch in Data.vocabulary:
reval.append(Data.vocabulary.index(ch))
# else:
# glog.warning('%s was not in vocabulary at %s' % (ch, filepath))
except KeyError:
pass
return reval
def cvt_np2string(inputs):
outputs = []
for input in inputs:
output = ''
for i in input:
# ch = i.decode('utf-8')
# if ch == '<EMP>':
# continue
output += i#.decode('utf-8')
outputs.append(output)
return outputs
def _find_best_match2(inputs):
def _find_node(values, start=(-1, -1)):
node = []
for index in range(start[1] + 1, len(values)):
value = values[index]
if len(value) > 0:
for v in value:
if v > start[0]:
if len(node) == 0:
node.append((v, index))
elif v < node[-1][0]:
node.append((v, index))
return node
def _find_nodes(values):
nodes = []
while True:
if len(nodes) == 0:
node = _find_node(values)
if len(node) == 0:
break
for n in node:
nodes.append([n])
else:
tmps = []
change = False
for tmp in nodes:
node = _find_node(values, tmp[-1])
if len(node) == 0:
tmps.append(tmp)
else:
for n in node:
tmps.append(tmp + [n])
change = True
if change:
nodes = tmps
else:
break
return nodes
nodes = _find_nodes(inputs)
if len(nodes) == 0:
return []
else:
return sorted(nodes, key=lambda iter: len(iter), reverse=True)[0]
def _normalize(inputs):
inputs = inputs.split(' ')
outputs = []
for input in inputs:
if input != '':
outputs.append(input)
return outputs
def evalute(predicts, labels):
predicts = _normalize(predicts)
labels = _normalize(labels)
matches = []
for label in labels:
match = []
for j, predict in enumerate(predicts):
if label == predict:
match.append(j)
if len(match) > 0:
matches.append(match)
match = _find_best_match2(matches)
return len(match), len(predicts), len(labels)
def evalutes(predicts, labels):
size = min(len(predicts), len(labels))
tp = 0
pred = 0
pos = 0
for i in range(size):
data = evalute(predicts[i], labels[i])
tp += data[0]
pred += data[1]
pos += data[2]
return tp, pred, pos
if __name__ == '__main__':
mfcc = read_wave('/lyuan/dataset/VCTK/VCTK-Corpus/wav48/p225/p225_001.wav')
print(mfcc.shape)
reval = read_txt('/lyuan/dataset/VCTK/VCTK-Corpus/txt/p225/p225_001.txt')
print(reval.shape)