| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import argparse
- import struct
- import wave
- from threading import Thread
- import pvrhino
- from pvrecorder import PvRecorder
- import platform
- import configparser
- import os
- from VoicePlay import Voice
- class Rhino(Thread):
- instance = None
- @staticmethod
- def getInstance():
- if Rhino.instance==None:
- Rhino.instance = Rhino()
- return Rhino.instance
- def __init__(self):
- super(Rhino, self).__init__()
- print("Rhino.__init__")
- config = configparser.ConfigParser()
- config.read('config/pico.ini')
- self._lang = "de"
- if "lang" in config["global"]:
- self._lang = config["global"]["lang"]
- self._os = platform.system().lower()
- if "ApiKey" in config["PicoVoice"]:
- print(" - Api Key: " + config["PicoVoice"]["ApiKey"])
- self._access_key = config["PicoVoice"]["ApiKey"]
- print(" - Library: " + pvrhino.LIBRARY_PATH)
- self._library_path = pvrhino.LIBRARY_PATH
- #if "Model" in config["PicoVoice"]:
- # print(" - Model: " + config["PicoVoice"]["Model"])
- # self._model_path = config["PicoVoice"]["Model"]
- self._model_path = 'ressources/model/porcupine/' + config["Rhino"]["model"]
- #self._context_path = context_path
- self._require_endpoint = None
- if "InputDeviceIndex" in config["Hardware"]:
- print(" - Audio Device Index: " + config["Hardware"]["InputDeviceIndex"])
- self._audio_device_index = int(config["Hardware"]["InputDeviceIndex"])
- else:
- self._audio_device_index = None
- print(" - Output Path")
- self._output_path = None
- def run(self, contextFile, intentHandler, startListening = None, stopListening = None):
- """
- Creates an input audio stream, instantiates an instance of Rhino object, and infers the intent from spoken
- commands.
- """
- print("Rhino.run("+contextFile+", func)")
- rhino = None
- recorder = None
- wav_file = None
- # Find Context File
- bFoundContextFile = False
- for iMajor in range(4,0,-1):
- for iMinor in range(10,-1,-1):
- for iRev in range(10,-1,-1):
- cf = "ressources/models/context/" + contextFile + "_" + self._lang + "_" + self._os + "_v" + str(iMajor) + "_" + str(iMinor) + "_" + str(iRev) + ".rhn"
- print("Try: "+cf)
- if os.path.isfile(cf):
- print(" FOUND")
- bFoundContextFile = True
- break
- if bFoundContextFile == True:
- break
- if bFoundContextFile == True:
- break
- if bFoundContextFile == True:
- print(" - Context File Path: " + cf)
- else:
- print(" - Context File not found, use Version 2.0.0")
- cf = "ressources/models/context/" + contextFile + "_" + self._lang + "_" + self._os + "_v2_0_0.rhn"
- try:
- print(" - Create Rhino")
- rhino = pvrhino.create(
- access_key=self._access_key,
- library_path=self._library_path,
- model_path=self._model_path,
- context_path=cf,
- require_endpoint=self._require_endpoint
- )
- print(" - Create Recorder")
- recorder = PvRecorder(device_index=self._audio_device_index, frame_length=rhino.frame_length)
- print(" - Start Recorder")
- recorder.start()
- if self._output_path is not None:
- wav_file = wave.open(self._output_path, "w")
- wav_file.setparams((1, 2, 16000, 512, "NONE", "NONE"))
- print(rhino.context_info)
- print()
- print("Using device: '"+recorder.selected_device+"'")
- print("Listening...")
- print()
- bRunning = True
- while bRunning:
- if startListening is not None:
- startListeningResult = startListening({
- "library": self._library_path,
- "model_path": self._model_path,
- "context_path": self._context_path,
- "require_endpoint": self._require_endpoint,
- "rhino": rhino,
- "recorder": recorder,
- "wave_file":self._output_path
- })
- if startListeningResult is not None and startListeningResult == False:
- bRunning=False
- recorder.stop()
- break
- pcm = recorder.read()
- if wav_file is not None:
- wav_file.writeframes(struct.pack("h" * len(pcm), *pcm))
- is_finalized = rhino.process(pcm)
- if is_finalized:
- inference = rhino.get_inference()
- recorder.stop()
- if inference.is_understood:
- print('{')
- print(" intent : '"+inference.intent+"'")
- print(' slots : {')
- for slot, value in inference.slots.items():
- print(" "+slot+" : '"+value+"'")
- print(' }')
- print('}\n')
- if intentHandler is not None:
- res = intentHandler(inference)
- if res==False and res != None:
- bRunning = False
- else:
- recorder.start()
- else:
- print("Didn't understand the command.\n")
- bRunning = False
- print("Stop listening")
- if stopListening is not None:
- stopListening()
- except pvrhino.RhinoInvalidArgumentError as e:
- print("One or more arguments provided to Rhino is invalid: {\n" +
- "\t"+self._access_key+"\n" +
- "\t"+self._library_path+"\n" +
- "\t"+self._model_path+"\n" +
- "\t"+cf+"\n" +
- "\t"+str(self._require_endpoint)+"\n" +
- "}")
- print("If all other arguments seem valid, ensure that '"+self._access_key+"' is a valid AccessKey")
- Voice.getInstance().Say("Ausnahmefehler in Rhino Argument")
- raise e
- except pvrhino.RhinoActivationError as e:
- print("AccessKey activation error")
- raise e
- except pvrhino.RhinoActivationLimitError as e:
- print("AccessKey '"+self._access_key+"' has reached it's temporary device limit")
- Voice.getInstance().Say("Ausnahmefehler in Rhino - Begränzung erreicht")
- raise e
- except pvrhino.RhinoActivationRefusedError as e:
- print("AccessKey '"+self._access_key+"' refused")
- raise e
- except pvrhino.RhinoActivationThrottledError as e:
- print("AccessKey '"+self._access_key+"' has been throttled")
- Voice.getInstance().Say("Ausnahmefehler in Rhino - Anfragenbegränzung erreicht")
- raise e
- except pvrhino.RhinoError as e:
- print("Failed to initialize Rhino")
- Voice.getInstance().Say("Ausnahmefehler in Rhino - Initialisierung fehlgeschlagen")
- raise e
- except KeyboardInterrupt:
- print('Stopping ...')
- finally:
- if recorder is not None:
- recorder.delete()
- if rhino is not None:
- rhino.delete()
- if wav_file is not None:
- wav_file.close()
- @staticmethod
- def formatChar(c:str):
- if c=="Ah": return "a"
- elif c=="Be": return "b"
- elif c=="Zeh": return "c"
- elif c=="De": return "d"
- elif c=="E": return "e"
- elif c=="Ef": return "f"
- elif c=="Geh": return "g"
- elif c=="H": return "h"
- elif c=="Jott": return "j"
- elif c=="Ka": return "k"
- elif c=="El": return "l"
- elif c=="Em": return "m"
- elif c=="En": return "n"
- elif c=="O": return "o"
- elif c=="Pe": return "p"
- elif c=="Kuh": return "q"
- elif c=="Er": return "r"
- elif c=="Es": return "s"
- elif c=="Tee": return "t"
- elif c=="U": return "u"
- elif c=="Fau": return "v"
- elif c=="We": return "w"
- elif c=="Ix": return "x"
- elif c=="Yipsilon": return "y"
- elif c=="Zett": return "z"
- elif c=="Null": return "0"
- elif c=="eins": return "1"
- elif c=="zwei": return "2"
- elif c=="drei": return "3"
- elif c=="vier": return "4"
- elif c=="fünf": return "5"
- elif c=="sechs": return "6"
- elif c=="sieben": return "7"
- elif c=="acht": return "8"
- elif c=="neun": return "9"
- elif c=="komma": return ","
- elif c=="punkt": return "."
- elif c=="doppelpunkt": return ":"
- elif c=="Dollar": return "$"
- elif c=="Raute": return "#"
- elif c=="Äh": return "ä"
- elif c=="Ö": return "ö"
- elif c=="Ü": return "ü"
- elif c=="Ausrufezeichen": return "!"
- elif c=="Und": return "&"
- elif c=="Slash": return "/"
- elif c=="Backslash": return "\\"
- elif c=="Plus": return "+"
- elif c=="Minus": return "-"
- elif c=="Unterstrich": return "_"
- elif c=="Fu": return "Foo"
- else: return c.lower()
|