所以我复制了一些代码来尝试在 python 中弄清楚机器学习。总的来说效果很好,但现在我不知道如何使用它(输入我自己的文件并分析它)。
import librosa
import numpy as np
import soundfile
import sklearn
import os, glob, pickle
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
def extract_feture(filepath,mfcc,chroma,mel):
with soundfile.SoundFile(filepath) as sound_file:
X = sound_file.read(dtype="float32")
sample_rate=sound_file.samplerate
if chroma:
stft = np.abs(librosa.stft(X))
result = np.array([])
if mfcc:
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40) .T, axis=0)
result = np.hstack((result, mfccs))
if chroma:
chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
result = np.hstack((result, chroma))
if mel:
mel = np.mean(librosa.feature.melspectrogram(X, sr= sample_rate).T,axis=0)
result = np.hstack((result, mel))
return result
emotions = {
'01':'neutral',
'02':'calm',
'03':'happy',
'04':'sad',
'05': 'angry',
'06': 'fearful',
'07': 'disgust',
'08': 'surprised'
}
observed_emotions =['calm', 'happy', 'fearful', 'disgust']
def load_data(test_size=0.2):
x, y = [], []
for file in glob.glob("/home/adobug2/Documents/ravdess-data/Actor_*/*.wav"):
file_name = os.path.basename(file)
emotion = emotions[file_name.split("-")[2]]
if emotion not in observed_emotions:
continue
feature = extract_feture(file, mfcc=True, chroma=True, mel=True)
x.append(feature)
y.append(emotion)
return train_test_split(np.array(x), y, test_size=test_size, random_state=9)
x_train,x_test,y_train,y_test=load_data(test_size=0.25)
print((x_train.shape[0], x_test.shape[0]))
POPMUISE
相关分类