File size: 1,850 Bytes
f831146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import numpy as np
from predictions import get_embeddings
from utils.preprocessing import extract_fbanks

os.environ['KMP_DUPLICATE_LIB_OK']='True'
 
def load_data_speaker(labId):
    speaker_path =f'./modelDir/{labId}/speaker/'
    if os.path.exists(speaker_path):
        data_dict = {}
        for dir_name in os.listdir(speaker_path):
            dir_path = os.path.join(speaker_path, dir_name)
            if os.path.isdir(dir_path):
                sub_data = {}
                for file_name in os.listdir(dir_path):
                    if file_name.endswith('.npy'):
                        file_path = os.path.join(dir_path, file_name)
                        key = file_name.replace('.npy', '')  # Sử dụng tên file làm key
                        value = np.load(file_path)  # Load file .npy
                        sub_data[key] = value
                
                data_dict[dir_name] = sub_data
                
        return data_dict
    else:
        return "folder do not exist"


async def show_all_speaker(labId):
    speaker_path =f'./modelDir/{labId}/speaker/'
    if not os.path.exists(speaker_path):
        os.makedirs(speaker_path)
    list_user=os.listdir(speaker_path)
    return {
        "result": list_user
    }

async def add_more_speaker(speech_file_path, speaker_name, labId):
    speaker_path =f'./modelDir/{labId}/speaker/'
    dir_ = speaker_path + speaker_name
    if not os.path.exists(dir_):
        os.makedirs(dir_)

    fbanks = extract_fbanks(speech_file_path)
    embeddings = get_embeddings(fbanks)
    print('shape of embeddings: {}'.format(embeddings.shape), flush=True)
    mean_embeddings = np.mean(embeddings, axis=0)
    np.save(speaker_path+speaker_name+'/embeddings.npy',mean_embeddings)
    list_user=os.listdir(speaker_path)
    return {
        "result": list_user
    }