| import librosa |
| import numpy as np |
| import tensorflow as tf |
| from tensorflow.keras.models import load_model |
|
|
| emotion_model = load_model('models/best_model_emotion.h5') |
| depression_model = load_model('models/best_model_depression.h5') |
|
|
| emotion_labels = ['Angry', 'Calm', 'Fearful', 'Happy', 'Sad'] |
| def extract_features(audio_path): |
| X, sample_rate = librosa.load(audio_path,duration=2.5,sr=22050*2,offset=0.5) |
| features = librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=30) |
| pad_emotion = 216 - features.shape[1] |
| pad_depression = 2584 - features.shape[1] |
|
|
| if pad_emotion > 0: |
| emo_features = np.pad(features, [(0, 0), (0, pad_emotion)], mode='constant') |
| elif pad_emotion < 0: |
| emo_features = features[:,pad_emotion ] |
| else : |
| emo_features = features |
|
|
| if pad_depression > 0: |
| dep_features = np.pad(features, [(0, 0), (0, pad_depression)], mode='constant') |
| elif pad_depression < 0: |
| dep_features = features[:,pad_depression] |
| else: |
| dep_features = features |
|
|
| emo_features = np.expand_dims(emo_features, axis = 0) |
| dep_features = np.expand_dims(dep_features, axis = 0) |
|
|
| return emo_features, dep_features |
|
|
| def predict_emotion_and_depression(audio): |
| |
| print(audio) |
| print(len(audio)) |
| emo_features, dep_features = extract_features(audio) |
|
|
| |
| emotion_pred = emotion_model.predict(emo_features)[0] |
| print(emotion_pred) |
| emotion_index = np.argmax(emotion_pred) |
| emotion = emotion_labels[emotion_index] |
|
|
| |
| depression_pred = depression_model.predict(dep_features)[0] |
| depression = "Depressed" if depression_pred >= 0.5 else "Not Depressed" |
|
|
| return emotion, depression |
|
|
| def handler(request): |
| if request.method == 'POST': |
| |
| audio = request.data |
|
|
| |
| emotion, depression = predict_emotion_and_depression(audio) |
|
|
| |
| response = { |
| "emotion": emotion, |
| "depression": depression |
| } |
|
|
| return response |