Exemplo n.º 1
0
def start_kalliope(settings, brain):
    """
    Start all signals declared in the brain
    """
    # start kalliope
    Utils.print_success("Starting Kalliope")
    Utils.print_info("Press Ctrl+C for stopping")
    # catch signal for killing on Ctrl+C pressed
    signal.signal(signal.SIGINT, signal_handler)

    # get a list of signal class to load from declared synapse in the brain
    # this list will contain string of signal class type.
    # For example, if the brain contains multiple time the signal type "order", the list will be ["order"]
    # If the brain contains some synapse with "order" and "event", the list will be ["order", "event"]
    list_signals_class_to_load = get_list_signal_class_to_load(brain)

    # start each class name

    for signal_class_name in list_signals_class_to_load:
        signal_instance = SignalLauncher.launch_signal_class_by_name(
            signal_name=signal_class_name, settings=settings)
        if signal_instance is not None:
            signal_instance.daemon = True
            signal_instance.start()

    while True:  # keep main thread alive
        time.sleep(0.1)
Exemplo n.º 2
0
    def houndify_callback(self, recognizer, audio):
        """
        called from the background thread
        """
        try:
            captured_audio = recognizer.recognize_houndify(
                audio,
                client_id=self.client_id,
                client_key=self.key,
                show_all=self.show_all)
            Utils.print_success(
                "Houndify Speech Recognition thinks you said %s" %
                captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Houndify Speech Recognition could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Could not request results from Houndify Speech Recognition service; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)

        # stop listening for an audio
        self.stop_listening()
Exemplo n.º 3
0
    def apiai_callback(self, recognizer, audio):
        """
        called from the background thread
        :param recognizer:
        :param audio:
        :return:
        """
        try:
            captured_audio = recognizer.recognize_api(
                audio,
                client_access_token=self.key,
                language=self.language,
                session_id=self.session_id,
                show_all=self.show_all)
            Utils.print_success("Apiai Speech Recognition thinks you said %s" %
                                captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError as e:
            Utils.print_warning(
                "Apiai Speech Recognition could not understand audio; {0}".
                format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Could not request results from Apiai Speech Recognition service; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning("No audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Exemplo n.º 4
0
    def sphinx_callback(self, recognizer, audio):
        """
        called from the background thread
        """
        try:
            captured_audio = recognizer.recognize_sphinx(
                audio,
                language=self.language,
                keyword_entries=self.keyword_entries,
                grammar=self.grammar_file)
            Utils.print_success(
                "Sphinx Speech Recognition thinks you said %s" %
                captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Sphinx Speech Recognition could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Could not request results from Sphinx Speech Recognition service; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning("No audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Exemplo n.º 5
0
def start_kalliope(settings, brain):
    """
    Start all signals declared in the brain
    """
    # start kalliope
    Utils.print_success("Starting Kalliope")
    Utils.print_info("Press Ctrl+C for stopping")
    # catch signal for killing on Ctrl+C pressed
    signal.signal(signal.SIGINT, signal_handler)

    # get a list of signal class to load from declared synapse in the brain
    # this list will contain string of signal class type.
    # For example, if the brain contains multiple time the signal type "order", the list will be ["order"]
    # If the brain contains some synapse with "order" and "event", the list will be ["order", "event"]
    list_signals_class_to_load = get_list_signal_class_to_load(brain)

    # start each class name
    try:
        for signal_class_name in list_signals_class_to_load:
            signal_instance = SignalLauncher.launch_signal_class_by_name(signal_name=signal_class_name,
                                                                         settings=settings)
            if signal_instance is not None:
                signal_instance.daemon = True
                signal_instance.start()

        while True:  # keep main thread alive
            time.sleep(0.1)

    except (KeyboardInterrupt, SystemExit):
        # we need to switch GPIO pin to default status if we are using a Rpi
        if settings.rpi_settings:
            Utils.print_info("GPIO cleaned")
            logger.debug("Clean GPIO")
            import RPi.GPIO as GPIO
            GPIO.cleanup()
Exemplo n.º 6
0
    def apiai_callback(self, recognizer, audio):
        """
        called from the background thread
        :param recognizer:
        :param audio:
        :return:
        """
        try:
            captured_audio = recognizer.recognize_api(audio,
                                                      client_access_token=self.key,
                                                      language=self.language,
                                                      session_id=self.session_id,
                                                      show_all=self.show_all)
            Utils.print_success("Apiai Speech Recognition thinks you said %s" % captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError as e:
            Utils.print_warning("Apiai Speech Recognition could not understand audio; {0}".format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger("Could not request results from Apiai Speech Recognition service; {0}".format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning("No audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Exemplo n.º 7
0
    def __init__(self, callback=None, **kwargs):
        """
        Start recording the microphone and analyse audio with google api
        :param callback: The callback function to call to send the text
        :param kwargs:
        """
        OrderListener.__init__(self)

        # callback function to call after the translation speech/tex
        self.callback = callback
        # obtain audio from the microphone
        r = sr.Recognizer()
        with sr.Microphone() as source:
            # listen for 1 second to calibrate the energy threshold for ambient noise levels
            r.adjust_for_ambient_noise(source)
            Utils.print_info("Say something!")
            audio = r.listen(source)

        # recognize speech using Google Speech Recognition
        try:
            # for testing purposes, we're just using the default API key
            # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
            # instead of `r.recognize_google(audio)`

            key = kwargs.get('key', None)
            language = kwargs.get('language', "en-US")
            show_all = kwargs.get('show_all', False)

            captured_audio = r.recognize_google(audio,
                                                key=key,
                                                language=language,
                                                show_all=show_all)
            Utils.print_success(
                "Google Speech Recognition thinks you said %s" %
                captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Google Speech Recognition could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Could not request results from Google Speech Recognition service; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio=None)
Exemplo n.º 8
0
    def vosk_callback(self, recognizer, audio_data):

        model = Model("model-fr")

        rec = KaldiRecognizer(model, 16000)

        upm = sr.Microphone()
        kl = sr.Recognizer()

        if not os.path.exists("model-fr"):
            print(
                "Please download the model from https://github.com/alphacep/kaldi-android-demo/releases and unpack as 'model-fr' in the current folder."
            )
            exit(1)

        sl_data = audio_data.get_raw_data(
            convert_rate=16000, convert_width=2
        )  # the included language models require audio to be 16-bit mono 16 kHz format

        try:

            if len(sl_data) == 0:
                print("len = 0")

            if rec.AcceptWaveform(sl_data):
                res = json.loads(rec.Result())

            res = json.loads(rec.FinalResult())
            captured_audio = res['text']
            Utils.print_success("Vosk thinks you said %s" % captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Vosk Speech Recognition could not understand audio")
            self._analyse_audio(audio_to_text=None)

        except sr.RequestError as e:
            Utils.print_danger(
                "Could not request results from Vosk Speech Recognition service; {0}"
                .format(e))
            self._analyse_audio(audio_to_text=None)

        except AssertionError:
            Utils.print_warning("No audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Exemplo n.º 9
0
    def __init__(self, callback=None, **kwargs):
        """
        Start recording the microphone and analyse audio with Houndify api
        :param callback: The callback function to call to send the text
        :param kwargs:
        """
        OrderListener.__init__(self)

        # callback function to call after the translation speech/tex
        self.callback = callback
        # obtain audio from the microphone
        r = sr.Recognizer()
        with sr.Microphone() as source:
            # listen for 1 second to calibrate the energy threshold for ambient noise levels
            r.adjust_for_ambient_noise(source)
            Utils.print_info("Say something!")
            audio = r.listen(source)

        # recognize speech using Houndify Speech Recognition
        try:
            client_id = kwargs.get('client_id', None)
            key = kwargs.get('key', None)
            language = kwargs.get('language', "en-US")
            show_all = kwargs.get('show_all', False)

            captured_audio = r.recognize_houndify(audio,
                                                  client_id=client_id,
                                                  client_key=key,
                                                  language=language,
                                                  show_all=show_all)
            Utils.print_success(
                "Houndify Speech Recognition thinks you said %s" %
                captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Houndify Speech Recognition could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Could not request results from Houndify Speech Recognition service; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio=None)
Exemplo n.º 10
0
    def wit_callback(self, recognizer, audio):
        try:
            captured_audio = recognizer.recognize_wit(audio,
                                                      key=self.key,
                                                      show_all=self.show_all)
            Utils.print_success("Wit.ai Speech Recognition thinks you said %s" % captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning("Wit.ai Speech Recognition could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger("Could not request results from Wit.ai Speech Recognition service; {0}".format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)

        # stop listening for an audio
        self.stop_listening()
Exemplo n.º 11
0
    def wit_callback(self, recognizer, audio):
        try:
            captured_audio = recognizer.recognize_wit(audio,
                                                      key=self.key,
                                                      show_all=self.show_all)
            Utils.print_success("Wit.ai Speech Recognition thinks you said %s" % captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning("Wit.ai Speech Recognition could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger("Could not request results from Wit.ai Speech Recognition service; {0}".format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning("No audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Exemplo n.º 12
0
 def google_callback(self, recognizer, audio):
     """
     called from the background thread
     """
     try:
         captured_audio = recognizer.recognize_google(audio,
                                                      key=self.key,
                                                      language=self.language,
                                                      show_all=self.show_all)
         Utils.print_success("Google Speech Recognition thinks you said %s" % captured_audio)
         self._analyse_audio(audio_to_text=captured_audio)
     except sr.UnknownValueError:
         Utils.print_warning("Google Speech Recognition could not understand audio")
         # callback anyway, we need to listen again for a new order
         self._analyse_audio(audio_to_text=None)
     except sr.RequestError as e:
         Utils.print_danger("Could not request results from Google Speech Recognition service; {0}".format(e))
         # callback anyway, we need to listen again for a new order
         self._analyse_audio(audio_to_text=None)
     except AssertionError:
         Utils.print_warning("No audio caught from microphone")
         self._analyse_audio(audio_to_text=None)
Exemplo n.º 13
0
def main():
    """
    Entry point of Kalliope program
    """
    # create arguments
    parser = argparse.ArgumentParser(description='Kalliope')
    parser.add_argument("action", help="[start|gui]")
    parser.add_argument("--run-synapse",
                        help="Name of a synapse to load surrounded by quote")
    parser.add_argument("--brain-file", help="Full path of a brain file")
    parser.add_argument("--debug",
                        action='store_true',
                        help="Show debug output")

    # parse arguments from script parameters
    args = parser.parse_args()

    # require at least one parameter, the action
    if len(sys.argv[1:]) == 0:
        parser.print_usage()
        sys.exit(1)

    # check if we want debug
    configure_logging(debug=args.debug)

    logger.debug("kalliope args: %s" % args)

    # by default, no brain file is set. Use the default one: brain.yml in the root path
    brain_file = None
    # check if user set a brain.yml file
    if args.brain_file:
        brain_file = args.brain_file
    # load the brain once
    brain_loader = BrainLoader.Instance(file_path=brain_file)
    brain = brain_loader.brain

    # check the user provide a valid action
    if args.action not in ACTION_LIST:
        Utils.print_warning("%s is not a recognised action\n" % args.action)
        parser.print_help()

    if args.action == "start":
        # user set a synapse to start
        if args.run_synapse is not None:
            SynapseLauncher.start_synapse(args.run_synapse, brain=brain)

        if args.run_synapse is None:
            # first, load events in crontab
            crontab_manager = CrontabManager(brain=brain)
            crontab_manager.load_events_in_crontab()
            Utils.print_success("Events loaded in crontab")
            # then start kalliope
            Utils.print_success("Starting Kalliope")
            Utils.print_info("Press Ctrl+C for stopping")
            # catch signal for killing on Ctrl+C pressed
            signal.signal(signal.SIGINT, signal_handler)
            # start the main controller
            MainController(brain=brain)

    if args.action == "gui":
        ShellGui(brain=brain)
Exemplo n.º 14
0
def main():
    """Entry point of Kalliope program."""

    # create arguments
    parser = argparse.ArgumentParser(description='Kalliope')
    parser.add_argument("action", help="[start|gui|install|uninstall]")
    parser.add_argument("--run-synapse",
                        help="Name of a synapse to load surrounded by quote")
    parser.add_argument("--run-order", help="order surrounded by a quote")
    parser.add_argument("--brain-file", help="Full path of a brain file")
    parser.add_argument("--debug",
                        action='store_true',
                        help="Show debug output")
    parser.add_argument("--git-url", help="Git URL of the neuron to install")
    parser.add_argument("--neuron-name", help="Neuron name to uninstall")
    parser.add_argument("--stt-name", help="STT name to uninstall")
    parser.add_argument("--tts-name", help="TTS name to uninstall")
    parser.add_argument("--trigger-name", help="Trigger name to uninstall")
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='Kalliope ' + version_str)

    # parse arguments from script parameters
    args = parser.parse_args()

    # require at least one parameter, the action
    if len(sys.argv[1:]) == 0:
        parser.print_usage()
        sys.exit(1)

    # check if we want debug
    configure_logging(debug=args.debug)

    logger.debug("kalliope args: %s" % args)

    # by default, no brain file is set.
    # Use the default one: brain.yml in the root path
    brain_file = None

    # check if user set a brain.yml file
    if args.brain_file:
        brain_file = args.brain_file

    # check the user provide a valid action
    if args.action not in ACTION_LIST:
        Utils.print_warning("%s is not a recognised action\n" % args.action)
        parser.print_help()

    # install modules
    if args.action == "install":
        if not args.git_url:
            Utils.print_danger("You must specify the git url")
        else:
            parameters = {"git_url": args.git_url}
            res_manager = ResourcesManager(**parameters)
            res_manager.install()
        return

    # uninstall modules
    if args.action == "uninstall":
        if not args.neuron_name and not args.stt_name and not args.tts_name and not args.trigger_name:
            Utils.print_danger(
                "You must specify a module name with --neuron-name or --stt-name or --tts-name "
                "or --trigger-name")
        else:
            res_manager = ResourcesManager()
            res_manager.uninstall(neuron_name=args.neuron_name,
                                  stt_name=args.stt_name,
                                  tts_name=args.tts_name,
                                  trigger_name=args.trigger_name)
        return

    # load the brain once
    brain_loader = BrainLoader(file_path=brain_file)
    brain = brain_loader.brain

    if args.action == "start":

        # user set a synapse to start
        if args.run_synapse is not None:
            SynapseLauncher.start_synapse(args.run_synapse, brain=brain)

        if args.run_order is not None:
            order_analyser = OrderAnalyser(args.run_order, brain=brain)
            order_analyser.start()

        if (args.run_synapse is None) and (args.run_order is None):
            # first, load events in event manager
            EventManager(brain.synapses)
            Utils.print_success("Events loaded")
            # then start kalliope
            Utils.print_success("Starting Kalliope")
            Utils.print_info("Press Ctrl+C for stopping")
            # catch signal for killing on Ctrl+C pressed
            signal.signal(signal.SIGINT, signal_handler)
            # start the state machine
            MainController(brain=brain)

    if args.action == "gui":
        ShellGui(brain=brain)
Exemplo n.º 15
0
def main():
    """Entry point of Kalliope program."""
    # parse argument. the script name is removed
    try:
        parser = parse_args(sys.argv[1:])
    except SystemExit:
        sys.exit(1)

    # check if we want debug
    configure_logging(debug=parser.debug)

    logger.debug("kalliope args: %s" % parser)

    # by default, no brain file is set.
    # Use the default one: brain.yml in the root path
    brain_file = None

    # check if user set a brain.yml file
    if parser.brain_file:
        brain_file = parser.brain_file

    # check the user provide a valid action
    if parser.action not in ACTION_LIST:
        Utils.print_warning("%s is not a recognised action\n" % parser.action)
        sys.exit(1)

    # install modules
    if parser.action == "install":
        if not parser.git_url:
            Utils.print_danger("You must specify the git url")
            sys.exit(1)
        else:
            parameters = {"git_url": parser.git_url}
            res_manager = ResourcesManager(**parameters)
            res_manager.install()
        return

    # uninstall modules
    if parser.action == "uninstall":
        if not parser.neuron_name and not parser.stt_name and not parser.tts_name and not parser.trigger_name:
            Utils.print_danger(
                "You must specify a module name with --neuron-name or --stt-name or --tts-name "
                "or --trigger-name")
            sys.exit(1)
        else:
            res_manager = ResourcesManager()
            res_manager.uninstall(neuron_name=parser.neuron_name,
                                  stt_name=parser.stt_name,
                                  tts_name=parser.tts_name,
                                  trigger_name=parser.trigger_name)
        return

    # load the brain once
    brain_loader = BrainLoader(file_path=brain_file)
    brain = brain_loader.brain

    # load settings
    # get global configuration once
    settings_loader = SettingLoader()
    settings = settings_loader.settings

    if parser.action == "start":

        if settings.rpi_settings:
            # init GPIO once
            RpiUtils(settings.rpi_settings)

        # user set a synapse to start
        if parser.run_synapse is not None:
            SynapseLauncher.start_synapse_by_name(parser.run_synapse,
                                                  brain=brain)

        if parser.run_order is not None:
            SynapseLauncher.run_matching_synapse_from_order(parser.run_order,
                                                            brain=brain,
                                                            settings=settings,
                                                            is_api_call=False)

        if (parser.run_synapse is None) and (parser.run_order is None):
            # first, load events in event manager
            EventManager(brain.synapses)
            Utils.print_success("Events loaded")
            # then start kalliope
            Utils.print_success("Starting Kalliope")
            Utils.print_info("Press Ctrl+C for stopping")
            # catch signal for killing on Ctrl+C pressed
            signal.signal(signal.SIGINT, signal_handler)
            # start the state machine
            try:
                MainController(brain=brain)
            except (KeyboardInterrupt, SystemExit):
                Utils.print_info("Ctrl+C pressed. Killing Kalliope")
            finally:
                # we need to switch GPIO pin to default status if we are using a Rpi
                if settings.rpi_settings:
                    logger.debug("Clean GPIO")
                    import RPi.GPIO as GPIO
                    GPIO.cleanup()

    if parser.action == "gui":
        try:
            ShellGui(brain=brain)
        except (KeyboardInterrupt, SystemExit):
            Utils.print_info("Ctrl+C pressed. Killing Kalliope")
            sys.exit(0)