-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecognize_voice.py
193 lines (143 loc) · 4.61 KB
/
recognize_voice.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import pyaudio
from IPython.display import Audio, display, clear_output
import wave
from scipy.io.wavfile import read
from sklearn.mixture import GMM
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import os
import pickle
from sklearn import preprocessing
import python_speech_features as mfcc
#Calculate and returns the delta of given feature vector matrix
def calculate_delta(array):
rows,cols = array.shape
deltas = np.zeros((rows,20))
N = 2
for i in range(rows):
index = []
j = 1
while j <= N:
if i-j < 0:
first = 0
else:
first = i-j
if i+j > rows -1:
second = rows -1
else:
second = i+j
index.append((second,first))
j+=1
deltas[i] = ( array[index[0][0]]-array[index[0][1]] + (2 * (array[index[1][0]]-array[index[1][1]])) ) / 10
return deltas
#convert audio to mfcc features
def extract_features(audio,rate):
mfcc_feat = mfcc.mfcc(audio,rate, 0.025, 0.01,20,appendEnergy = True, nfft=1200)
mfcc_feat = preprocessing.scale(mfcc_feat)
delta = calculate_delta(mfcc_feat)
#combining both mfcc features and delta
combined = np.hstack((mfcc_feat,delta))
return combined
def recognize(filename,username):
# Voice Authentication
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 3
#FILENAME = "./test.wav"
#FILENAME = "./flute.wav"
#FILENAME = "./piano.wav"
FILENAME = filename
modelpath = "./gmm_models/"
gmm_files = [os.path.join(modelpath,fname) for fname in
os.listdir(modelpath) if fname.endswith('.gmm')]
models = [pickle.load(open(fname,'rb')) for fname in gmm_files]
speakers = [fname.split("/")[-1].split(".gmm")[0] for fname
in gmm_files]
if len(models) == 0:
print("No Users in the Database!")
return
#read test file
sr,audio = read(FILENAME)
# extract mfcc features
vector = extract_features(audio,sr)
log_likelihood = np.zeros(len(models))
#checking with each model one by one
#scorer = 0
for i in range(len(models)):
gmm = models[i]
scores = np.array(gmm.score(vector))
log_likelihood[i] = scores.sum()
if speakers[i] == username:
scorer = log_likelihood[i]
print("\n\n\n")
print(speakers[i])
print(log_likelihood[i])
pred = np.argmax(log_likelihood)
identity = speakers[pred]
maximum = max(log_likelihood)
minimum = min(log_likelihood)
score = (2*(scorer - minimum)/(maximum - minimum)) - 1
#score = int(score)
# if voice not recognized than terminate the process
#if identity == 'unknown':
# print("Not Recognized! Try again...")
# return
#print( "Recognized as - ", identity)
return identity,score
def reconize_with_model(filename,model,username):
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 3
#FILENAME = "./test.wav"
#FILENAME = "./flute.wav"
#FILENAME = "./piano.wav"
FILENAME = filename
modelpath = model
#modelpath = "./temp/models/"
#gmm_files = [os.path.join(modelpath,fname) for fname in
#os.listdir(modelpath) if fname.endswith('.gmm')]
models = pickle.load(open(modelpath,'rb'))
#speakers = [fname.split("/")[-1].split(".gmm")[0] for fname
#in gmm_files]
#if len(models) == 0:
#print("No Users in the Database!")
#return
#read test file
sr,audio = read(FILENAME)
# extract mfcc features
vector = extract_features(audio,sr)
log_likelihood = 0
#checking with each model one by one
#scorer = 0
#for i in range(len(models)):
gmm = models
scores = np.array(gmm.score(vector))
log_likelihood = scores.sum()/len(scores)
#if speakers[i] == username:
#scorer = log_likelihood[i]
print("\n\n\n")
#print(speakers[i])
print(log_likelihood)
#pred = np.argmax(log_likelihood)
if log_likelihood > -4:
identity = username
else :
identity = "Unknown"
maximum = 0
minimum = -30
score = (2*(log_likelihood - minimum)/(maximum - minimum)) - 1
#score = int(score)
#score = log_likelihood
# if voice not recognized than terminate the process
#if identity == 'unknown':
# print("Not Recognized! Try again...")
# return
#print( "Recognized as - ", identity)
return identity,score
#if __name__ == '__main__':
#recognize()