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 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)) 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 t3_bot(): src = Source(rate=16000, channels=4, frames_size=320) ch1 = ChannelPicker(channels=4, pick=1) kws = KWS() doa = DOA(rate=16000) src.link(ch1) ch1.link(kws) src.link(doa) pixels.listen() pwm.setPWM(0, 0, 370) pwm.setPWM(1, 0, 640) counter = 0 def on_detected(keyword): position = doa.get_direction() pixels.wakeup(position) print('detected {} at direction {}'.format(keyword, position)) if position >= 30 and position <= 180: pwm.setPWM(0, 0, 175) pwm.setPWM(1, 0, 500) elif position > 180 and position <= 330: pwm.setPWM(0, 0, 560) pwm.setPWM(1, 0, 500) elif position > 330 or position < 30: pwm.setPWM(0, 0, 370) pwm.setPWM(1, 0, 6200) else: pwm.setPWM(0, 0, 370) pwm.setPWM(1, 0, 640) #talkassist.os.system("espeak 'may i help you'") print("How may I help you?") print("call google assistant here, delete this line.") kws.set_callback(on_detected) src.recursive_start() while True: try: time.sleep(1) counter += 1 print("counter is at " + str(counter)) except KeyboardInterrupt: break src.recursive_stop()
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()
from pixels import pixels BUTTON = 17 #LED = 16 hold_time = 1.2 GPIO.setmode(GPIO.BCM) GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) #GPIO.setup(LED, GPIO.OUT) actions = [ 'python3 visiontrans.py --trans ja-JP', #ボタン長押しで顔、ラベル、ロゴ全部読み取り、日本語発話 'python3 visiontrans.py --detect text --trans ja-JP', #ワンプッシュで文字読み取り、日本語翻訳発話 'python3 visiontrans.py --detect face' ] #ダブルプッシュで顔読み取り pixels.listen() time.sleep(hold_time) pixels.off() GPIO.add_event_detect(BUTTON, GPIO.FALLING) while True: if GPIO.event_detected(BUTTON): GPIO.remove_event_detect(BUTTON) now = time.time() count = 0 GPIO.add_event_detect(BUTTON, GPIO.RISING) while time.time() < now + hold_time: if GPIO.event_detected(BUTTON): count += 1 time.sleep(.3) # debounce time
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. ")
def main(detect="", photo_file="", trans_lang=""): pixels.wakeup() if photo_file == "": photo_file = camera() pixels.off() credentials = GoogleCredentials.get_application_default() service = discovery.build('vision', 'v1', credentials=credentials, discoveryServiceUrl=DISCOVERY_URL) with open(photo_file, 'rb') as image: image_content = base64.b64encode(image.read()) if detect == "": #No parameter DETECT = default_detect else: #Paremater specified DETECT = [detect.upper()] result = "" bounds = [] tlocale = "" for DET in DETECT: pixels.listen() service_request = service.images().annotate( body={ 'requests': [{ 'image': { 'content': image_content.decode('UTF-8') }, 'features': [{ 'type': DET + '_DETECTION', 'maxResults': default_max }] }] }) response = service_request.execute() annotation = DET.lower() + 'Annotations' try: results = response['responses'][0][annotation] for res in results: if DET in ["LABEL", "LOGO"]: if res["score"] > 0.7: result += res["description"] + ", " elif DET in ["TEXT"]: tlocale = res["locale"] result += res["description"] + ", " bounds += res["boundingPoly"]["vertices"] elif DET in ["FACE"]: if res["joyLikelihood"] == "VERY_LIKELY" or res[ "joyLikelihood"] == "LIKELY": result += "Smile " if res["angerLikelihood"] == "VERY_LIKELY" or res[ "angerLikelihood"] == "LIKELY": result += "Angry " if res["headwearLikelihood"] == "VERY_LIKELY" or res[ "headwearLikelihood"] == "LIKELY": rsult += "Capped " result += DET + ", " except: result += "No " + DET + ", " pixels.off() print('Result: ' + result) pixels.listen() if trans_lang: trans_text = translate_text(result, trans_lang) trans_text = trans_text.replace("'", "") print('Trans: ' + trans_text) if trans_lang in aiy_lang: aiy.audio.say(trans_text, trans_lang) elif trans_lang == "ja-JP": os.system(aquest_dir + ' -g {} {} | aplay -D plughw:{},{}'.format( VOLUME, trans_text, CARD, DEVICE)) else: aiy.audio.say('Nothing to trans!', 'en-US') else: #trans_lang = null then default en-US aiy.audio.say(result, 'en-US') pixels.off()