app.py 3.44 KB
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)