Example #1
0
def task(quit_event):
    #mic = Microphone(quit_event=quit_event)
    mic_index = None
    for i, microphone_name in enumerate(Microphone.list_microphone_names()):
        if 'seeed' in microphone_name:
            mic_index = i
            print("Using microphone {}".format(microphone_name))
            break
    if not mic_index:
        print("Could not find a proper microphone")
        exit()
    with Microphone(device_index=mic_index) as mic:
        recognizer = Recognizer()
        while not quit_event.is_set():
            pixel_ring.off()
            print("Listening for keyword")
            data = recognizer.listen(source=mic)
            kw_text = recognizer.recognize_sphinx(data)
            print("Heard '{}' while listening for keyword".format(kw_text))
            if kw_text == name:
                print('Wake up')
                pixel_ring.listen()
                data = recognizer.listen(mic)
                pixel_ring.think()
                text = recognizer.recognize_sphinx(data)
                print('Done listening')
                pixel_ring.off()
                if text:
                    print('Recognized {}'.format(text))
                    tts.say(text)
Example #2
0
def task(quit_event):
    with Microphone(device_index=mic_index, quit_event=quit_event) as mic:
        recognizer = Recognizer()
        while not quit_event.is_set():
            pixel_ring.off()
            print("Listening for keyword")
            data = recognizer.listen(source=mic)
            kw_text = recognizer.recognize_sphinx(data)
            print("Heard '{}' while listening for keyword".format(kw_text))
            if kw_text == name:
                print('Wake up')
                pixel_ring.listen()
                data = recognizer.listen(mic)
                pixel_ring.think()
                text = recognizer.recognize_sphinx(data)
                print('Done listening')
                pixel_ring.off()
                if text:
                    print('Recognized {}'.format(text))
                    tts.say(text)
Example #3
0
def recognizer_func(recognizer: sr.Recognizer, audio: sr.AudioData):
    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)`
        print("Decoding")
        print("Google Speech Recognition thinks you said:  " + recognizer.recognize_sphinx(audio))

    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
class AudioToTextSphinxPipe(PushPipe[AudioRecording, str]):
    def __init__(self, postProcessCallback=None):
        super().__init__(postProcessCallback=postProcessCallback)
        self.sr = Recognizer()

    def process(self, recording: AudioRecording,
                passThrough: PushPipe.PassThrough):
        print('Sample width:', recording.METADATA.getFormatByteSize())
        data = AudioData(recording.data, recording.METADATA.Rate,
                         recording.METADATA.getFormatByteSize())
        try:
            return self.sr.recognize_sphinx(data, 'en-IN')
        except UnknownValueError:
            self.setErrored("Couldn't identify speech.")
            return ''
    def listen_for_hotword(self, recognizer: sr.Recognizer, audio: sr.AudioData):
        """
        Callback that runs continuously in the background to listen
        for hotwords using offline speech recognition.

        :param recognizer: the Recognizer instance
        :param audio: audio data to analyze
        :return: None
        """
        if self.listening_for_command:
            return
        try:
            speech_as_text: Decoder = recognizer.recognize_sphinx(audio, keyword_entries=keywords)
            print(f"Heard '{speech_as_text}'")

            for keyword, sensitivity in keywords:
                if keyword in speech_as_text:
                    self.listen_command_queue.put('command')
                    break
        except sr.UnknownValueError:
            print("Oops! Didn't catch that")
class CommandsRecognition(ApiState):
    def __init__(self, callback, language="pl-PL", api_option=ApiOption.GOOGLE):
        super().__init__()
        self.callback_command_detected = callback
        self.api_option = api_option
        self.language = language
        self.listen_thread = None
        self.phrase_time_limit = 3

        try:
            self.recognizer = Recognizer()
            self.microphone = Microphone()
            """
                adjust the recognizer sensitivity to ambient noise and record audio
                from the microphone
            """
            with self.microphone as source:
                self.recognizer.adjust_for_ambient_noise(source)

        except OSError as ose_err:
            Logger.critical("OSError: {0}".format(ose_err))
            self.api_runs = False
            self.api_info = GLO_MSG['MICROPHONE_FAILURE']
            return
        except Exception as err:
            Logger.critical("Exception: {0}".format(err))
            self.api_runs = False
            self.api_info = GLO_MSG['MICROPHONE_FAILURE']
            return
        Logger.debug("Initialization of CommandsRecognition class")
        self.api_info = GLO_MSG['MICROPHONE_INITIALIZED']

    def validate_command(self, command):
        if command.lower() in GLO_CMD.values():
            detected_command_id = GET_COMMAND(command.lower())
            Logger.info("GLO_CMD command available -> {0}".format(command.lower()))
            self.callback_command_detected(detected_command_id)
        else:
            Logger.info("Detected command: {0}".format(command.lower()))

    def callback_recognition(self, recognizer, audio):
        try:
            command = self._api_recognition(audio)
            self.validate_command(command)
        except UnboundLocalError as err:
            Logger.warning("UnboundLocalError : {0} ".format(err))
        except RequestError as err:
            Logger.warning("RequestError : {0} ".format(err))
        except UnknownValueError as err:
            Logger.debug("UnknownValueError : {0} ".format(err))

    def background_listen(self):
        self.listen_thread = self.recognizer.listen_in_background(
            source=self.microphone, callback=self.callback_recognition, phrase_time_limit=self.phrase_time_limit)

    def _api_recognition(self, audio):
        if self.api_option is ApiOption.GOOGLE:
            return self.recognizer.recognize_google(audio, language=self.language)
        elif self.api_option is ApiOption.GOOGLE_CLOUD:
            # Support languages: https://cloud.google.com/speech-to-text/docs/languages
            return self.recognizer.recognize_google_cloud(audio, credentials_json='', language=self.language)
        elif self.api_option is ApiOption.SPHINX:
            # Support languages : https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/
            return self.recognizer.recognize_sphinx(audio, language=self.language)
        elif self.api_option is ApiOption.WIT:
            # Support languages : https://wit.ai/faq, login required
            return self.recognizer.recognize_wit(audio, key='',)
        elif self.api_option is ApiOption.AZURE_BING:
            # Support languages : https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/language-support, login required
            self.recognizer.recognize_bing(audio, key='', language=self.language)
        elif self.api_option is ApiOption.LEX:
            # Support languages: ONLY ENG -> https://docs.aws.amazon.com/lex/latest/dg/gl-limits.html
            return self.recognizer.recognize_lex(audio)
        elif self.api_option is ApiOption.HOUNDIFY:
            # Support languages: ONLY ENG, login required
            return self.recognizer.recognize_houndify(audio, client_id='', client_key='')
        elif self.api_option is ApiOption.IBM:
            # Support languages : https://www.ibm.com/watson/services/language-translator/, login required
            return self.recognizer.recognize_ibm(audio, username='', password='', language=self.language)
        else:
            Logger.error("Api recognition option is not defined")
Example #7
0
class Speech(object):
    def __init__(self):
        rospy.init_node("speech")
        self.pub = rospy.Publisher("speech_recognizer",
                                   String,
                                   latch=True,
                                   queue_size=1)
        self.recognizer = Recognizer()
        # self.recognizer.energy_threshold = 1000
        # self.recognizer.pause_threshold = .7
        self.microphone = Microphone()

    @staticmethod
    def check_internet_connection():
        connection = httplib2.HTTPConnectionWithTimeout("www.google.com",
                                                        timeout=5)
        try:
            connection.request("HEAD", "/")
            connection.close()
            return True
        except httplib2.HttpLib2Error:
            connection.close()
            return False

    def __recognize_helper(self, recognized_speech):
        '''
        Once speech obtained do TODO:something with it
        '''
        print(recognized_speech)
        if recognized_speech != "":
            rospy.loginfo("You said: " + recognized_speech)
            self.pub.publish(recognized_speech)

    def __microphone_helper(self, ):
        rospy.loginfo('Listening...')
        with self.microphone as source:
            # If phase_time_limit is not set to 5 it will
            # take a really long time every 3-4th attempt
            audio = self.recognizer.listen(source, phrase_time_limit=5)
        rospy.loginfo('Got a sound; recognizing...')
        return audio

    def recognize_google(self):
        with self.microphone as source:
            self.recognizer.adjust_for_ambient_noise(source)

        try:
            while not rospy.is_shutdown():
                audio = self.__microphone_helper()
                recognized_speech = ""
                if Speech.check_internet_connection():
                    try:
                        recognized_speech = self.recognizer.recognize_google(
                            audio)
                    except sr.UnknownValueError:
                        rospy.logerr("Could not understand audio.")
                    except sr.RequestError:
                        rospy.logerr("Could not request results.")
                else:
                    rospy.logerr("No internet conneciton for Google API")

                self.__recognize_helper(recognized_speech)

        except Exception as exc:
            rospy.logerr(exc)

    def recognize_sphynix(self):
        with self.microphone as source:
            self.recognizer.adjust_for_ambient_noise(source)
        try:
            while not rospy.is_shutdown():
                audio = self.__microphone_helper()
                recognized_speech = ""

                try:
                    recognized_speech = self.recognizer.recognize_sphinx(audio)
                except sr.UnknownValueError:
                    rospy.logerr("Could not understand audio.")
                except sr.RequestError:
                    rospy.logerr("""Could not request results. Do you have
                     pocket Sphynx installed?""")
                self.__recognize_helper(recognized_speech)
        except Exception as exc:
            rospy.logerr(exc)
# On enregistre le son

with Microphone() as source:
    print("Réglage du bruit ambiant... Patientez...")
    recognizer.adjust_for_ambient_noise(source)
    print("Vous pouvez parler...")
    recorded_audio = recognizer.listen(source)
    print("Enregistrement terminé !")
    with open("record.wav", "wb") as f:
        f.write(recorded_audio.get_wav_data())
# Reconnaissance de l'audio

try:
    print("Reconnaissance du texte...")
    googletext = recognizer.recognize_google(recorded_audio, language="fr-FR")
    print("Google pense que vous avez dit {}".format(googletext))
except UnknownValueError:
    print("L'audio n'as pas été compris par Google")
except Exception as ex:
    print(ex)

try:
    print("Reconnaissance du texte...")
    sphinxtext = recognizer.recognize_sphinx(recorded_audio, language="fr-FR")
    print("Sphinx pense que vous avez dit {}".format(sphinxtext))
except UnknownValueError:
    print("L'audio n'as pas été compris par Sphinx")
except Exception as ex:
    print(ex)