def process_event(event): """Pretty prints events. Prints all events that occur with two spaces between each new conversation and a single space between turns of a conversation. Args: event(event.Event): The current event to process. """ if event.type == EventType.ON_CONVERSATION_TURN_STARTED: print() pixels.wakeup() print(event) if event.type == EventType.ON_END_OF_UTTERANCE: pixels.think() if event.type == EventType.ON_RESPONDING_STARTED: pixels.speak() if event.type == EventType.ON_CONVERSATION_TURN_FINISHED: pixels.off() if event.args and event.args['with_follow_on_turn']: pixels.listen()
def process_event(event, device_id): """Pretty prints events. Prints all events that occur with two spaces between each new conversation and a single space between turns of a conversation. Args: event(event.Event): The current event to process. device_id(str): The device ID of the new instance. """ if event.type == EventType.ON_CONVERSATION_TURN_STARTED: print() pixels.wakeup() print(event) if event.type == EventType.ON_END_OF_UTTERANCE: pixels.think() if event.type == EventType.ON_RESPONDING_STARTED: pixels.speak() if event.type == EventType.ON_CONVERSATION_TURN_FINISHED: if event.args and event.args['with_follow_on_turn']: pixels.listen() else: pixels.off() print() if event.type == EventType.ON_DEVICE_ACTION: for command, params in process_device_actions(event, device_id): print('Do command', command, 'with params', str(params))
def on_message(client, userdata, msg): #print("Message received on topic {0}: {1}".format(msg.topic, msg.payload)) if "hotword" in msg.topic: print("==> hotword message {0}: {1}".format(msg.topic, msg.payload)) if "detected" in msg.topic: pixels.wakeup() if msg.topic == "hermes/hotword/toggleOn": pixels.off() if "asr" in msg.topic: print("==> asr message {0}: {1}".format(msg.topic, msg.payload)) if "textCaptured" in msg.topic: pixels.think() if "nlu" in msg.topic: print("==> nlu message {0}: {1}".format(msg.topic, msg.payload)) if "tts" in msg.topic: print("==> tts message {0}: {1}".format(msg.topic, msg.payload)) if msg.topic == "hermes/tts/say": pixels.speak() elif "intent" in msg.topic: print("==> intent message {0}: {1}".format(msg.topic, msg.payload)) #intent_topic = msg.topic.split("/") payload = json.loads(msg.payload) if "intent" in payload: name = payload["intent"]["intentName"] slots = payload["slots"] print("====> intent {0} detected with slots {1}".format(name, slots))
def playtts(msg): pixels.think() print('Sending to server ....') r = requests.post(url2, json=msg, headers=headers2) pixels.off() links = json.loads(r.text)['message'] if links: for link in links: if len(link)>0: link = host + link os.system('mplayer -volume 100 ' + link)
def on_message(client, userdata, msg): if msg.topic == "hermes/asr/startListening": pixels.think() if msg.topic == "hermes/audioServer/default/playFinished": pixels.off() if msg.topic == "hermes/asr/stopListening": pixels.off() if msg.topic == "hermes/tts/say": pixels.speak()
def process_event(event, device_id): """Pretty prints events. Prints all events that occur with two spaces between each new conversation and a single space between turns of a conversation. Args: event(event.Event): The current event to process. device_id(str): The device ID of the new instance. """ if event.type == EventType.ON_CONVERSATION_TURN_STARTED: print() pixels.wakeup() print(event) if event.type == EventType.ON_END_OF_UTTERANCE: pixels.think() if event.type == EventType.ON_RESPONDING_STARTED: pixels.speak() if event.type == EventType.ON_CONVERSATION_TURN_FINISHED: if event.args and event.args['with_follow_on_turn']: pixels.listen() else: pixels.off() print() if event.type == EventType.ON_DEVICE_ACTION: for command, params in process_device_actions(event, device_id): print('Do command', command, 'with params', str(params)) if command == "com.example.commands.Shutters": action = '' shutters_url = 'http://10.0.0.31:8181/json.htm' shutters_header = {'Authorization': 'Basic *******************'} if params['status'] == "CLOSE": print('Closing shutters') action = 'On' shutters_params = {'type': 'command', 'param': 'switchlight', 'idx': '13', 'switchcmd': action } r = requests.get(shutters_url, params=shutters_params, headers=shutters_header) print(r.url) print(r.status_code) if params['status'] == "OPEN": print('Opening shutters') action = 'Off' shutters_params = {'type': 'command', 'param': 'switchlight', 'idx': '13', 'switchcmd': action } r = requests.get(shutters_url, params=shutters_params, headers=shutters_header) print(r.url) print(r.status_code) print(r.headers)
def play_ack(num): global proc if proc != None: proc.terminate() cmd = ['cvlc', '-q', '--play-and-exit', SOUND_ACK[num]] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if num == 0: pixels.off() if num == 1: pixels.think()
def ActivateLeds(LedOn): if LedOn == True: pixels.wakeup() time.sleep(3) pixels.think() time.sleep(3) pixels.speak() time.sleep(6) pixels.off() time.sleep(3) else: LedOn = False pixels.off() time.sleep(1)
def on_message(client, userdata, msg): ''' Process a message as it arrives ''' # Read Snips Payload #print(msg.payload) json_data = msg.payload.decode('utf-8') slots = parse_slots(json.loads(json_data)) session_id = parse_session_id(json_data) if 'Action' not in slots: say("I don't know where I should go.", session_id) else: action = slots['Action'] if with_respeaker: pixels.speak() say('OK {}'.format(action), session_id) make_move(action, session_id=session_id) if with_respeaker: pixels.think() say("Done")
def run(self): global moving, last_movement_timestamp, doa_valid src = Source(rate=16000, channels=4, frames_size=320) #ch1 = ChannelPicker(channels=4, pick=1) doa = DOA(rate=16000) #src.link(ch1) src.link(doa) src.recursive_start() self.running = True while self.running: try: time.sleep(1) current_timestamp = datetime.datetime.now() if doa_valid == True and ( (current_timestamp - last_movement_timestamp).seconds > 2): position, amplitute = doa.get_direction() if amplitute > 2000: pixels.wakeup(position) print amplitute, position if position > 0 and position < 180: pivot_right() time.sleep(position / 200) stop() elif position >= 180 and position < 360: pivot_left() position = 360 - position time.sleep(position / 200) stop() time.sleep(2) else: pixels.speak() else: pixels.think() except: print sys.exc_info() src.recursive_stop()
def pixels_think(client, userdata, msg): # print("pixels_think") pixels.think() time.sleep(3)
import time from pixels import Pixels, pixels from alexa_led_pattern import AlexaLedPattern from google_home_led_pattern import GoogleHomeLedPattern if __name__ == '__main__': pixels.pattern = GoogleHomeLedPattern(show=pixels.show) while True: try: pixels.wakeup() time.sleep(3) pixels.think() time.sleep(3) pixels.speak() time.sleep(6) pixels.off() time.sleep(3) except KeyboardInterrupt: break pixels.off() time.sleep(1)
import time from pixels import Pixels, pixels from alexa_led_pattern import AlexaLedPattern from google_home_led_pattern import GoogleHomeLedPattern if __name__ == '__main__': pixels.pattern = GoogleHomeLedPattern(show=pixels.show) while True: try: print('Wakekup') pixels.wakeup() time.sleep(3) print('think') pixels.think() time.sleep(3) print('speak') pixels.speak() time.sleep(6) print('off') pixels.off() time.sleep(3) except KeyboardInterrupt: break pixels.off() time.sleep(1)
def main(): recognizer = aiy.cloudspeech.get_recognizer() recognizer.expect_phrase('turn off the light') recognizer.expect_phrase('turn on the light') recognizer.expect_phrase('blink') recognizer.expect_phrase('repeat after me') button = aiy.voicehat.get_button() led = aiy.voicehat.get_led() aiy.audio.get_recorder().start() aiy.i18n.set_language_code(speech_lang) for i in range(3): pixels.wakeup() time.sleep(1) pixels.off() while True: print('Press the button and speak') pixels.wakeup() button.wait_for_press() while True: print('Listening...') bye_words = ['goodbye', 'good bye', 'see you', 'bye bye'] pixels.think() text = recognizer.recognize() if not text: print('Sorry but please say again in ' + speech_lang) else: pixels.listen() print('Speech: ' + text) trans_text = translate_text(text, trans_lang) trans_text = trans_text.replace("'", "") print('Trans: ' + trans_text) pixels.off() pixels.listen() if trans_lang in aiy_lang: aiy.audio.say(trans_text, trans_lang) elif trans_lang == "ja-JP": os.system( '~/AIY-projects-python/src/aquestalkpi/AquesTalkPi -g {} {} | aplay -D plughw:{},{}' .format(VOLUME, trans_text, CARD, DEVICE)) else: print("No lang to say") if 'turn on the light' in text: led.set_state(aiy.voicehat.LED.ON) elif 'turn off the light' in text: led.set_state(aiy.voicehat.LED.OFF) elif 'blink' in text: led.set_state(aiy.voicehat.LED.BLINK) elif 'repeat after me' in text: to_repeat = text.replace('repeat after me', '', 1) aiy.audio.say(to_repeat) for b in bye_words: if text.find(b) > -1: keyw = "bye" break if text in bye_words: pixels.off() break time.sleep(0.2) pixels.off()
def pixels_think(client, userdata, msg): pixels.think() time.sleep(3)
def onMessage(client, userData, message): global lang intent = message.topic payload = json.loads(message.payload) if intent == HERMES_ON_HOTWORD: last_hotword = utils.read_file("hotword.txt") current_hotword = payload['modelId'].encode('utf-8') if last_hotword != current_hotword: utils.write_to_file("hotword.txt", current_hotword) if settings.USE_LEDS: pixels.wakeup() return elif intent == HERMES_SAY: if settings.USE_LEDS: pixels.speak() return elif intent == HERMES_CAPTURED: if settings.USE_LEDS: pixels.think() return elif intent == HERMES_START_LISTENING: if settings.USE_LEDS: pixels.listen() return elif intent == HERMES_HOTWORD_TOGGLE_ON: if settings.USE_LEDS: pixels.off() return global recipe, currentStep, timers, confirm, sessionId, product, tipIndex, fromIntent sessionId = payload['sessionId'] ##### TODO stabiliser avant réactivation if intent == OPEN_RECIPE: print("INTENT : OPEN_RECIPE") if 'slots' not in payload: error(sessionId) return slotRecipeName = payload['slots'][0]['value']['value'].encode('utf-8') if recipe is not None and currentStep > 0: if confirm <= 0: confirm = 1 endTalk(sessionId, text=lang['warningRecipeAlreadyOpen']) return else: for timer in timers: timer.cancel() timers = {} confirm = 0 currentStep = 0 if any(product.lower() in ingredients for ingredients in tips_list_from_paprika): recipe_nb = len(tips_list_from_paprika[product.lower()]) if recipe_nb == 1: for recipe in tips_list_from_paprika[product.lower()]: continueSession(sessionId, "j'ai trouvé une astuce: " + recipe + ". Tu veux faire ça ?", intents=['Pierrot-app:validateQuestion']) elif recipe_nb == 2: askForTwoTips(getTipList) else: endTalk(sessionId, text=lang['noTipsForProduct']) fromIntent = "OPEN_RECIPE" elif intent == NEXT_STEP: print("INTENT : NEXT_STEP") if recipe is None: endTalk(sessionId, text=lang['sorryNoRecipeOpen']) else: if str(currentStep + 1) not in recipe['steps']: endTalk(sessionId, text=lang['recipeEnd']) else: currentStep += 1 step = recipe['steps'][str(currentStep)] ask = False if type(step) is dict and currentStep not in timers: ask = True step = step['text'] endTalk(sessionId, text=lang['nextStep'].format(step)) if ask: say(text=lang['timerAsk']) fromIntent = "NEXT_STEP" elif intent == INGREDIENTS: print("INTENT : INGREDIENTS") if recipe is None: endTalk(sessionId, text=lang['sorryNoRecipeOpen']) else: ingredients = '' for ingredient in recipe['ingredients']: ingredients += u"{}. ".format(ingredient) endTalk(sessionId, text=lang['neededIngredients'].format(ingredients)) fromIntent = "INGREDIENTS" elif intent == PREVIOUS_STEP: print("INTENT : PREVIOUS_STEP") if recipe is None: endTalk(sessionId, text=lang['sorryNoRecipeOpen']) else: if currentStep <= 1: endTalk(sessionId, text=lang['noPreviousStep']) else: currentStep -= 1 step = recipe['steps'][str(currentStep)] ask = False timer = 0 if type(step) is dict and currentStep not in timers: ask = True timer = step['timer'] step = step['text'] endTalk(sessionId, text=lang['previousStepWas'].format(step)) if ask: say(text=lang['hadTimerAsk'].format(timer)) fromIntent = "PREVIOUS_STEP" elif intent == REPEAT_STEP: print("INTENT : REPEAT_STEP") if recipe is None: endTalk(sessionId, text=lang['sorryNoRecipeOpen']) else: if currentStep < 1: ingredients = '' for ingredient in recipe['ingredients']: ingredients += u"{}. ".format(ingredient) endTalk(sessionId, text=lang['neededIngredients'].format(ingredients)) else: step = recipe['steps'][str(currentStep)] endTalk(sessionId, text=lang['repeatStep'].format(step)) fromIntent = "REPEAT_STEP" elif intent == ACTIVATE_TIMER: print("INTENT : ACTIVATE_TIMER") if recipe is None: endTalk(sessionId, text=lang['noTimerNotStarted']) else: step = recipe['steps'][str(currentStep)] if type(step) is not dict: endTalk(sessionId, text=lang['notTimerForThisStep']) elif currentStep in timers: endTalk(sessionId, text=lang['timerAlreadyRunning']) else: timer = Timer(int(step['timer']), onTimeUp, args=[currentStep, step]) timer.start() timers[currentStep] = timer endTalk(sessionId, text=lang['timerConfirm']) fromIntent = "ACTIVATE_TIMER" elif intent == GET_FOOD: print("INTENT : GET_FOOD") sayNoSession(lang['searching']) asTalk = False tipIndex = 1 product = payload["slots"][0]["rawValue"] if lastIntent == "ASK_FOR_TIP" or getAssistant() == "marin": currentStep = 0 readTipsProposition() else: continueSession(sessionId=sessionId, text=lang['cookNowOrKeep'].format(product), intents=['Pierrot-app:nowOrLater']) fromIntent = "GET_FOOD" elif intent == ASK_FOR_TIP: print("INTENT : ASK_FOR_TIP") if product in getTipList(): currentStep = 0 tipIndex = 1 continueSession(sessionId=sessionId, text=lang['tipFor'].format(product), intents=[ 'Pierrot-app:validateQuestion', 'Pierrot-app:invalidateQuestion' ]) else: continueSession(sessionId=sessionId, text=lang['tipForWhat'], intents=['Pierrot-app:getFoodRequest']) fromIntent = "ASK_FOR_TIP" # elif intent == GET_FOOD_COOK_NOW: # product = payload['slots'][0]['value']['value'].encode('utf-8') # if any(product.lower() in ingredients for ingredients in recipe_ingredients): # # endTalk(sessionId=sessionId, text=lang['startRecipe'].format(food), intents=['openRecipe']) # readRecipe(sessionId, product, payload) # else: # endTalk(sessionId, text=lang['recipeNotFound']) elif intent == COOK_NOW_OR_KEEP: print("INTENT : COOK_NOW_OR_KEEP") # if recipe is None: # endTalk(sessionId, text=lang['sorryNoRecipeOpen']) # else: readTipsProposition() fromIntent = "COOK_NOW_OR_KEEP" elif intent == VALIDATE_QUESTION: print("INTENT : VALIDATE_QUESTION") if recipe is None: endTalk(sessionId, text=lang['sorryNoRecipeOpen']) elif fromIntent == "ASK_FOR_TIP": readTipsProposition() else: if currentStep != 0: currentStep += 1 step = recipe['steps'][str(currentStep)] ask = False if type(step) is dict and currentStep not in timers: ask = True step = step['text'] endTalk(sessionId, text=lang['nextStep'].format(step)) else: ingredients = '' for ingredient in recipe['ingredients']: ingredients += u"{}, ".format(ingredient) endTalk(sessionId, text=lang['neededIngredients'].format(ingredients)) fromIntent = "VALIDATE_QUESTION" elif intent == INVALIDATE_QUESTION: print("INTENT : INVALIDATE_QUESTION") if recipe is None: endTalk(sessionId, text=lang['sorryNoRecipeOpen']) elif fromIntent == "GET_FOOD" or fromIntent == "INVALIDATE_QUESTION" or fromIntent == "COOK_NOW_OR_KEEP" or fromIntent == "VALIDATE_QUESTION": readTipsProposition() elif lastIntent == "ASK_FOR_TIP": continueSession(sessionId=sessionId, text=lang['tipForWhat'], intents=['Pierrot-app:getFoodRequest']) fromIntent = "INVALIDATE_QUESTION" elif intent == START_RECIPE: print("INTENT : START_RECIPE") if recipe is None: endTalk(sessionId, text=lang['sorryNoRecipeOpen']) else: currentStep += 1 step = recipe['steps'][str(currentStep)] ask = False if type(step) is dict and currentStep not in timers: ask = True step = step['text'] endTalk(sessionId, text=lang['firstStep'].format(step)) if ask: say(text=lang['timerAsk']) fromIntent = "START_RECIPE" elif intent == CANCEL: if settings.USE_LEDS: pixels.off() error(sessionId) mqttClient.loop_stop() mqttClient.disconnect() running = False elif intent == JOKE: sayNoSession( "Je ne crois pas qu'il y ai de bons ou de mauvais assistant. ")