-
Notifications
You must be signed in to change notification settings - Fork 5
/
customHotword.py
81 lines (68 loc) · 2.42 KB
/
customHotword.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# By Psychokiller1888
import json
import os.path
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import pytoml
import signal
import snowboydecoder
import sys
SNIPS_CONFIG_PATH = '/etc/snips.toml'
interrupted = False
siteId = 'default'
mqttServer = '127.0.0.1'
mqttPort = 1883
model = ''
sensitivity = 0.4
hotwordId = 'default'
def loadConfigs():
global mqttServer, mqttPort, siteId, hotwordId
if os.path.isfile(SNIPS_CONFIG_PATH):
with open(SNIPS_CONFIG_PATH) as confFile:
configs = pytoml.load(confFile)
if 'mqtt' in configs['snips-common']:
if ':' in configs['snips-common']['mqtt']:
mqttServer = configs['snips-common']['mqtt'].split(':')[0]
mqttPort = int(configs['snips-common']['mqtt'].split(':')[1])
elif '@' in configs['snips-common']['mqtt']:
mqttServer = configs['snips-common']['mqtt'].split('@')[0]
mqttPort = int(configs['snips-common']['mqtt'].split('@')[1])
if 'bind' in configs['snips-audio-server']:
if ':' in configs['snips-audio-server']['bind']:
siteId = configs['snips-audio-server']['bind'].split(':')[0]
elif '@' in configs['snips-audio-server']['bind']:
siteId = configs['snips-audio-server']['bind'].split('@')[0]
if 'hotword_id' in configs['snips-hotword']:
hotwordId = configs['snips-hotword']['hotword_id']
else:
print('Snips configs not found')
def signal_handler(signal, frame):
global interrupted
interrupted = True
def interrupt_callback():
global interrupted
return interrupted
def onHotword():
global mqttServer, mqttPort, siteId
publish.single('hermes/hotword/{0}/detected'.format(hotwordId), payload=json.dumps({'siteId': siteId, 'modelId': 'default'}), hostname=mqttServer, port=1883)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == '__main__':
try:
model = sys.argv[1]
sensitivity = float(sys.argv[2])
except IndexError:
print('Please provide model name and sensitivity as argument')
sys.exit()
if not os.path.isfile('{}.pmdl'.format(model)):
print('The specified model doesn\'t exist')
sys.exit()
if sensitivity < 0 or sensitivity > 1:
print('Sensitivity should by a float between 0 and 1')
sys.exit()
loadConfigs()
detector = snowboydecoder.HotwordDetector('{}.pmdl'.format(model), sensitivity=sensitivity)
print('Listening...')
detector.start(detected_callback=onHotword, interrupt_check=interrupt_callback, sleep_time=0.03)
detector.terminate()