forked from Renovamen/Speech-Emotion-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLibrosa_Feature.py
226 lines (169 loc) · 6.13 KB
/
Librosa_Feature.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
import os
import re
import sys
import librosa
import librosa.display
from random import shuffle
import numpy as np
from typing import Tuple
import pickle
import pandas as pd
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from Config import Config
def features(X, sample_rate):
stft = np.abs(librosa.stft(X))
# fmin 和 fmax 对应于人类语音的最小最大基本频率
pitches, magnitudes = librosa.piptrack(X, sr=sample_rate, S=stft, fmin=70, fmax=400)
pitch = []
for i in range(magnitudes.shape[1]):
index = magnitudes[:, 1].argmax()
pitch.append(pitches[index, i])
pitch_tuning_offset = librosa.pitch_tuning(pitches)
pitchmean = np.mean(pitch)
pitchstd = np.std(pitch)
pitchmax = np.max(pitch)
pitchmin = np.min(pitch)
# 频谱质心
cent = librosa.feature.spectral_centroid(y=X, sr=sample_rate)
cent = cent / np.sum(cent)
meancent = np.mean(cent)
stdcent = np.std(cent)
maxcent = np.max(cent)
# 谱平面
flatness = np.mean(librosa.feature.spectral_flatness(y=X))
# 使用系数为50的MFCC特征
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=50).T, axis=0)
mfccsstd = np.std(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=50).T, axis=0)
mfccmax = np.max(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=50).T, axis=0)
# 色谱图
chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
# 梅尔频率
mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T, axis=0)
# ottava对比
contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T, axis=0)
# 过零率
zerocr = np.mean(librosa.feature.zero_crossing_rate(X))
S, phase = librosa.magphase(stft)
meanMagnitude = np.mean(S)
stdMagnitude = np.std(S)
maxMagnitude = np.max(S)
# 均方根能量
rmse = librosa.feature.rmse(S=S)[0]
meanrms = np.mean(rmse)
stdrms = np.std(rmse)
maxrms = np.max(rmse)
ext_features = np.array([
flatness, zerocr, meanMagnitude, maxMagnitude, meancent, stdcent,
maxcent, stdMagnitude, pitchmean, pitchmax, pitchstd,
pitch_tuning_offset, meanrms, maxrms, stdrms
])
ext_features = np.concatenate((ext_features, mfccs, mfccsstd, mfccmax, chroma, mel, contrast))
return ext_features
def extract_features(file, pad = False):
X, sample_rate = librosa.load(file, sr = None)
max_ = X.shape[0] / sample_rate
if pad:
length = (max_ * sample_rate) - X.shape[0]
X = np.pad(X, (0, int(length)), 'constant')
return features(X, sample_rate)
def get_max_min(files):
min_, max_ = 100, 0
for file in files:
sound_file, samplerate = librosa.load(file, sr = None)
t = sound_file.shape[0] / samplerate
if t < min_:
min_ = t
if t > max_:
max_ = t
return max_, min_
'''
get_data_path():
获取所有音频的路径
输入:
data_path: 数据集文件夹路径
输出:
所有音频的路径
'''
def get_data_path(data_path: str):
wav_file_path = []
cur_dir = os.getcwd()
sys.stderr.write('Curdir: %s\n' % cur_dir)
os.chdir(data_path)
# 遍历文件夹
for _, directory in enumerate(Config.CLASS_LABELS):
os.chdir(directory)
# 读取该文件夹下的音频
for filename in os.listdir('.'):
if not filename.endswith('wav'):
continue
filepath = os.getcwd() + '/' + filename
wav_file_path.append(filepath)
os.chdir('..')
os.chdir(cur_dir)
shuffle(wav_file_path)
return wav_file_path
'''
load_feature():
从 csv 加载特征数据
输入:
feature_path: 特征文件路径
train: 是否为训练数据
输出:
训练数据、测试数据和对应的标签
'''
def load_feature(feature_path: str, train: bool):
features = pd.DataFrame(data = joblib.load(feature_path), columns = ['file_name', 'features', 'emotion'])
X = list(features['features'])
Y = list(features['emotion'])
if train == True:
# 标准化数据
scaler = StandardScaler().fit(X)
# 保存标准化模型
joblib.dump(scaler, Config.MODEL_PATH + 'SCALER_LIBROSA.m')
X = scaler.transform(X)
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)
return x_train, x_test, y_train, y_test
else:
# 标准化数据
# 加载标准化模型
scaler = joblib.load(Config.MODEL_PATH + 'SCALER_LIBROSA.m')
X = scaler.transform(X)
return X
'''
get_data():
提取所有音频的特征: 遍历所有文件夹, 读取每个文件夹中的音频, 提取每个音频的特征,把所有特征保存在 feature_path 中
输入:
data_path: 数据集文件夹路径
feature_path: 保存特征的路径
train: 是否为训练数据
输出:
train = True:
训练数据、测试数据特征和对应的标签
train = False:
预测数据特征
'''
def get_data(data_path: str, feature_path: str, train: bool):
if(train == True):
files = get_data_path(data_path)
max_, min_ = get_max_min(files)
mfcc_data = []
for file in files:
label = re.findall(".*-(.*)-.*", file)[0]
# 三分类
# if(label == "sad" or label == "neutral"):
# label = "neutral"
# elif(label == "angry" or label == "fear"):
# label = "negative"
# elif(label == "happy" or label == "surprise"):
# label = "positive"
features = extract_features(file, max_)
mfcc_data.append([file, features, Config.CLASS_LABELS.index(label)])
else:
features = extract_features(data_path)
mfcc_data = [[data_path, features, -1]]
cols = ['file_name', 'features', 'emotion']
mfcc_pd = pd.DataFrame(data = mfcc_data, columns = cols)
pickle.dump(mfcc_data, open(feature_path, 'wb'))
return load_feature(feature_path, train = train)