예제 #1
0
def do_recognition(args, recorder, recognizer, player, status_ui):
    """Configure and run the recognizer."""
    say = aiy.audio.say
    actor = action.make_actor(say)

    if args.cloud_speech:
        action.add_commands_just_for_cloud_speech_api(actor, say)

    recognizer.add_phrases(actor)
    recognizer.set_audio_logging_enabled(args.audio_logging)

    if args.trigger == 'gpio':
        import triggers.gpio
        triggerer = triggers.gpio.GpioTrigger(channel=23)
        msg = 'Press the button on GPIO 23'
    elif args.trigger == 'clap':
        import triggers.clap
        triggerer = triggers.clap.ClapTrigger(recorder)
        msg = 'Clap your hands'
    else:
        logger.error("Unknown trigger '%s'", args.trigger)
        return

    mic_recognizer = SyncMicRecognizer(actor, recognizer, recorder, player,
                                       say, triggerer, status_ui,
                                       args.assistant_always_responds)

    with mic_recognizer:
        if sys.stdout.isatty():
            print(msg + ' then speak, or press Ctrl+C to quit...')

        # wait for KeyboardInterrupt
        while True:
            time.sleep(1)
예제 #2
0
def do_recognition(args, recorder, recognizer, player, status_ui):
    """Configure and run the recognizer."""
    say = aiy.audio.say
    actor = action.make_actor(say)

    if args.cloud_speech:
        action.add_commands_just_for_cloud_speech_api(actor, say)

    recognizer.add_phrases(actor)
    recognizer.set_audio_logging_enabled(args.audio_logging)

    if args.trigger == 'gpio':
        import triggers.gpio
        triggerer = triggers.gpio.GpioTrigger(channel=23)
        msg = 'Press the button on GPIO 23'
    elif args.trigger == 'clap':
        import triggers.clap
        triggerer = triggers.clap.ClapTrigger(recorder)
        msg = 'Clap your hands'
    else:
        logger.error("Unknown trigger '%s'", args.trigger)
        return

    mic_recognizer = SyncMicRecognizer(
        actor, recognizer, recorder, player, say, triggerer, status_ui,
        args.assistant_always_responds)

    with mic_recognizer:
        if sys.stdout.isatty():
            print(msg + ' then speak, or press Ctrl+C to quit...')

        # wait for KeyboardInterrupt
        while True:
            time.sleep(1)
예제 #3
0
def do_recognition(args, recorder, recognizer, player):
    """Configure and run the recognizer."""

    global Dplayer, Dsay, Drecorder, Drecognizer, Dtriggerer

    say = tts.create_say(player)
    actor = action.make_actor(say)

    Dplayer = player
    Dsay = say
    Drecorder = recorder
    Drecognizer = recognizer

    if args.cloud_speech:
        action.add_commands_just_for_cloud_speech_api(actor, say)

    recognizer.add_phrases(actor)
    recognizer.set_audio_logging_enabled(args.audio_logging)

    if args.trigger == 'gpio':
        import triggers.gpio
        triggerer = triggers.gpio.GpioTrigger(channel=23)
        msg = 'Press the button on GPIO 23'
    elif args.trigger == 'clap':
        import triggers.clap
        triggerer = triggers.clap.ClapTrigger(recorder)
        msg = 'Clap your hands'
    else:
        logger.error("Unknown trigger '%s'", args.trigger)
        return
    Dtriggerer = triggerer

    mic_recognizer = SyncMicRecognizer(actor,
                                       recognizer,
                                       recorder,
                                       player,
                                       say,
                                       triggerer,
                                       led_fifo=args.led_fifo)

    with mic_recognizer:
        if sys.stdout.isatty():
            print(msg + ' then speak, or press Ctrl+C to quit...')

        # wait for KeyboardInterrupt
        i = 0
        while True:
            i += 1
            if mqttclient:
                mqttclient.loop(
                    timeout=0.2
                )  # needs to be called regularly (otherwise would need its own thread)
            else:
                time.sleep(0.2)
            if (i % 100) == 0:
                logger.info("tick")
예제 #4
0
def do_assistant_library(args, recognizer, credentials, player, status_ui):
    """Run a recognizer using the Google Assistant Library.

    The Google Assistant Library has direct access to the audio API, so this
    Python code doesn't need to record audio.
    """

    try:
        from google.assistant.library import Assistant
        from google.assistant.library.event import EventType
    except ImportError:
        print('''
ERROR: failed to import the Google Assistant Library. This is required for
"OK Google" hotwording, but is only available for Raspberry Pi 2/3. It can be
installed with:
    env/bin/pip install google-assistant-library==0.0.2''')
        sys.exit(1)

    say = tts.create_say(player)
    actor = action.make_actor(say)
    action.add_commands_just_for_cloud_speech_api(actor, say)

    recognizer.add_phrases(actor)

    def process_event(event):
        logging.info(event)

        if event.type == EventType.ON_START_FINISHED:
            status_ui.status('ready')
            if sys.stdout.isatty():
                print(
                    'Say "OK, Google" then speak, or press Ctrl+C to quit...')

        elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
            status_ui.status('listening')

        elif event.type == EventType.ON_END_OF_UTTERANCE:
            status_ui.status('thinking')

        elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and \
                event.args and actor.can_handle(event.args['text']):
            if not args.assistant_always_responds:
                assistant.stop_conversation()
            actor.handle(event.args['text'])

        elif event.type == EventType.ON_CONVERSATION_TURN_FINISHED:
            status_ui.status('ready')

        elif event.type == EventType.ON_ASSISTANT_ERROR and \
                event.args and event.args['is_fatal']:
            sys.exit(1)

    with Assistant(credentials) as assistant:
        for event in assistant.start():
            process_event(event)
예제 #5
0
def do_recognition(args, recorder, recognizer, player, credentials):
    """Configure and run the recognizer."""
    say = tts.create_say(player)

    actor = action.make_actor(say)

    if args.cloud_speech:
        action.add_commands_just_for_cloud_speech_api(actor, say)

    recognizer.add_phrases(actor)
    recognizer.set_audio_logging_enabled(args.audio_logging)

    if args.trigger == 'gpio':
        import triggers.gpio
        triggerer = triggers.gpio.GpioTrigger(channel=23)
        msg = 'Press the button on GPIO 23'
    elif args.trigger == 'clap':
        import triggers.clap
        triggerer = triggers.clap.ClapTrigger(recorder)
        msg = 'Clap your hands'
    elif args.trigger == 'hotword':
        import triggers.hotword
        triggerer = triggers.hotword.HotwordTrigger(credentials)
        msg = 'Say "Ok Google"'
    else:
        logger.error("Unknown trigger '%s'", args.trigger)
        return

    mic_recognizer = SyncMicRecognizer(actor,
                                       recognizer,
                                       recorder,
                                       player,
                                       say,
                                       triggerer,
                                       led_fifo=args.led_fifo)

    with mic_recognizer:
        if sys.stdout.isatty():
            print(msg + ' then speak, or press Ctrl+C to quit...')

        # wait for KeyboardInterrupt
        while True:
            time.sleep(1)