-
Notifications
You must be signed in to change notification settings - Fork 14
/
ModelsTrainer.py
64 lines (52 loc) · 1.95 KB
/
ModelsTrainer.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
import os
import pickle
import warnings
import numpy as np
from sklearn.mixture import GMM
from FeaturesExtractor import FeaturesExtractor
from SilenceEliminator import SilenceEliminator
warnings.filterwarnings("ignore")
# initializatiions
gmm_destination = "SpeakerModels/"
trainpath = "TrainingData"
file_paths = []
try:
os.mkdir(gmm_destination)
except:
pass
# get file paths
for root, dirs, files in os.walk(trainpath):
speaker_file_paths = []
for file in files:
speaker_file_paths.append( os.path.join(root, file) )
if speaker_file_paths != []:
file_paths.append(speaker_file_paths)
# extracting features for each speaker (5 files per speakers)
for files in file_paths:
features = np.asarray(())
for filepath in files:
print (filepath)
# extract voice features
features_extractor = FeaturesExtractor()
silence_eliminator = SilenceEliminator()
try :
silence_eliminated_wave_file_path = "temp-" + os.path.basename(filepath).split('.')[0] + ".wav"
audio, duration_string = silence_eliminator.ffmpeg_silence_eliminator(filepath, silence_eliminated_wave_file_path)
vector = features_extractor.accelerated_get_features_vector(filepath, audio, 8000)
except:
continue
if features.size == 0:
features = vector
else:
try:
features = np.vstack((features, vector))
except:
print("ValueError: Shape mismatch")
# adapt gmm
gmm = GMM(n_components = 16, n_iter = 200, covariance_type='diag', n_init = 3)
gmm.fit(features)
# dumping the trained gaussian model
picklefile = gmm_destination + os.path.basename(filepath).split('_')[0] + ".gmm"
with open(picklefile, 'wb') as gmm_file:
pickle.dump(gmm, gmm_file)
print ('+ modeling completed for speaker:', picklefile," with data point = ", features.shape )