|
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 |
|
|
|
|
|
|
|
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")) |
|
|