forked from DingKe/yoyo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwakeup.py
94 lines (68 loc) · 2.6 KB
/
wakeup.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
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 13 22:47:21 2018
@author: keding
"""
from __future__ import print_function
import os
import time
from snowboy.snowboydetect import SnowboyDetect
from audio import AudioStream, play
class Wakeup(object):
pass
class Snowboy(Wakeup):
"""Keyword Detector by [snowboy](https://github.com/Kitt-AI/snowboy)
"""
def __init__(self, models, sensitivity, audio_gain=1.):
cur_dir = os.path.dirname(os.path.abspath(__file__))
resource = os.path.join(cur_dir, "snowboy/resources/common.res")
if type(models) != list:
models = [models]
models = [os.path.join(cur_dir, model) for model in models]
model_str = ','.join(models)
if type(sensitivity) != list:
sensitivity = [sensitivity] * len(models)
sensitivity_str = ','.join([str(s) for s in sensitivity])
assert len(models) == len(sensitivity), \
"number of hotwords in decoder_model (%d) and sensitivity " \
"(%d) does not match" % (len(models), len(sensitivity))
self.detector = SnowboyDetect(
resource_filename=resource.encode(),
model_str=model_str.encode())
self.detector.SetSensitivity(sensitivity_str.encode())
self.detector.SetAudioGain(audio_gain)
def start_detect(self, sleep_time=0.05, callback=None):
stream = AudioStream(chunk=16 * 100, buffer_bytes=16000 * 2)
stream.start()
while True:
data = stream.get()
if len(data) == 0:
time.sleep(sleep_time)
continue
ans = self.detector.RunDetection(data)
if ans <= 0:
time.sleep(sleep_time)
else:
break
stream.stop()
if callback:
callback(ans)
return ans
def test_snowboy():
def callback(keyword):
ding = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'snowboy/resources/ding.wav')
dong = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'snowboy/resources/dong.wav')
if keyword == 1:
play(ding)
else:
play(dong)
models = ['snowboy/resources/models/snowboy.umdl',
'snowboy/resources/models/alexa_02092017.umdl']
kws = Snowboy(models, 0.5)
print('Wake me up~')
kws.start_detect(callback=callback)
if __name__ == '__main__':
import pytest
pytest.main([__file__, '-s'])