app.py
3.44 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from flask import Flask, request, send_file
from extract_server import extract
from verification_server import load_model, load_enroll_embeddings, perform_verification
from identification_server import perform_identification
from enroll_server import split_enroll_and_test, enroll_per_spk
import os
import shutil
app = Flask(__name__)
log_dir = '../new_model4_merge' # Where the checkpoints are saved
embedding_dir = '../enroll_embeddings4_merge' # Where embeddings are saved
test_dir = '../feat_logfbank_nfilt40/test/' # Where test features are saved
# Settings
use_cuda = True # Use cuda or not
embedding_size = 128 # Dimension of speaker embeddings
cp_num = 50 # Which checkpoint to use?
n_classes = 348 # How many speakers in training data?
test_frames = 100 # Split the test utterance
model = load_model(use_cuda, log_dir, cp_num, embedding_size, n_classes)
embeddings = load_enroll_embeddings(embedding_dir)
test_path = './test.p'
spk_list = ['103F3021', '207F2088', '213F5100', '217F3038', '225M4062',\
'229M2031', '230M4087', '233F4013', '236M3043', '240M3063']
def enrollment():
try:
global embeddings
enroll_DB, test_DB = split_enroll_and_test(test_dir)
enroll_per_spk(use_cuda, test_frames, model, enroll_DB, embedding_dir)
embeddings = load_enroll_embeddings(embedding_dir)
except Exception as e:
print(e)
def verification(enroll_speaker):
test_speaker = 'TEST_SPEAKER'
thres = 0.95
# Perform the test
return perform_verification(use_cuda, model, embeddings, enroll_speaker,
test_path, test_frames, thres)
def identification():
best_spk = perform_identification(use_cuda, model, embeddings, test_path,
test_frames, spk_list)
return best_spk
@app.route('/enroll', methods=['POST', "GET"])
def enroll_controller():
if request.method == 'POST':
f = request.files['file']
enroll_speaker = request.form['enroll_speaker']
print(f.name)
f.save('./myrequest_enroll.wav')
extract('./myrequest_enroll.wav', enroll_speaker)
new_path = '../feat_logfbank_nfilt40/test/' + enroll_speaker + '/'
os.mkdir(new_path)
shutil.move('./enroll.p', new_path + 'enroll.p')
try:
enrollment()
spk_list.append(enroll_speaker)
return 'enroll_complete'
except:
return 'failed'
#return 'post'
return 'get'
@app.route('/verification', methods=['POST', "GET"])
def verfication_controller():
if request.method == 'POST':
f = request.files['file']
enroll_speaker = request.form['enroll_speaker']
print(f.name)
f.save('./myrequest.wav')
extract('./myrequest.wav')
speak, score = verification(enroll_speaker)
return score
#return 'post'
return 'get'
@app.route('/identification', methods=['POST', "GET"])
def identification_controller():
if request.method == 'POST':
f = request.files['file']
print(f.name)
f.save('./myrequest.wav')
extract('./myrequest.wav')
best_spk = identification()
return best_spk
#return 'post'
return 'get'
@app.route('/debugger', methods=['GET'])
def debugger():
return anything
@app.route('/robots.txt', methods=['GET'])
def antirobot():
return send_file('robots.txt')
if __name__ == '__main__':
app.run(host='0.0.0.0', port="7777", debug=True)