-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpeakerRecognition.py
71 lines (51 loc) · 1.92 KB
/
SpeakerRecognition.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
import time
import librosa
import config
from sklearn.externals import joblib
from Utils import LoggingHelper
from Utils import FeatureExtractionHelper
import itertools
import operator
def find_most_common(l):
# get an iterable of (item, iterable) pairs
SL = sorted((x, i) for i, x in enumerate(l))
# print 'SL:', SL
groups = itertools.groupby(SL, key=operator.itemgetter(0))
# auxiliary function to get "quality" for an item
def _auxfun(g):
item, iterable = g
count = 0
min_index = len(l)
for _, where in iterable:
count += 1
min_index = min(min_index, where)
# print 'item %r, count %r, minind %r' % (item, count, min_index)
return count, -min_index
# pick the highest-count/earliest item
return max(groups, key=_auxfun)[0]
def find_the_speaker(sample_array, samp_rate, flen, hlen):
# extract features from sample array
features = FeatureExtractionHelper.extract_features(sample_array, samp_rate, flen, hlen)
# get the model from pkl file
model = joblib.load('model.pkl')
# predict the speaker by using the model
predicted_labels = model.predict(features)
result = find_most_common(predicted_labels)
return result
def main(audio_path):
# set necessary properties
samp_rate = config.Audio.samp_rate
flen = config.Audio.flen
hlen = config.Audio.hlen
# get the audio as sample array
sample_array, sr = librosa.load(audio_path, sr=config.Audio.samp_rate, mono=True)
# get the current time
start_time = time.time()
# recognize the speaker
result = find_the_speaker(sample_array, samp_rate, flen, hlen)
# compute elapsed time for finding the speaker
elapsed_time = time.time() - start_time
LoggingHelper.log("The speaker is: " + result + " ( found in " + str(elapsed_time) + " )")
return result
if __name__ == '__main__':
main(config.Paths.TEST_PATH)