Psychokiller1888 / snips-custom-hotword

Using Snowboy to customize the snips hotword
GNU General Public License v3.0
21 stars 5 forks source link

Multiple hot words patch #6

Open a1higgins-oss opened 5 years ago

a1higgins-oss commented 5 years ago

Awesome script and tutorial. Thanks! I added support for multiple hot words and umdl files. Not tested other than for my setup, but FYI in case you want it. Note the double sensitivity setting needed for jarvis.umdl.

diff --git a/customHotword.py b/customHotword.py
index 4ce1445..e66c8ad 100644
--- a/customHotword.py
+++ b/customHotword.py
@@ -17,8 +17,8 @@ interrupted = False
 siteId = 'default'
 mqttServer = '127.0.0.1'
 mqttPort = 1883
-model = ''
-sensitivity = 0.4
+models = []
+sensitivities = []
 hotwordId = 'default'

 def loadConfigs():
@@ -59,23 +59,28 @@ def onHotword():
 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()
+        num_args = (len(sys.argv)-1)/2
+        for i in range(0, num_args):
+               try:
+                       models.append(sys.argv[1+(i*2)])
+                       sensitivities.extend(sys.argv[2+(i*2)].split(','))
+               except IndexError:
+                       print('Please provide model name and sensitivity pair as argument')
+                       sys.exit()

-       if not os.path.isfile('{}.pmdl'.format(model)):
-               print('The specified model doesn\'t exist')
-               sys.exit()
+        for i in range(0, len(models)):
+               if not os.path.isfile(models[i]):
+                       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()
+        for i in range(0, len(sensitivities)):
+                sensitivities[i] = float(sensitivities[i])
+               if sensitivities[i] < 0 or sensitivities[i] > 1:
+                       print('Sensitivity should by a float between 0 and 1')
+                       sys.exit()

        loadConfigs()
-       detector = snowboydecoder.HotwordDetector('{}.pmdl'.format(model), sensitivity=sensitivity)
+       detector = snowboydecoder.HotwordDetector(models, sensitivity=sensitivities)
        print('Listening...')
-       detector.start(detected_callback=onHotword, interrupt_check=interrupt_callback, sleep_time=0.03)
+       detector.start(detected_callback=[onHotword]*len(sensitivities), interrupt_check=interrupt_callback, sleep_time=0.03)
        detector.terminate()

It supports a snipsCustomHotword.service ExecStart setting like:

[Service] ExecStart=/usr/bin/python customHotword.py computer.umdl 0.5 jarvis.umdl 0.5,0.5