def google_match(input): log.debug("Google Search...") try: cmd_google.ex(input) cmd = {"name": "google", "text": input, "input": input} return cmd except: log.debug("No result from Google Search...")
def quit(self): log.debug("Quitting...") # set quit to True self.stop = True self.clean() log.info("Bye!")
def listen(): global audio # use microphone as raw input with sr.Microphone() as raw_microphone_input: log.debug("Listening to ambient...") # listen to raw microphone input audio = recognizer.listen(raw_microphone_input) return audio
def listen_for_keyword(): log.debug("Keyword loop...") # global processes global keyword_detected global new_process keyword_detected = False new_process = True log.info("Waiting for '{}'...".format(settings.KEYWORD)) while True: # quit when keyword is detected if keyword_detected: break # if new process is requested if new_process: new_process = False # start async keyword recognizing thread (start new process) threading.Thread(target=recognize_for_keyword).start() # play activation sound tts.play_mp3(settings.ACTIVATION_SOUND_PATH) return True
def greet(self): log.debug("Greeting...") print(".........................") print(" ") print(colored_ascii, end=" ") print(" ") print(colored_ascii_1, end=" ") print(" ") print(".........................") print("Basic usage:") print( replying.get_reply("greet", system=True).format(settings.KEYWORD)) print( replying.get_reply("greet", system=True, stage=1).format(settings.KEYWORD)) print( replying.get_reply("greet", system=True, stage=2).format(settings.KEYWORD)) tts.say(replying.get_reply("greet", system=True, stage=3))
def recognize(audio): output = None # recognize microphone input using selected speech engine log.debug("Recognizing audio...") # check speech engine if settings.STT_ENGINE == "google": # try recognizing audio from google try: # settings query output = recognizer.recognize_google(audio, language=settings.LANGUAGE) # return output as text except sr.UnknownValueError: log.debug("Speech engine couldn't resolve audio") except sr.RequestError: log.error("You need a WiFi connection for Google STT") except: traceback.print_exc() log.error("Unkown exception") finally: return output
def get_reply(cmd, system=False, module=False, stage=0): # keep console clean if not system: log.debug("Getting reply...") # read replies file # and compile json data with open(settings.REPLIES_FILE_PATH, "r+", encoding="utf-8") as replies_file: replies_file_data = json.load(replies_file) # check if call is a system call if system: # get reply in given language and requested stage if module: reply = replies_file_data["system"][cmd[0]][cmd[1]][ settings.LANGUAGE]["data"][stage] else: reply = replies_file_data["system"][cmd][ settings.LANGUAGE]["data"][stage] else: # get reply in given language and requested stage reply = replies_file_data[cmd][settings.LANGUAGE]["data"][stage] # return matched reply return reply
def greet(self): log.debug("Greeting...") print("") print(r" __ __ ") print(r" /\ \__ /\ \__ ") print(r" ___ \ \ ,_\ \ \ ,_\ ___ ") print(r" / __`\ \ \ \/ \ \ \/ / __`\ ") print(r"/\ \ \ \ \ \ \_ \ \ \_ /\ \ \ \ ") print(r"\ \____/ \ \__\ \ \__\\ \____/") print(r" \/___/ \/__/ \/__/ \/___/ ") print("") print("Basic usage:") print( replying.get_reply("greet", system=True).format(settings.KEYWORD)) print( replying.get_reply("greet", system=True, stage=1).format(settings.KEYWORD)) print( replying.get_reply("greet", system=True, stage=2).format(settings.KEYWORD)) tts.say(replying.get_reply("greet", system=True, stage=3))
def run(self): # greeting self.greet() # global loop while True: # check if quit if self.stop: break try: # listen for keyword # wake up on recognized keyword if stt.listen_for_keyword(): log.debug("Back in loop...") # listen for text input audio = stt.listen() # try resolving input audio_input = stt.recognize(audio) # check if text input received if not audio_input: log.info("Couldn't resolve audio...") continue else: log.info("Catched input '{}'...".format(audio_input)) # find match cmd = matching.get_match(audio_input) # execute match matching.execute_match(cmd) # user interrupted program except KeyboardInterrupt: log.info("Detected keyboard interruption...") self.quit() break except: log.error("Unexpected error") traceback.print_exc() break
def execute_match(cmd): log.debug("Executing...") # try: # check if command exists as python script if commands.__contains__(cmd["name"]): log.debug("Replying...") commands.get(cmd["name"]).ex(cmd) elif cmd["name"] == "google": log.debug("Already executed...") else: log.error("Couldn't match command script") # type error: no command found except TypeError: tts.say( replying.get_reply(["matching", "fail"], system=True, module=True))
def recognize_for_keyword(): global keyword_detected global new_process audio = listen() # start new process new_process = True log.debug("Recognizing keyword...") try: # recognize input input = recognizer.recognize_google(audio, language=settings.LANGUAGE) # check if keyword in input if settings.KEYWORD in input.lower(): log.debug("Keyword detected") # to stop listening keyword_detected = True else: log.debug("Keyword not detected in '{}'".format(input)) except sr.UnknownValueError: log.debug("Speech engine couldn't resolve audio") except sr.RequestError: log.error("You need a WiFi connection for Google STT") except: log.error("Unkown exception") traceback.print_exc()
def listen_for_yn(): # get yes and no replies yes = replying.get_reply(["stt", "yn_y"], system=True, module=True) no = replying.get_reply(["stt", "yn_n"], system=True, module=True) log.info("Waiting for {0} or {1}".format(yes, no)) while True: audio = listen() input = recognize(audio) if input: if yes in input.lower(): log.debug("'{}' detected".format(yes)) return True elif no in input.lower(): log.debug("'{}' detected".format(no)) return False else: log.debug("Not detected in '{}'".format(input))
def get_match(input): log.debug("Matching...") cmd = {"name": None, "text": None, "input": input} # all jobs in correct order jobs = [check_match, test_match, google_match] # for every job for job in range(len(jobs)): cmd = jobs[job](input) # check if matched if cmd is not None: log.debug("Matched command '{}'".format(cmd["name"])) return cmd else: # next stage matching continue # unable to match log.debug("Couldn't match command from input...")
def clean(self): log.debug("Cleaning...")
def setup(self): log.debug("Setup...") # setup speech-to-text stt.setup() # setup text-to-speech tts.setup()