-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpmdl_train.py
63 lines (46 loc) · 1.91 KB
/
pmdl_train.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
# -*- coding: utf-8 -*-
import os
import tempfile
from io import BytesIO
from scipy.io import wavfile
from pmdl.snowboy import SnowboyPersonalEnroll, SnowboyTemplateCut
ENROLL_ERRORS = {
-1: 'Error initializing streams or reading audio data',
1: 'Hotword is too long',
2: 'Hotword is too short',
}
DEFAULT_LANGUAGE = 'en'
PMDL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pmdl')
LNG_PATH = os.path.join(PMDL_PATH, 'lng')
LANGUAGES = {x.lower(): x for x in os.listdir(LNG_PATH)}
def check_snowboy(cut, enroll):
data = [
['NumChannels', cut.NumChannels(), enroll.NumChannels()],
['SampleRate', cut.SampleRate(), enroll.SampleRate()],
['BitsPerSample', cut.BitsPerSample(), enroll.BitsPerSample()]
]
for name, x, y in data:
if x != y:
raise SamplesException('', '{} in cut and enroll are different: {} != {}'.format(name, x, y))
def get_resource(lang):
return os.path.join(LNG_PATH, LANGUAGES.get(lang.lower(), DEFAULT_LANGUAGE), 'personal_enroll.res')
def generate(samples, lang=DEFAULT_LANGUAGE):
resource_filename = get_resource(lang)
tmp_file = tempfile.NamedTemporaryFile()
cut = SnowboyTemplateCut(resource_filename=resource_filename.encode())
enroll = SnowboyPersonalEnroll(resource_filename=resource_filename.encode(), model_filename=tmp_file.name.encode())
check_snowboy(cut, enroll)
errors = []
for sample in samples:
_, data = wavfile.read(BytesIO(sample))
data_cut = cut.CutTemplate(data.tobytes())
enroll_ans = enroll.RunEnrollment(data_cut)
errors.append(ENROLL_ERRORS.get(enroll_ans))
if any(errors):
raise SamplesException(errors, '')
with open(tmp_file.name) as fd:
return fd.read()
class SamplesException(Exception):
def __init__(self, samples, msg):
self.samples = samples
super(SamplesException, self).__init__(msg)