forked from buriburisuri/speech-to-text-wavenet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
executable file
·260 lines (200 loc) · 7.7 KB
/
preprocess.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import numpy as np
import pandas as pd
import glob
import csv
import librosa
import scikits.audiolab
import data
import os
import subprocess
__author__ = '[email protected]'
# data path
_data_path = "asset/data/"
#
# process VCTK corpus
#
def process_vctk(csv_file):
# create csv writer
writer = csv.writer(csv_file, delimiter=',')
# read label-info
df = pd.read_table(_data_path + 'VCTK-Corpus/speaker-info.txt', usecols=['ID'],
index_col=False, delim_whitespace=True)
# read file IDs
file_ids = []
for d in [_data_path + 'VCTK-Corpus/txt/p%d/' % uid for uid in df.ID.values]:
file_ids.extend([f[-12:-4] for f in sorted(glob.glob(d + '*.txt'))])
for i, f in enumerate(file_ids):
# wave file name
wave_file = _data_path + 'VCTK-Corpus/wav48/%s/' % f[:4] + f + '.wav'
fn = wave_file.split('/')[-1]
target_filename = 'asset/data/preprocess/mfcc/' + fn + '.npy'
if os.path.exists( target_filename ):
continue
# print info
print("VCTK corpus preprocessing (%d / %d) - '%s']" % (i, len(file_ids), wave_file))
# load wave file
wave, sr = librosa.load(wave_file, mono=True, sr=None)
# re-sample ( 48K -> 16K )
wave = wave[::3]
# get mfcc feature
mfcc = librosa.feature.mfcc(wave, sr=16000)
# get label index
label = data.str2index(open(_data_path + 'VCTK-Corpus/txt/%s/' % f[:4] + f + '.txt').read())
# save result ( exclude small mfcc data to prevent ctc loss )
if len(label) < mfcc.shape[1]:
# save meta info
writer.writerow([fn] + label)
# save mfcc
np.save(target_filename, mfcc, allow_pickle=False)
#
# process LibriSpeech corpus
#
def process_libri(csv_file, category):
parent_path = _data_path + 'LibriSpeech/' + category + '/'
labels, wave_files = [], []
# create csv writer
writer = csv.writer(csv_file, delimiter=',')
# read directory list by speaker
speaker_list = glob.glob(parent_path + '*')
for spk in speaker_list:
# read directory list by chapter
chapter_list = glob.glob(spk + '/*/')
for chap in chapter_list:
# read label text file list
txt_list = glob.glob(chap + '/*.txt')
for txt in txt_list:
with open(txt, 'rt') as f:
records = f.readlines()
for record in records:
# parsing record
field = record.split('-') # split by '-'
speaker = field[0]
chapter = field[1]
field = field[2].split() # split field[2] by ' '
utterance = field[0] # first column is utterance id
# wave file name
wave_file = parent_path + '%s/%s/%s-%s-%s.flac' % \
(speaker, chapter, speaker, chapter, utterance)
wave_files.append(wave_file)
# label index
labels.append(data.str2index(' '.join(field[1:]))) # last column is text label
# save results
for i, (wave_file, label) in enumerate(zip(wave_files, labels)):
fn = wave_file.split('/')[-1]
target_filename = 'asset/data/preprocess/mfcc/' + fn + '.npy'
if os.path.exists( target_filename ):
continue
# print info
print("LibriSpeech corpus preprocessing (%d / %d) - '%s']" % (i, len(wave_files), wave_file))
# load flac file
wave, sr, _ = scikits.audiolab.flacread(wave_file)
# get mfcc feature
mfcc = librosa.feature.mfcc(wave, sr=16000)
# save result ( exclude small mfcc data to prevent ctc loss )
if len(label) < mfcc.shape[1]:
# filename
# save meta info
writer.writerow([fn] + label)
# save mfcc
np.save(target_filename, mfcc, allow_pickle=False)
#
# process TEDLIUM corpus
#
def convert_sph( sph, wav ):
"""Convert an sph file into wav format for further processing"""
command = [
'sox','-t','sph', sph, '-b','16','-t','wav', wav
]
subprocess.check_call( command ) # Did you install sox (apt-get install sox)
def process_ted(csv_file, category):
parent_path = _data_path + 'TEDLIUM_release2/' + category + '/'
labels, wave_files, offsets, durs = [], [], [], []
# create csv writer
writer = csv.writer(csv_file, delimiter=',')
# read STM file list
stm_list = glob.glob(parent_path + 'stm/*')
for stm in stm_list:
with open(stm, 'rt') as f:
records = f.readlines()
for record in records:
field = record.split()
# wave file name
wave_file = parent_path + 'sph/%s.sph.wav' % field[0]
wave_files.append(wave_file)
# label index
labels.append(data.str2index(' '.join(field[6:])))
# start, end info
start, end = float(field[3]), float(field[4])
offsets.append(start)
durs.append(end - start)
# save results
for i, (wave_file, label, offset, dur) in enumerate(zip(wave_files, labels, offsets, durs)):
fn = "%s-%.2f" % (wave_file.split('/')[-1], offset)
target_filename = 'asset/data/preprocess/mfcc/' + fn + '.npy'
if os.path.exists( target_filename ):
continue
# print info
print("TEDLIUM corpus preprocessing (%d / %d) - '%s-%.2f]" % (i, len(wave_files), wave_file, offset))
# load wave file
if not os.path.exists( wave_file ):
sph_file = wave_file.rsplit('.',1)[0]
if os.path.exists( sph_file ):
convert_sph( sph_file, wave_file )
else:
raise RuntimeError("Missing sph file from TedLium corpus at %s"%(sph_file))
wave, sr = librosa.load(wave_file, mono=True, sr=None, offset=offset, duration=dur)
# get mfcc feature
mfcc = librosa.feature.mfcc(wave, sr=16000)
# save result ( exclude small mfcc data to prevent ctc loss )
if len(label) < mfcc.shape[1]:
# filename
# save meta info
writer.writerow([fn] + label)
# save mfcc
np.save(target_filename, mfcc, allow_pickle=False)
#
# Create directories
#
if not os.path.exists('asset/data/preprocess'):
os.makedirs('asset/data/preprocess')
if not os.path.exists('asset/data/preprocess/meta'):
os.makedirs('asset/data/preprocess/meta')
if not os.path.exists('asset/data/preprocess/mfcc'):
os.makedirs('asset/data/preprocess/mfcc')
#
# Run pre-processing for training
#
# VCTK corpus
csv_f = open('asset/data/preprocess/meta/train.csv', 'w')
process_vctk(csv_f)
csv_f.close()
# LibriSpeech corpus for train
csv_f = open('asset/data/preprocess/meta/train.csv', 'a+')
process_libri(csv_f, 'train-clean-360')
csv_f.close()
# TEDLIUM corpus for train
csv_f = open('asset/data/preprocess/meta/train.csv', 'a+')
process_ted(csv_f, 'train')
csv_f.close()
#
# Run pre-processing for validation
#
# LibriSpeech corpus for valid
csv_f = open('asset/data/preprocess/meta/valid.csv', 'w')
process_libri(csv_f, 'dev-clean')
csv_f.close()
# TEDLIUM corpus for valid
csv_f = open('asset/data/preprocess/meta/valid.csv', 'a+')
process_ted(csv_f, 'dev')
csv_f.close()
#
# Run pre-processing for testing
#
# LibriSpeech corpus for test
csv_f = open('asset/data/preprocess/meta/test.csv', 'w')
process_libri(csv_f, 'test-clean')
csv_f.close()
# TEDLIUM corpus for test
csv_f = open('asset/data/preprocess/meta/test.csv', 'a+')
process_ted(csv_f, 'test')
csv_f.close()