-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
64 lines (53 loc) · 1.88 KB
/
server.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
from flask import Flask, request, jsonify
from flask_cors import CORS
import pickle
import soundfile
import numpy as np
import librosa
import sys
import os
import warnings
warnings.simplefilter("ignore")
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
model = pickle.load(open('Speech_Emotions_Recognition_Model.pkl', 'rb'))
# Feature Extraction of Audio Files Function
# Extract features (mfcc, chroma, mel) from a sound file
def extract_feature(f, mfcc, chroma, mel):
with soundfile.SoundFile(f) 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(
X, sr=sample_rate).T, axis=0)
result = np.hstack((result, mel))
return result
@app.route('/', methods=['GET'])
def hello():
return jsonify('Hello world, use the /api route and read the README.md please')
@app.route('/api', methods=['GET', 'POST'])
def predict():
# data = request.get_json(force=True)
f = request.files.get('audioFile', None)
# print(data, file=sys.stdout)
print(f, file=sys.stdout)
x = []
x.append(extract_feature(f, mfcc=True, chroma=True, mel=True))
prediction = model.predict(np.array(x))
output = prediction[0]
return jsonify(output)
port = int(os.environ.get('PORT', 5000))
debug = bool(os.environ.get('DEBUG', True))
if __name__ == '__main__':
app.run(port=port, debug=debug)