-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio_func.py
83 lines (60 loc) · 2.45 KB
/
audio_func.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
import librosa
import numpy as np
import pickle
from keras.models import model_from_json
import tensorflow
sample_rate = 22050
def extract_features(data):
result = np.array([])
# Zero Crossing Rate
zcr = np.mean(librosa.feature.zero_crossing_rate(y=data).T, axis=0)
result = np.hstack((result, zcr)) # stacking horizontally
# Chroma_stft
stft = np.abs(librosa.stft(data))
chroma_stft = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
result = np.hstack((result, chroma_stft)) # stacking horizontally
# MFCC
mfcc = np.mean(librosa.feature.mfcc(y=data, sr=sample_rate).T, axis=0)
result = np.hstack((result, mfcc)) # stacking horizontally
# Root Mean Square Value
rms = np.mean(librosa.feature.rms(y=data).T, axis=0)
result = np.hstack((result, rms)) # stacking horizontally
# Mel Spectogram
mel = np.mean(librosa.feature.melspectrogram(y=data, sr=sample_rate).T, axis=0)
result = np.hstack((result, mel)) # stacking horizontally
return result
def get_features_noeffect(path):
data, sample_rate = librosa.load(path)
res1 = extract_features(data)
result = np.array(res1)
return result
def get_nd_audio_cnn(path):
feature = get_features_noeffect(path)
feature = [feature]
feature = np.expand_dims(feature, axis=2)
return feature
def get_nd_audio(path):
feature = get_features_noeffect(path)
feature = [feature]
feature = np.array(feature)
return feature
def predict_emotion(model, path):
feature = get_nd_audio(path)
return model.predict(feature)[0]
def predict_emotion_cnn(model_path, model, path):
feature = get_nd_audio_cnn(path)
encoder = pickle.load(open(model_path + '/../encoder.pkl', 'rb'))
return encoder.inverse_transform(model.predict(feature))[0][0]
def load_model(model_path):
model = pickle.load(open(model_path, 'rb'))
return model
def load_model_cnn(model_path):
json_file = open(model_path + '/model_json.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(model_path + "/cnn.h5")
opt = tensorflow.keras.optimizers.RMSprop(lr=0.00001, decay=1e-6)
loaded_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
return loaded_model