File size: 1,797 Bytes
f28a05f
b7fde08
 
 
 
f28a05f
b7fde08
f28a05f
 
 
b7fde08
f28a05f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7fde08
f28a05f
 
 
 
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
from flask import Flask, jsonify, request
from librosa.feature import mfcc
import numpy as np
from scipy.io.wavfile import read
from tsai.all import *
from werkzeug.utils import secure_filename

app = Flask(__name__)
UPLOAD_FOLDER = "./"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER

def preprocess(audio):
    sr, a = audio
    m = mfcc(y=a.astype(float), sr=sr)
    l = []
    for a in m:
        l.append([])
        flag = True
        for b in a:
            if abs(b) < 1e-3 and flag:
                continue
            else:
                flag = False
                l[len(l) - 1].append(b)
    listy = []
    for a in l.reverse():
        listy.insert(0, [])
        for b in a.reverse():
            if abs(b) < 1e-3 and flag:
                continue
            else:
                flag = False
                listy[0].insert(0, b)
    temp = []
    for a in listy:
        if len(a) < 236:
            a.extend([0 for i in range(236 - len(a))])
            temp.append(a)
        elif len(a) > 236:
            c = (len(a) - 236) / 2
            temp.append(a[c : c + 236])
        else:
            temp.append(a)
    m = np.array(temp)
    return m
    
@app.route("/tone-vowel", methods=["POST"])
def tone_vowel_pred():
    if "blob" in request.files:
        file = request.files["blob"]
        filename = secure_filename(file.filename)
        filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
        file.save(filepath)
        audio = read(filepath)
        m = preprocess(audio)
        t = tone.predict(m).item(0)
        v = vowel.predict(m).item(0)
        return jsonify({"vowel": v, "tone": t})

if __name__ == "__main__":
    tone = load_minirocket("minirocket-tone-8981")
    vowel = load_minirocket("minirocket-vowel-9814")
    app.run(debug=True)