File size: 1,394 Bytes
8701113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle
import numpy as np
import librosa
import soundfile

__model__ = None
__folder_path__ = "C:\\Users\\Abhay\\Downloads\\dataset\\"


def load_model():
    with open("assets/ser_model.pickle", 'rb') as f:
        model = pickle.load(f)
    global __model__
    __model__ = model


# Extract features (mfcc, chroma, mel) from a sound file
def extract_feature(file_name, mfcc, chroma, mel):
    with soundfile.SoundFile(file_name) 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(y=X, sr=sample_rate).T, axis=0)
            result = np.hstack((result, mel))
    return result

def predict_emotion(file):
    feature = extract_feature(file, mfcc=True, chroma=True, mel=True)
    emo = __model__.predict([feature])
    return emo[0]


load_model()
print(predict_emotion("C:\\Users\\Abhay\\Downloads\\dataset\\Actor_18\\03-01-03-01-02-02-18.wav"))