-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice-recognition_training.py
43 lines (35 loc) · 1.23 KB
/
voice-recognition_training.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
import os
import _pickle as cPickle # for python 3.x
# import cPickle # for python 2.x
import numpy as np
from scipy.io.wavfile import read
from sklearn.mixture import GMM
import python_speech_features as mfcc
from sklearn import preprocessing
import warnings
warnings.filterwarnings("ignore")
def get_MFCC(sr,audio):
features = mfcc.mfcc(audio,sr, 0.025, 0.01, 13,appendEnergy = False)
features = preprocessing.scale(features)
return features
#path to training data
# source = "pygender\\train_data\\youtube\\female\\"
source = "pygender\\train_data\\youtube\\male\\"
#path to save trained model
dest = "pygender\\"
files = [os.path.join(source,f) for f in os.listdir(source) if f.endswith('.wav')]
features = np.asarray(());
for f in files:
sr,audio = read(f)
vector = get_MFCC(sr,audio)
if features.size == 0:
features = vector
else:
features = np.vstack((features, vector))
gmm = GMM(n_components = 8, n_iter = 200, covariance_type='diag', n_init = 3)
gmm.fit(features)
picklefile = f.split("\\")[-2].split(".wav")[0]+".gmm"
# model saved as male.gmm
#cPickle.dump(gmm, open(dest + picklefile, ’w’))
cPickle.dump(gmm,open(dest + picklefile,'wb'))
print('modeling completed for gender:',picklefile)