-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.py
70 lines (62 loc) · 2.39 KB
/
sound.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
from random import sample
import librosa as lb
import numpy as np
import soundfile as sf
import os, glob, pickle
import pickle
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
emotional_labels = {
'01': 'neutral',
'02': 'calm',
'03': 'happy',
'04': 'sad',
'05': 'angry',
'06': 'fearful',
'07': 'disgust',
'08': 'surprised'
}
focussed_emotional_labels = ['calm', 'happy', 'fearful', 'disgust']
def audio_features(file_name, mfcc, chroma, mel):
print(file_name)
with sf.SoundFile(file_name) as sound_file:
X = sound_file.read(dtype = "float32")
sample_rate = sound_file.samplerate
result = np.array([])
if mfcc:
mfccs = np.mean(lb.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
result = np.hstack((result, mfccs))
if chroma:
stft = np.abs(lb.stft(X))
chroma = np.mean(lb.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
result = np.hstack((result, chroma))
if mel:
mel = np.mean(lb.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
result = np.hstack((result, mel))
return result
def loading_audio_data(test_size=0.2):
x,y = [],[]
for file in glob.glob("./data/Actor_*/*.wav"):
# print(file)
file_name = os.path.basename(file)
emotion = emotional_labels[file_name.split("-")[2]]
if emotion not in focussed_emotional_labels:
continue
feature = audio_features(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=101)
def main():
X_train, X_test, y_train, y_test = loading_audio_data(test_size=0.25)
# print(f'Features extracted: {X_train.shape[1]}')
model = MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08,
hidden_layer_sizes=(300,), learning_rate='adaptive', max_iter=500)
model.fit(X_train,y_train)
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
y_pred = model.predict(X_test)
print(f'Accuracy: {accuracy_score(y_test, y_pred) * 100}%')
print(classification_report(y_true=y_test, y_pred=y_pred))
if __name__ == '__main__':
main()