-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocess_data.py
91 lines (73 loc) · 2.49 KB
/
process_data.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
import pickle
from glob import iglob
import numpy as np
import librosa
from shutil import rmtree
from constants import *
def mkdir_p(path):
import errno
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def del_folder(path):
try:
rmtree(path)
except:
pass
del_folder(OUTPUT_DIR_TRAIN)
del_folder(OUTPUT_DIR_TEST)
mkdir_p(OUTPUT_DIR_TRAIN)
mkdir_p(OUTPUT_DIR_TEST)
class_ids = {
'normal': 0,
'murmur': 1,
'extrahls': 2,
'artifact': 3,
'unlabelled': 4,
}
def extract_class_id(wav_filename):
if 'normal' in wav_filename:
return class_ids.get('normal')
elif 'murmur' in wav_filename:
return class_ids.get('murmur')
elif 'extrahls' in wav_filename:
return class_ids.get('extrahls')
elif 'artifact' in wav_filename:
return class_ids.get('artifact')
elif 'unlabelled' in wav_filename:
return class_ids.get('unlabelled')
else:
return class_ids.get('unlabelled')
def read_audio_from_filename(filename, target_sr):
audio, _ = librosa.load(filename, sr=target_sr, mono=True)
audio = audio.reshape(-1, 1)
return audio
def convert_data():
for i, wav_filename in enumerate(iglob(os.path.join(DATA_AUDIO_DIR, '**/**.wav'), recursive=True)):
class_id = extract_class_id(wav_filename)
audio_buf = read_audio_from_filename(wav_filename, target_sr=TARGET_SR)
# normalize mean 0, variance 1
audio_buf = (audio_buf - np.mean(audio_buf)) / np.std(audio_buf)
original_length = len(audio_buf)
print(i, wav_filename, original_length, np.round(np.mean(audio_buf), 4), np.std(audio_buf))
if original_length < AUDIO_LENGTH:
audio_buf = np.concatenate((audio_buf, np.zeros(shape=(AUDIO_LENGTH - original_length, 1))))
print('PAD New length =', len(audio_buf))
elif original_length > AUDIO_LENGTH:
audio_buf = audio_buf[0:AUDIO_LENGTH]
print('CUT New length =', len(audio_buf))
output_folder = OUTPUT_DIR_TRAIN
if i % 50 == 0:
output_folder = OUTPUT_DIR_TEST
output_filename = os.path.join(output_folder, str(i) + '.pkl')
out = {'class_id': class_id,
'audio': audio_buf,
'sr': TARGET_SR}
with open(output_filename, 'wb') as w:
pickle.dump(out, w)
if __name__ == '__main__':
convert_data()