Exemplo n.º 1
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.º 2
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.º 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 __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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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.º 11
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\
                and not parser.signal_name:
            Utils.print_danger("You must specify a module name with "
                               "--neuron-name "
                               "or --stt-name "
                               "or --tts-name "
                               "or --trigger-name "
                               "or --signal-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,
                                  signal_name=parser.signal_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":

        # 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):
            # if --muted
            if parser.muted:
                settings.start_options['muted'] = True

            # start rest api
            start_rest_api(settings, brain)
            start_kalliope(settings, brain)

    if parser.action == "gui":
        try:
            ShellGui(brain=brain)
        except (KeyboardInterrupt, SystemExit):
            Utils.print_info("Ctrl+C pressed. Killing Kalliope")
            sys.exit(0)
Exemplo n.º 12
0
def main():
    """
    Entry point of Kalliope program
    """
    # create arguments
    parser = argparse.ArgumentParser(description='Kalliope')
    parser.add_argument("action", help="[start|gui|install]")
    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")
    parser.add_argument("--git-url", help="Git URL of the neuron to install")
    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
    # load the brain once
    brain_loader = BrainLoader(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 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 main controller
            MainController(brain=brain)

    if args.action == "gui":
        ShellGui(brain=brain)

    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()
Exemplo n.º 13
0
    def _check_dna_file(dna_file):
        """
        Check the content of a DNA file
        :param dna_file: the dna to check
        :return: True if ok, False otherwise
        """
        success_loading = True
        if "name" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"name\" tag")
            success_loading = False

        if "type" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"type\" tag")
            success_loading = False

        else:
            # we have a type, check that is a valid one
            if dna_file["type"] not in VALID_DNA_MODULE_TYPE:
                Utils.print_danger("The DNA type %s is not valid" %
                                   dna_file["type"])
                Utils.print_danger(
                    "The DNA type must be one of the following: %s" %
                    VALID_DNA_MODULE_TYPE)
                success_loading = False

        if "kalliope_supported_version" not in dna_file:
            Utils.print_danger(
                "The DNA of does not contains a \"kalliope_supported_version\" tag"
            )
            success_loading = False
        else:
            # kalliope_supported_version must be a non empty list
            if not isinstance(dna_file["kalliope_supported_version"], list):
                Utils.print_danger("kalliope_supported_version is not a list")
                success_loading = False
            else:
                if not dna_file["kalliope_supported_version"]:
                    Utils.print_danger(
                        "kalliope_supported_version cannot be empty")
                    success_loading = False
                else:
                    for supported_version in dna_file[
                            "kalliope_supported_version"]:
                        # check if major version is provided
                        if not re.search('^[\d]*[.][\d]*$',
                                         str(supported_version)):
                            Utils.print_danger(
                                "kalliope_supported_version cannot handle this format of version %s. "
                                "Only major version should be provided" %
                                supported_version)
                            success_loading = False

        return success_loading
Exemplo n.º 14
0
    def _check_dna_file(dna_file):
        """
        Check the content of a DNA file
        :param dna_file: the dna to check
        :return: True if ok, False otherwise
        """
        success_loading = True
        if "name" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"name\" tag")
            success_loading = False

        if "type" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"type\" tag")
            success_loading = False

        else:
            # we have a type, check that is a valid one
            if dna_file["type"] not in VALID_DNA_MODULE_TYPE:
                Utils.print_danger("The DNA type %s is not valid" % dna_file["type"])
                Utils.print_danger("The DNA type must be one of the following: %s" % VALID_DNA_MODULE_TYPE)
                success_loading = False

        if "kalliope_supported_version" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"kalliope_supported_version\" tag")
            success_loading = False
        else:
            # kalliope_supported_version must be a non empty list
            if not isinstance(dna_file["kalliope_supported_version"], list):
                Utils.print_danger("kalliope_supported_version is not a list")
                success_loading = False
            else:
                if not dna_file["kalliope_supported_version"]:
                    Utils.print_danger("kalliope_supported_version cannot be empty")
                    success_loading = False

        return success_loading
Exemplo n.º 15
0
    def _check_dna_file(dna_file):
        """
        Check the content of a DNA file
        :param dna_file: the dna to check
        :return: True if ok, False otherwise
        """
        success_loading = True
        if "name" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"name\" tag")
            success_loading = False

        if "type" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"type\" tag")
            success_loading = False

        else:
            # we have a type, check that is a valid one
            if dna_file["type"] not in VALID_DNA_MODULE_TYPE:
                Utils.print_danger("The DNA type %s is not valid" % dna_file["type"])
                Utils.print_danger("The DNA type must be one of the following: %s" % VALID_DNA_MODULE_TYPE)
                success_loading = False

        if "kalliope_supported_version" not in dna_file:
            Utils.print_danger("The DNA of does not contains a \"kalliope_supported_version\" tag")
            success_loading = False
        else:
            # kalliope_supported_version must be a non empty list
            if not isinstance(dna_file["kalliope_supported_version"], list):
                Utils.print_danger("kalliope_supported_version is not a list")
                success_loading = False
            else:
                if not dna_file["kalliope_supported_version"]:
                    Utils.print_danger("kalliope_supported_version cannot be empty")
                    success_loading = False
                else:
                    for supported_version in dna_file["kalliope_supported_version"]:
                        # check if major version is provided
                        if not re.search('^[\d]*[.][\d]*$', str(supported_version)):
                            Utils.print_danger("kalliope_supported_version cannot handle this format of version %s. "
                                               "Only major version should be provided" % supported_version)
                            success_loading = False


        return success_loading
Exemplo n.º 16
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)