-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathlive_predictions.py
63 lines (52 loc) · 1.94 KB
/
live_predictions.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
"""
This file can be used to try a live prediction.
"""
import keras
import librosa
import numpy as np
from config import EXAMPLES_PATH
from config import MODEL_DIR_PATH
class LivePredictions:
"""
Main class of the application.
"""
def __init__(self, file):
"""
Init method is used to initialize the main parameters.
"""
self.file = file
self.path = MODEL_DIR_PATH + 'Emotion_Voice_Detection_Model.h5'
self.loaded_model = keras.models.load_model(self.path)
def make_predictions(self):
"""
Method to process the files and create your features.
"""
data, sampling_rate = librosa.load(self.file)
mfccs = np.mean(librosa.feature.mfcc(y=data, sr=sampling_rate, n_mfcc=40).T, axis=0)
x = np.expand_dims(mfccs, axis=2)
x = np.expand_dims(x, axis=0)
predictions = self.loaded_model.predict_classes(x)
print( "Prediction is", " ", self.convert_class_to_emotion(predictions))
@staticmethod
def convert_class_to_emotion(pred):
"""
Method to convert the predictions (int) into human readable strings.
"""
label_conversion = {'0': 'neutral',
'1': 'calm',
'2': 'happy',
'3': 'sad',
'4': 'angry',
'5': 'fearful',
'6': 'disgust',
'7': 'surprised'}
for key, value in label_conversion.items():
if int(key) == pred:
label = value
return label
if __name__ == '__main__':
live_prediction = LivePredictions(file=EXAMPLES_PATH + '03-01-01-01-01-02-05.wav')
live_prediction.loaded_model.summary()
live_prediction.make_predictions()
live_prediction = LivePredictions(file=EXAMPLES_PATH + '10-16-07-29-82-30-63.wav')
live_prediction.make_predictions()