Python speech_recognition 模块,UnknownValueError() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用speech_recognition.UnknownValueError()

项目:customer-service-chatbot    作者:xploiter-projects    | 项目源码 | 文件源码
def SpeechToText():
        r = sr.Recognizer()   #Speech recognition
        with sr.Microphone() as source:
            print("Say something!")
            audio = r.listen(source)
            message = r.recognize_google(audio)
            print("Check: "+message)
        try:
            print("User: " + r.recognize_google(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))
        return message

#function to find importance of words to use them to deduce that which thing is being asked more
项目:B.E.N.J.I.    作者:the-ethan-hunt    | 项目源码 | 文件源码
def OnClicked(self):
        r = sr.Recognizer()
        with sr.Microphone() as source:
            speak.say('Hey I am Listening ')
            speak.runAndWait()
            audio = r.listen(source)
        try:
            put=r.recognize_google(audio)
            self.displayText(put)
            self.textBox.insert('1.2',put)
            self.textBox.delete('1.2',tk.END)
            events(self,put)
        except sr.UnknownValueError:
            self.displayText("Could not understand audio")
        except sr.RequestError as e:
            self.displayText("Could not request results; {0}".format(e))
项目:B.E.N.J.I.    作者:the-ethan-hunt    | 项目源码 | 文件源码
def OnClicked(self):
        r = sr.Recognizer()
        with sr.Microphone() as source:
            speak.say('Hey I am Listening ')
            speak.runAndWait()
            audio = r.listen(source)
        try:
            put=r.recognize_google(audio)

            self.displayText(put)
            self.textBox.insert('1.2',put)
            put=put.lower()
            put = put.strip()
            #put = re.sub(r'[?|$|.|!]', r'', put)
            link=put.split()
            events(self,put,link)
        except sr.UnknownValueError:
            self.displayText("Could not understand audio")
        except sr.RequestError as e:
            self.displayText("Could not request results; {0}".format(e))
项目:transcriber    作者:xaratustrah    | 项目源码 | 文件源码
def recognize(recognizer_instance, audio_data):
    # 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)`
        text = str(recognizer_instance.recognize_google(audio_data))
        print(text)
        if 'on' in text.lower():
            print('Turning on!')
        if 'off' in text.lower():
            print('Turning off!')
    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))
项目:Lab-Assistant    作者:SPARC-Auburn    | 项目源码 | 文件源码
def listen(self):
        """Listens to response from microphone and converts to text"""
        self.userCommand = ""
        with sr.Microphone(device_index=self.mic, chunk_size=1024, sample_rate=48000) as source:
            print ("\tThreshold: " + str(self.r.energy_threshold))
            print ("\tWaiting for words...")
            try:
                audio = self.r.listen(source, timeout=5)
                # self.playSound("end.mp3")
                try:
                    self.userCommand = self.r.recognize_google(audio)
                    self.userCommand = self.userCommand.lower()
                    if not self.processcommand(self.userCommand, source):
                        return False
                    else:
                        return True
                except sr.UnknownValueError:
                    print ("\t...")
                except sr.RequestError as e:
                    print("\tCould not request results from Google Speech Recognition service; {0}".format(e))
                except Exception as e:
                    print (str(e))
            except Exception:
                print ("\tNo audio heard")
                pass
项目:Personal_AI_Assistant    作者:PratylenClub    | 项目源码 | 文件源码
def active_listen(self):
        with self.sr_mic() as microphone:
            try:
                audio = self.sr_recognizer.listen(microphone,timeout=TIME_OUT_CONSTANT, phrase_time_limit=PHRASE_TIME_LIMIT)
            except sr.WaitTimeoutError:
                return ""
        msg = ""
        try:
            msg = self.sr_recognizer.recognize_google(audio)#,language="fr-FR") 
            #msg = self.sr_recognizer.recognize_sphinx(audio)
            print(msg.lower())
        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
            self.speak("Could not request results from Google STT. Check your connection! {0}".format(e))
        except:
            self.speak("Something it is Wrong! It hurts! Wrong! Wrong! Wrong!")
        finally:
            return msg.lower()
项目:Personal_AI_Assistant    作者:PratylenClub    | 项目源码 | 文件源码
def active_listen(self):
        with self.sr_mic() as microphone:
            try:
                audio = self.sr_recognizer.listen(microphone,timeout=TIME_OUT_CONSTANT, phrase_time_limit=PHRASE_TIME_LIMIT)
            except sr.WaitTimeoutError:
                pass
        msg = ""
        try:
            msg = self.sr_recognizer.recognize_google(audio)#,language="fr-FR") 
            #msg = self.sr_recognizer.recognize_sphinx(audio)
            print(msg.lower())
        except sr.UnknownValueError:
            pass#print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
            self.speak("Could not request results from Google STT. Check your connection! {0}".format(e))
        except:
            pass#self.speak("Something it is Wrong! It hurts! Wrong! Wrong! Wrong!")
        finally:
            return msg.lower()
项目:aide    作者:Lambda-3    | 项目源码 | 文件源码
def process_audio(self, audio):
        """
        Converts given audio to speech.

        :param audio: Audio snippet to be converted to text.
        """
        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)`
            return self.recognizer.recognize_google(audio)
        except UnknownValueError:
            print("Google Speech Recognition could not understand audio")
        except RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e))
项目:kalliope    作者:kalliope-project    | 项目源码 | 文件源码
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()
项目:kalliope    作者:kalliope-project    | 项目源码 | 文件源码
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()
项目:kalliope    作者:kalliope-project    | 项目源码 | 文件源码
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)

        self.stop_listening()
项目:kalliope    作者:kalliope-project    | 项目源码 | 文件源码
def bing_callback(self, recognizer, audio):
        """
        called from the background thread
        """
        try:
            captured_audio = recognizer.recognize_bing(audio,
                                                       key=self.key,
                                                       language=self.language,
                                                       show_all=self.show_all)
            Utils.print_success("Bing Speech Recognition thinks you said %s" % captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning("Bing 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 Bing 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()
项目:hal    作者:brandenk514    | 项目源码 | 文件源码
def listen(self):
        """
        :param self
        This is a wrapper method to the Speech_Recognition package
        found @ https://gist.github.com/GGulati/1ebaeaaa7f7408647fef#file-jarvis-py
        """
        try:
            with speech_recognition.Microphone() as source:
                self.recognizer.adjust_for_ambient_noise(source)
                audio = self.recognizer.listen(source, timeout=10, phrase_time_limit=None)
            return self.recognizer.recognize_google(audio)
        except speech_recognition.WaitTimeoutError:
            self.error_message = "I could not hear you!"
        except speech_recognition.UnknownValueError:
            self.error_message = "I did not quite get that"
        except speech_recognition.RequestError as e:
            self.error_message = "Recognition Error: {0}".format(e)
项目:xarvis    作者:satwikkansal    | 项目源码 | 文件源码
def callback(recognizer, audio):
    try:
        recognized = recognizer.recognize_google(audio)
        if recognized:
            logging.info("clipped"+recognized+"\n")
            copy_to_clipboard(recognized)
            logging.info("copied text to clipboard.")
            beep()
            process_recognized_string(recognized)
        return recognized

    except sr.UnknownValueError:
        print("..")

    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
项目:CodeLabs    作者:TheIoTLearningInitiative    | 项目源码 | 文件源码
def recognize():

    r = sr.Recognizer()
    m = sr.Microphone()

    try:
        with m as source: r.adjust_for_ambient_noise(source)
        print("Escuchando...")
        play('beep.wav')
        with m as source: audio = r.listen(source)
        print("Enterado! Reconociendo...")
        try:
            value = r.recognize_google(audio, language="es-MX")
            if str is bytes:
                print(u"Dijiste {}".format(value).encode("utf-8"))
            else:
                print("Dijiste {}".format(value))
        except sr.UnknownValueError:
            print("Ups! No entendimos")
        except sr.RequestError as e:
            print("Oh oh! Error en la peticion a Google; {0}".format(e))
    except KeyboardInterrupt:
        pass
项目:furbymt    作者:starfys    | 项目源码 | 文件源码
def get_query():
    with sr.Microphone() as source:
        print("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)`
        tts_result = r.recognize_google(audio)
        print('Got ' + tts_result)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
        return None
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
        return None
    return tts_result
项目:Friday    作者:Zenohm    | 项目源码 | 文件源码
def _google_stt(self):
        """
        Uses Google's Speech to Text engine to
        understand speech and convert it into
        text.
        :return: String, either the spoken text or error text.
        """
        click.echo("Please speak now")
        # Listen to the microphone set up in the __init__ file.
        with self.microphone as mic_source:
            audio = self.recognizer.listen(mic_source)
        click.echo("Processing audio")
        try:
            # Try to recognize the text
            return self.recognizer.recognize_google(audio, show_all=self.debugging)
        except sr.UnknownValueError:  # speech is unintelligible
            return "Google could not understand the audio."
        except sr.RequestError:
            return "Could not request results from Google's speech recognition service."
项目:MUSTani-Robot    作者:swayam01    | 项目源码 | 文件源码
def main():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Say something!")
        audio = r.listen(source)

    try:
        speech_text = r.recognize_google(audio).lower().replace("'", "")
        print("Mustani thinks you said '" + speech_text + "'")
    except sr.UnknownValueError:
        print("Mustani could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition
        service; {0}".format(e))

    brain(name,speech_text,city_name, city_code)
项目:Smart-Mirror    作者:aishmittal    | 项目源码 | 文件源码
def start_speech_recording(tmp): 
# Record Audio
    global recognised_speech
    BING_KEY = "cfee7d6db79d4671b9cea936da4689d7" 
    while True:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("Say something!")
            r.adjust_for_ambient_noise(source, duration = 1)
            audio = r.listen(source)

        try:
            recognised_speech = r.recognize_bing(audio, key=BING_KEY).lower()
            print("Microsoft Bing Voice Recognition thinks you said:" + recognised_speech)
            if "hallo" in recognised_speech or "wakeup" in recognised_speech or "start" in recognised_speech or "makeup" in recognised_speech or "star" in recognised_speech or "breakup" in recognised_speech:
                thread.start_new_thread( face_identify, (3, ) )       
        except sr.UnknownValueError:
            print("Microsoft Bing Voice Recognition could not understand audio")
        except sr.RequestError as e:
            print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
项目:Smart-Mirror    作者:aishmittal    | 项目源码 | 文件源码
def start_speech_recording(tmp): 
# Record Audio
    global recognised_speech 
    while True:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("Say something!")
            r.adjust_for_ambient_noise(source, duration = 1)
            audio = r.listen(source)

        try:
            recognised_speech = r.recognize_google(audio).lower()
            print("You said: " + r.recognize_google(audio))
            if "hallo" in recognised_speech or "wakeup" in recognised_speech or "start" in recognised_speech or "makeup" in recognised_speech or "star" in recognised_speech or "breakup" in recognised_speech:
                thread.start_new_thread( face_identify, (3, ) )       
        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))
项目:synthia    作者:TomAlanCarroll    | 项目源码 | 文件源码
def recognize_speech():
    # obtain audio from the microphone
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Say something!")
        audio = r.listen(source)

    try:
        print("Starting speech recognition")
        cred_json = json.loads(open(config.get('google_cloud_api_service_account_json')).read())
        recognized_speech = r.recognize_google_cloud(audio, credentials_json=cred_json)
        # we need some special handling here to correctly print unicode characters to standard output
        if str is bytes:  # this version of Python uses bytes for strings (Python 2)
            print(u"You said {}".format(recognized_speech).encode("utf-8"))
        else:  # this version of Python uses unicode for strings (Python 3+)
            print("You said {}".format(recognized_speech))
    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))
项目:Harmonbot    作者:Harmon758    | 项目源码 | 文件源码
def process_listen(self):
        if not os.path.isfile("data/temp/heard.pcm") or os.stat("data/temp/heard.pcm").st_size == 0:
            await self.bot.send_embed(self.text_channel, ":warning: No input found")
            return
        func = functools.partial(subprocess.call, ["ffmpeg", "-f", "s16le", "-y", "-ar", "44.1k", "-ac", "2", "-i", "data/temp/heard.pcm", "data/temp/heard.wav"], shell = True)
        await self.bot.loop.run_in_executor(None, func)
        with speech_recognition.AudioFile("data/temp/heard.wav") as source:
            audio = self.recognizer.record(source)
        '''
        try:
            await self.bot.reply("Sphinx thinks you said: " + recognizer.recognize_sphinx(audio))
        except speech_recognition.UnknownValueError:
            await self.bot.reply("Sphinx could not understand audio")
        except speech_recognition.RequestError as e:
            await self.bot.reply("Sphinx error; {0}".format(e))
        '''
        try:
            text = self.recognizer.recognize_google(audio)
            await self.bot.send_embed(self.text_channel, "I think you said: `{}`".format(text))
        except speech_recognition.UnknownValueError:
            # await self.bot.send_embed(self.text_channel, ":no_entry: Google Speech Recognition could not understand audio")
            await self.bot.send_embed(self.text_channel, ":no_entry: I couldn't understand that")
        except speech_recognition.RequestError as e:
            await self.bot.send_embed(self.text_channel, ":warning: Could not request results from Google Speech Recognition service; {}".format(e))
        else:
            response = clients.aiml_kernel.respond(text)
            # TODO: Handle brain not loaded?
            if not response:
                games_cog = client.get_cog("Games")
                if not games_cog: return
                response = await games_cog.cleverbot_get_reply(text)
            await self.bot.send_embed(self.text_channel, "Responding with: `{}`".format(response))
            await self.play_tts(response, self.bot.user)
        # open("data/heard.pcm", 'w').close() # necessary?
        # os.remove ?
项目:susi_linux    作者:fossasia    | 项目源码 | 文件源码
def on_enter(self, payload=None):
        """ Executed on the entry to the Recognizing State. Upon entry, audio is captured from the Microphone and
        recognition with preferred speech recognition engine is done. If successful, the machine transitions to Busy
        State. On failure, it transitions to Error state.
        :param payload: No payload is expected by this state
        :return: None
        """

        self.notify_renderer('listening')
        recognizer = self.components.recognizer
        try:
            print("Say something!")
            with self.components.microphone as source:
                audio = recognizer.listen(source, phrase_time_limit=5)
            self.notify_renderer('recognizing')
            print("Got it! Now to recognize it...")
            try:
                value = self.__recognize_audio(
                    audio=audio, recognizer=recognizer)
                print(value)
                self.notify_renderer('recognized', value)
                self.transition(self.allowedStateTransitions.get(
                    'busy'), payload=value)
            except sr.UnknownValueError:
                print("Oops! Didn't catch that")
                self.transition(self.allowedStateTransitions.get(
                    'error'), payload='RecognitionError')

            except sr.RequestError as e:
                print(
                    "Uh oh! Couldn't request results from Speech Recognition service; {0}".format(e))
                self.transition(self.allowedStateTransitions.get(
                    'error'), payload='ConnectionError')

        except KeyboardInterrupt:
            pass
项目:Jarvis    作者:sukeesh    | 项目源码 | 文件源码
def listen():
    with speech_recognition.Microphone() as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    try:
        return recognizer.recognize_sphinx(audio)
        # or: return recognizer.recognize_google(audio)
    except speech_recognition.UnknownValueError:
        print("Could not understand audio")
    except speech_recognition.RequestError as e:
        print("Recog Error; {0}".format(e))

    return ""
项目:transcriber    作者:xaratustrah    | 项目源码 | 文件源码
def cut_and_send(infile, outfile, length):
    # print(infile)
    # print(outfile)
    # print(length)
    # return
    myaudio = AudioSegment.from_file(infile, "wav")
    chunk_length_ms = length  # pydub calculates in millisec
    chunks = make_chunks(myaudio, chunk_length_ms)  # Make chunks of one sec

    for i, chunk in enumerate(chunks):
        chunk_name = "chunk{0}.wav".format(i)
        print("exporting", chunk_name)
        chunk.export(chunk_name, format="wav")
        r = sr.Recognizer()
        with sr.AudioFile(chunk_name) as source:
            audio = r.record(source)
        # recognize speech using Google Speech Recognition
        try:
            # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
            # instead of `r.recognize_google(audio)`
            txt = r.recognize_google(audio) + " "
            with open(outfile, 'a') as f:
                f.write(txt)
        except sr.UnknownValueError:
            print("Ehm... sorry not understood this one.")
        except sr.RequestError as e:
            print("Request failed; {0}".format(e))
        os.remove(chunk_name)
项目:Personal_AI_Assistant    作者:PratylenClub    | 项目源码 | 文件源码
def callback(recognizer, audio):
    try:
        sentence = recognizer.recognize_google(audio, language=language)
        wave_file_name = "train.wav"
        wav_file = open(wave_file_name,"wb")
        wav_file.write(audio.get_wav_data())
        wav_file.close()
        wave, sample_rate = librosa.load(wave_file_name, mono=True, sr=None)
        wave = wave[::3]
        save_recording(wave_file_name,wave,sentence,CSV_BIG_ONE)

    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))
项目:Personal_AI_Assistant    作者:PratylenClub    | 项目源码 | 文件源码
def callback(recognizer, audio):
    # received audio data, now we'll recognize it 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)`
        #print(r.recognize_sphinx(audio,language="fr-FR"))#
        print recognizer.recognize_google(audio)#, language="fr-FR"))
    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))
项目:Python-Scripts    作者:amitsagtani97    | 项目源码 | 文件源码
def stot():
    r = sr.Recognizer()


    with sr.Microphone() as source:
        try:
            print("Say it!!")
            audio = r.listen(source)

            print(r.recognize_google(audio))
        except sr.UnknownValueError:
            print("Could not understand audio")
        except sr.RequestError as e:
            print("Could not request results; {0}".format(e))
项目:cozmo    作者:yp7y-stu    | 项目源码 | 文件源码
def hear(source, r, robot):
    audio = r.listen(source)
    try:
        recognized = r.recognize_google(audio)
        print("You said: " + recognized)
        if command_activate in recognized or command_activate.lower() in recognized:
            print("Action command recognized")

            if command_pickup in recognized:
                do_blocks(robot)

            elif command_dance in recognized:
                do_dance()

            elif command_lookatme in recognized:
                do_lookforface(robot)

            elif command_followme in recognized:
                do_lookforface(robot)
                do_followface(robot)

            elif command_takepictureofme in recognized:
                do_lookforface(robot)
                do_takepicture(robot)

            elif command_takepicture in recognized:
                do_takepicture(robot)

            elif command_say in recognized:
                do_say(recognized, robot)

            else:
                print("Command not recognized")

        else:
            print("You did not say the magic word " + command_activate)

    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))
项目:kalliope    作者:kalliope-project    | 项目源码 | 文件源码
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)

        # stop listening for an audio
        self.stop_listening()
项目:xarvis    作者:satwikkansal    | 项目源码 | 文件源码
def callback(recognizer, audio):
    # received audio data, now we'll recognize it 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)`
        print("Google Speech Recognition thinks you said " + recognizer.recognize_google(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))
项目:Jarvis    作者:m4n3dw0lf    | 项目源码 | 文件源码
def Listen(self):
        with sr.Microphone() as source:
            self.audio = self.rec.listen(source)
        try:
            self.result = self.rec.recognize_google(self.audio)
            print self.result
            return self.result
        except sr.UnknownValueError:
                    print("Google Speech Recognition could not understand audio")
            except sr.RequestError:
                    self.Say("Could not request results from Google Speech Recognition service master, check our internet connection.")
项目:Persia    作者:rjcf18    | 项目源码 | 文件源码
def initiate_Persia(self):
        """
        Initializes Persia and starts the listening loop
        """

        # starts the recognizer
        r = sr.Recognizer()

        with sr.Microphone() as source:

            while True:
                logger.debug("Awaiting user input.")
                audio = r.listen(source)

                logger.debug("Interpreting user input.")

                # Speech recognition using Google Speech Recognition
                try:
                    result = r.recognize_google(audio)
                    #result = r.recognize_sphinx(audio)

                    self.handle_action(result)

                except sr.UnknownValueError:
                    logger.debug("Could not understand audio")
                    #Persia.speak("I'm sorry, but I couldn't understand what you said.")
                except sr.RequestError as e:
                    logger.warn("Could not request results from Google Speech Recognition service: %s", e)
                except Exception as e:
                    logger.error("Could not process text: %s", e)
项目:audio-tagging-toolkit    作者:hipstas    | 项目源码 | 文件源码
def transcribe(inputfile,outputfile='',to_txt=True):
    wav_source=True
    if inputfile.lower()[-4:]!='.wav':     # Creates a temporary WAV
        wav_source=False                         # if input is MP3
        temp_filename=inputfile.split('/')[-1]+'_temp.wav'
        wav_path='/var/tmp/'+temp_filename   # Pathname for temp WAV
        subprocess.call(['ffmpeg', '-y', '-i', inputfile, wav_path]) # '-y' option overwrites existing file if present
    else:
        wav_path=inputfile
    transcript=''
    r = sr.Recognizer()
    with sr.AudioFile(wav_path) as source:
        audio = r.record(source)    # read the entire audio file
    try:                            # recognize speech using Sphinx
        print('Processing ...')
        transcript=r.recognize_sphinx(audio)
    except sr.UnknownValueError:
        print("Sphinx error: No speech detected.")
    except sr.RequestError as e:
        print("Sphinx error; {0}".format(e))
    if wav_source==False:
        os.remove(wav_path)       # deleting temp WAV
    if to_txt==True:
        if outputfile=='':
            outputfile=inputfile[:-4]+'.pocketsphinx.txt'
        with open(outputfile, 'w') as fo:
            fo.write(transcript)
        return transcript
    else:
        return transcript
项目:xiaokai-bot    作者:CCZU-DEV    | 项目源码 | 文件源码
def _recognize_bing(wav_path, api_key, language='zh-CN'):
    r = sr.Recognizer()
    with sr.AudioFile(wav_path) as source:
        audio = r.record(source)
    try:
        text = r.recognize_bing(audio, key=api_key, language=language)
        return text
    except (sr.UnknownValueError, sr.RequestError):
        return None
项目:pandora    作者:ryzokuken    | 项目源码 | 文件源码
def listen(recognizer, message='Say Something'):
    with speech_recognition.Microphone() as source:
        print(message)
        audio = recognizer.listen(source)
    try:
        return recognizer.recognize_google(audio).lower()
    except speech_recognition.UnknownValueError:
        try:
            return recognizer.recognize_sphinx(audio).lower()
        except speech_recognition.UnknownValueError:
            print("Pandora could not understand audio")
        except speech_recognition.RequestError as e:
            print("Sphinx error; {0}".format(e))
    except speech_recognition.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
项目:SearchAndWatch    作者:aysedilekk    | 项目源码 | 文件源码
def sound_to_text():
    NEWS = []
    for i in range(int(length / 8)):

        WAV_FILE = path.join(path.dirname(path.realpath(__file__)), 'nlp_' + str(i) + '.wav')

        # use "english.wav" as the audio source
        r = sr.Recognizer()
        with sr.WavFile(WAV_FILE) as source:
            audio = r.record(source)  # read the entire WAV file
        # 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)`
            print(i, ". part: ", r.recognize_google(audio,language="tr"))

            NEWS.append(r.recognize_google(audio,language="tr"))

        except sr.UnknownValueError:
            # print("Google Speech Recognition could not understand audio")
            pass
        except sr.RequestError as e:
            # print("Could not request results from Google Speech Recognition service; {0}".format(e))
            pass

    return NEWS
项目:CodeLabs    作者:TheIoTLearningInitiative    | 项目源码 | 文件源码
def recognize(self):
        if self.engine == 'google':
            try:
                from pprint import pprint
                print("Google Speech Recognition")
                return self.r.recognize_google(self.audio, show_all=False)
            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))
        if self.engine == 'witai':
            WIT_AI_KEY = "7LUFYD6ZZKLYSI652C75J4UZWTIMJIPX"
            try:
                print("Wit.ai")
                return self.r.recognize_wit(self.audio, key=WIT_AI_KEY)
            except sr.UnknownValueError:
                print("Wit.ai could not understand audio")
            except sr.RequestError as e:
                print("Could not request results from Wit.ai service; {0}".format(e))
        if self.engine == 'sphinx':
            try:
                print("Sphinx")
                return self.r.recognize_sphinx(self.audio)
            except sr.UnknownValueError:
                print("Sphinx could not understand audio")
            except sr.RequestError as e:
                print("Sphinx error; {0}".format(e))
        return None
项目:Pyggy    作者:korymath    | 项目源码 | 文件源码
def callback(recognizer, audio):
    # received audio data, now we'll recognize it 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)`
        print("Google Speech Recognition thinks you said " + recognizer.recognize_google(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))
项目:awesome-hacking-via-python    作者:shashi12533    | 项目源码 | 文件源码
def callback(r,audio):
    try:
        s=r.recognize_google(audio)
        print(s)
        brain(s)
    except sr.UnknownValueError:
        print("~~~~~~~~~~~~~~~~~~~")
    except sr.RequestError as e:
        print('Internet Error......!')
项目:Onyx    作者:OnyxProject    | 项目源码 | 文件源码
def detected_callback(self):
        self.detector.terminate()
        play_wav(onyx.__path__[0] + "/client/speech/resources/ding.wav")

        r = sr.Recognizer()

        with sr.Microphone() as source:
            print("Say something!")
            audio = r.listen(source, timeout=1, phrase_time_limit=5)

        try:
            result = stt.execute(audio, language=self.lang)
            print("You said: " + result)

            def create_ws():
                def onConnected(event=None):
                    print ("Sending message...")
                    payload = {
                            'utterances': [result]
                    }
                    ws.emit(Message('recognizer_loop:utterance', payload))
                    t.close()
                    #self.detector.start(self.detected_callback)


                ws = WebsocketClient()
                ws.on('connected', onConnected)
                # This will block until the client gets closed
                ws.run_forever()

            t = threading.Thread(target=create_ws)
            t.start()
            time.sleep(2)
            self.detector.start(self.detected_callback)


        except sr.UnknownValueError:
            print("Speech Recognition could not understand audio")
        except sr.RequestError as e:
            print("Could not request results from Speech Recognition service; {0}".format(e))
项目:furbymt    作者:starfys    | 项目源码 | 文件源码
def get_command():
    with sr.Microphone() as source:
        print("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)`
        tts_result = r.recognize_google(audio)
        print('Got ' + tts_result)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
        return None
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
        return None
    #Read keys from json
    with open('keys.json','r') as keys_file:
        keys = json.load(keys_file)
    apiai_client_token = keys['apiai_client_token']
    #Create a session
    session = requests.Session()
    session.headers.update({"Authorization":"Bearer {}".format(apiai_client_token),
                        "Content-Type":"application/json; charset=utf-8"})
    #API.ai
    API_BASE_URL="https://api.api.ai/v1/"
    #Make a request
    return session.get(API_BASE_URL+"query", params={"query": tts_result,"v":"20170204","sessionId":"furby","lang":"en"}).json()["result"]
项目:alan    作者:camtaylor    | 项目源码 | 文件源码
def ears():
  # obtain audio from the microphone
  r = sr.Recognizer()
  with sr.Microphone() as source:
    audio = r.listen(source)
  # recognize speech using Google Speech Recognition
  try:
    return r.recognize_google(audio)
  except sr.UnknownValueError:
    return ears() 
  except sr.RequestError as e:
    return "I do not understand; {0}".format(e)
项目:Nancy-VA--MacOS    作者:itz-azhar    | 项目源码 | 文件源码
def listen():
        speak('Listening!')
        with speech_recognition.Microphone() as source:
            recognizer.adjust_for_ambient_noise(source)
            audio = recognizer.listen(source)
        try:
            return recognizer.recognize_google(audio)
        except speech_recognition.UnknownValueError:
            notify(message="Could not understand audio")
        except speech_recognition.RequestError as e:
            notify(message="Connection Problem")
        return ""
项目:audiolearning    作者:jingwang3235    | 项目源码 | 文件源码
def listen_translate():
    while(True):
        # obtain audio from the microphone
        r = sr.Recognizer()
        with sr.Microphone(sample_rate=8000) as source:
            print("Say something!")
    #         print(5),
    #         time.sleep(1)
    #         print(4),
    #         time.sleep(1)
    #         print(3),
    #         time.sleep(1)
    #         print(2),
    #         time.sleep(1)  
    #         print(1),     
    #         time.sleep(1)  
            audio = r.listen(source)#,timeout=5,phrase_time_limit=0.05

    #     r = sr.Recognizer()
    #     with sr.AudioFile('./english.wav') as source:
    #         audio = r.record(source)  # read the entire audio file

        # write audio to a WAV file    ``
with open("microphone-results.wav", "wb") as f:
        f.write(audio.get_wav_data())

    # recognize speech using Sphinx
    try:
        print("Sphinx thinks you said :" + r.recognize_sphinx(audio))
    except sr.UnknownValueError:
        print("Sphinx could not understand audio")
    except sr.RequestError as e:
        print("Sphinx error; {0}".format(e))

```

项目:audiolearning    作者:jingwang3235    | 项目源码 | 文件源码
def translate(r,audio):    
    try:
        s=time.time()
        print(str(len(audiolist))+" Sphinx thinks you said :" + r.recognize_sphinx(audio))
        print time.time()-s
    except sr.UnknownValueError:
        print("Sphinx could not understand audio")
    except sr.RequestError as e:
        print("Sphinx error; {0}".format(e))
项目:audiolearning    作者:jingwang3235    | 项目源码 | 文件源码
def play_audio():

        r = sr.Recognizer()        

        with sr.AudioFile('./english.wav') as source:
            audio = r.record(source)  # read the entire audio file    
            print audio  

        # recognize speech using Sphinx
        try:
            print("Sphinx thinks you said :" + r.recognize_sphinx(audio))
        except sr.UnknownValueError:
            print("Sphinx could not understand audio")
        except sr.RequestError as e:
            print("Sphinx error; {0}".format(e))
项目:Lab-Assistant    作者:SPARC-Auburn    | 项目源码 | 文件源码
def processcommand(self, usermsg, source):
        """
        Processes command from user and deals with wake word detection
        Returns True if Response was when assistant is awoken
        :param usermsg: The message that the user inputted
        :param source: Microphone audio source
        """
        print ("< " + usermsg)
        wake_words = ["hey", "ok", "okay", "yo", "you", "hi"]
        awake = False
        for word in wake_words:
            if self.userCommand == word + " " + self.name:
                awake = True
        if awake:
            self.playsound("start.mp3")
            self.speak("Yes?")
            print ("\tWaiting for command..")
            response_heard = False
            while not response_heard:
                print ("\tThreshold: " + str(self.r.energy_threshold))
                print ("\tWaiting for command...")
                try:
                    audio = self.r.listen(source, timeout=5)
                    print("...")
                    self.playsound("end.mp3")
                    try:
                        self.userCommand = self.r.recognize_google(audio)
                        self.userCommand = self.userCommand.lower()
                        response_heard = True
                        print ("< " + self.userCommand)
                    except sr.UnknownValueError:
                        print("\tUnknown Value from Google, or nothing heard")
                    except sr.RequestError as e:
                        print("\tCould not request results from Google Speech Recognition service; {0}".format(e))
                    except Exception as e:
                        print (str(e))
                except Exception:
                    print ("\tHeard nothing")
                    pass
            return True
        else:
            for word in wake_words:
                if self.userCommand.__contains__(word + " " + self.name):
                    print ("\tGetting command..")
                    self.userCommand = self.userCommand.split(word + " " + self.name + " ")[1]
                    return True
            return False
项目:Face-Recognition-for-Mobile-Robot    作者:gagolucasm    | 项目源码 | 文件源码
def callback(self,data):
        i=0
    rg=spr.Recognizer()
        try:
            frame = self.bridge.imgmsg_to_cv2(data, "bgr8")
            frame = libs.resize(frame, width=600)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            (rects, i, facess) = et.track(gray, i)
            for rect in rects:
                cv2.rectangle(frame, (rect[0], rect[1]), (rect[2], rect[3]), (0, 255, 0), 2)
            if facess != []:
                for face in facess:
                    pred, conf = recognizer.predict(face)
                    if conf < 120:
                        print "Reconozco a Lucas con una confianza de {}".format(conf)
            self.num=self.num+1
            if self.num==10:
                            self.engine.say('Hi ')
                self.engine.say( list(dictid.keys())[list(dictid.values()).index(pred)])
                self.engine.runAndWait()
                with spr.Microphone() as source:
                    rg.adjust_for_ambient_noise(source)
                    print 'Escuchando'
                    audio=rg.listen(source)
                    try:
                        respuesta= rg.recognize_sphinx(audio)
                        print respuesta
                        if respuesta!='no':
                            self.engine.say('OKEY ')
                            self.engine.say('Getting')
                            self.engine.say('new')
                            self.engine.say('data')
                            self.engine.runAndWait()
                    except spr.UnknownValueError:
                        print 'error'

                    else:
                        print "Desconocido"
            cv2.imshow("Tracking", frame)
            cv2.waitKey(1)
        except CvBridgeError as e:
            print(e)
项目:furbymt    作者:starfys    | 项目源码 | 文件源码
def get_command():
    with sr.Microphone() as source:
        print("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)`
        tts_result = r.recognize_google(audio)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
        exit()
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
        exit()
    #Read keys from json
    with open('keys.json','r') as keys_file:
        keys = json.load(keys_file)
    apiai_client_token = keys['apiai_client_token']
    #Create a session
    session = requests.Session()
    session.headers.update({"Authorization":"Bearer {}".format(apiai_client_token),
                        "Content-Type":"application/json; charset=utf-8"})
    #API.ai
    API_BASE_URL="https://api.api.ai/v1/"
    #Make a request
    return session.get(API_BASE_URL+"query", params={"query": tts_result,"v":"20170204","sessionId":"furby","lang":"en"}).json()["result"]

#bee_movie, 
#get_date, 
#get_forecast, 
#get_fortune, 
#prompt_name (for get lucky number), 
#get_stallman, 
#torture, 
#get_time, 
#prompt_question (for wolfram query)
#
# joke
# dad joke
# love
# music