-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (61 loc) · 2.21 KB
/
app.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
from flask import Flask,jsonify, request, render_template
import numpy as np
import joblib
import keras
import librosa
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 = 'static/Emotion_Voice_Detection_Model66.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=1)
x = np.expand_dims(x, axis=0)
predictions=self.loaded_model.predict(x)
classes_x=np.argmax(predictions,axis=1)
a=self.convert_class_to_emotion(classes_x)
return a
@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
# Declare a Flask app
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def main():
# If a form is submitted
if request.method == "POST":
file = request.form['todo']
live_prediction = LivePredictions(file='static/Audios/'+file)
b=live_prediction.make_predictions()
return jsonify({'output':'You are ' + b + ', right?'})
else:
b = ""
return render_template("index.html")
# Running the app
if __name__ == '__main__':
app.run(debug = True)