Python speech_recognition.Recognizer() Examples

The following are 30 code examples of speech_recognition.Recognizer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module speech_recognition , or try the search function .
Example #1
Source File: benji.py    From B.E.N.J.I. with MIT License 12 votes vote down vote up
def OnClicked(self):
		"""Recognizes the audio and sends it for display to displayText."""
		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)) 
Example #2
Source File: _P506_PocketSphinx.py    From rpieasy with GNU General Public License v3.0 8 votes vote down vote up
def plugin_init(self,enableplugin=None):
  plugin.PluginProto.plugin_init(self,enableplugin)
  self.decimals[0]=0
  if self.enabled:
   misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Init speech recognition")
   try:
    recog = sr.Recognizer()
    print("--- DEBUG MESSAGES ---")
    self.mic = sr.Microphone()
    print("--- DEBUG MESSAGES END ---")
    print("Available mics: ", self.mic.list_microphone_names())
    self.initialized = True
   except Exception as e:
    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"SpeechRecognition error: "+str(e))
    self.initialized = False
   if self.initialized:
    try:
     with self.mic as source:
      recog.adjust_for_ambient_noise(source, duration=0.5)
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"SpeechRecognition error: "+str(e))
     self.initialized = False
   self.readinprogress = 0
   if self.initialized:
    self.rprocess = recog.listen_in_background(self.mic,self.processor)
    misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"SpeechRecognition start listening")
  else:
   self.plugin_exit() 
Example #3
Source File: client_reverse.py    From Adeept_PiCar-B_oldversion with GNU General Public License v3.0 8 votes vote down vote up
def voice_input():
    global a2t
    r = sr.Recognizer()
    with sr.Microphone() as source:
        #r.adjust_for_ambient_noise(source)
        r.record(source,duration=2)
        print("Say something!")
        audio = r.listen(source)
    try:
        a2t=r.recognize_sphinx(audio,keyword_entries=[('forward',1.0),('backward',1.0),('left',1.0),('right',1.0),('stop',1.0),('find line',0.95),('follow',1),('lights on',1),('lights off',1)])
        print("Sphinx thinks you said " + a2t)
    except sr.UnknownValueError:
        print("Sphinx could not understand audio")
    except sr.RequestError as e:
        print("Sphinx error; {0}".format(e))
    BtnVIN.config(fg=color_text,bg=color_btn)
    return a2t 
Example #4
Source File: benji.py    From B.E.N.J.I. with MIT License 8 votes vote down vote up
def OnClicked(self):
		"""Recognizes the audio and sends it for display to displayText."""
		r = sr.Recognizer()
		with sr.Microphone() as source:
			system('say Hey I am Listening ')
			
			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)) 
Example #5
Source File: speech_to_text.py    From robovision with GNU General Public License v3.0 8 votes vote down vote up
def main():

    r = sr.Recognizer()

    with sr.Microphone() as source:
        print ('say something')
        audio = r.listen(source)
        print ('done')
    try:
        text = r.recognize_google(audio)
        print('Neo said:\n' + text)
    except Exception as e:
        print (e) 
Example #6
Source File: ted.py    From You-are-Pythonista with GNU General Public License v3.0 8 votes vote down vote up
def yuyin():
    logging.basicConfig(level=logging.INFO)
    wav_num = 0
    while True:
        r = sr.Recognizer()
        #启用麦克风
        mic = sr.Microphone()
        logging.info('录音中...')
        with mic as source:
            #降噪
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
        with open(f"00{wav_num}.wav", "wb") as f:
            #将麦克风录到的声音保存为wav文件
            f.write(audio.get_wav_data(convert_rate=16000))
        logging.info('录音结束,识别中...')
        target = audio_baidu(f"00{wav_num}.wav")
        wav_num += 1
        if target == -1:
            continue 
Example #7
Source File: stt.py    From FunUtils with MIT License 7 votes vote down vote up
def __init__(self, recognition_api="google", language="en-us"):
        self._recognizer = sr.Recognizer()
        # below energy_threshold is considered silence, above speech
        self._recognizer.energy_threshold = 500
        self._recognition_api = recognition_api
        self._recognition_method = None
        self._determine_recognition_method()
        self._microphone = sr.Microphone()
        self._language = language

    # public methods 
Example #8
Source File: Audio.py    From jarvis with MIT License 7 votes vote down vote up
def getAudio():
    r = sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        os.system("say '请问我能为您做些什么?'")
        print("请问我能为您做些什么?")
        audio = r.listen(source)

        with open(file_location + "input.wav", "wb") as f:
            f.write(audio.get_wav_data(convert_rate=16000))
            return True

    return False


# 识别本地文件 
Example #9
Source File: gen_sentence_with_emoticons.py    From Real-Time-Facial-Expression-Recognition-with-DeepLearning with MIT License 7 votes vote down vote up
def speechRecognition():
    # obtain audio from the microphone 
    print("Press 'y' to start~")
    inputdata = input()
    if inputdata == 'y':
        inputdata = 0
        r = sr.Recognizer()
        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)`
            recSuccess = 1
            recContent = r.recognize_google(audio)
            print("Speech Recognition thinks you said " + recContent)#,language="cmn-Hant-TW")
            return recContent
        except sr.UnknownValueError:
            print("Could not understand audio")
        except sr.RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e)) 
Example #10
Source File: client.py    From Adeept_PiCar-B_oldversion with GNU General Public License v3.0 7 votes vote down vote up
def voice_input():
    global a2t
    r = sr.Recognizer()
    with sr.Microphone() as source:
        #r.adjust_for_ambient_noise(source)
        r.record(source,duration=2)
        print("Say something!")
        audio = r.listen(source)
    try:
        a2t=r.recognize_sphinx(audio,keyword_entries=[('forward',1.0),('backward',1.0),('left',1.0),('right',1.0),('stop',1.0),('find line',0.95),('follow',1),('lights on',1),('lights off',1)])
        print("Sphinx thinks you said " + a2t)
    except sr.UnknownValueError:
        print("Sphinx could not understand audio")
    except sr.RequestError as e:
        print("Sphinx error; {0}".format(e))
    BtnVIN.config(fg=color_text,bg=color_btn)
    return a2t 
Example #11
Source File: ear.py    From robovision with GNU General Public License v3.0 7 votes vote down vote up
def get_audio(self):
        """
        Get audio from the microphone.

        The SpeechRecognition package is used to automatically stop listening
        when the user stops speaking.

        Function returns the raw binary audio string (PCM)
        """
        l = sr.Microphone.list_microphone_names()
        log.debug(l)

        r = sr.Recognizer()

        di = l.index("default")

        with sr.Microphone(device_index=di) as source:
            # with sr.Microphone() as source:
            log.debug("listening for audio from microphone")
            # r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
            log.debug("listening done")

        # convert audio to raw_data (PCM)
        raw_audio = audio.get_raw_data()

        # recognize speech using Google Speech Recognition
        text = r.recognize_google(audio)

        return text 
Example #12
Source File: speechtotext.py    From GROOT with Mozilla Public License 2.0 7 votes vote down vote up
def listen():
    #obtain audio from the microphone
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Say something!")
        r.adjust_for_ambient_noise(source, duration=0.5)
        audio = r.listen(source)
    #recognize speech using Google Speech Recognition
    try:
        var=r.recognize_google(audio)
    except sr.UnknownValueError:
        var="Groot could not understand audio"
    except sr.RequestError:
        var=" Looks like, there is some problem with Google Speech Recognition"

    return var

    #will show all posible text from audio
    #print(r.recognize_google(audio, show_all=True)) 
Example #13
Source File: benji.py    From B.E.N.J.I. with MIT License 7 votes vote down vote up
def OnClicked(self):
		"""Recognizes the audio and sends it for display to displayText."""
		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)) 
Example #14
Source File: voice_recognition.py    From speech_recognition_chatbot with MIT License 7 votes vote down vote up
def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.

    Returns a dictionary with three keys:
    "success": a boolean indicating whether or not the API request was
               successful
    "error":   `None` if no error occured, otherwise a string containing
               an error message if the API could not be reached or
               speech was unrecognizable
    "transcription": `None` if speech could not be transcribed,
               otherwise a string containing the transcribed text
    """
    # check that recognizer and microphone arguments are appropriate type
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    # adjust the recognizer sensitivity to ambient noise and record audio
    # from the microphone
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source) # #  analyze the audio source for 1 second
        audio = recognizer.listen(source)

    # set up the response object
    response = {
        "success": True,
        "error": None,
        "transcription": None
    }

    # try recognizing the speech in the recording
    # if a RequestError or UnknownValueError exception is caught,
    #   update the response object accordingly
    try:
        response["transcription"] = recognizer.recognize_google(audio)
    except sr.RequestError:
        # API was unreachable or unresponsive
        response["success"] = False
        response["error"] = "API unavailable/unresponsive"
    except sr.UnknownValueError:
        # speech was unintelligible
        response["error"] = "Unable to recognize speech"

    return response

#%% 
Example #15
Source File: smartmirror.py    From Smart-Mirror with MIT License 6 votes vote down vote up
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)) 
Example #16
Source File: smartmirror-bing.py    From Smart-Mirror with MIT License 6 votes vote down vote up
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)) 
Example #17
Source File: speech_to_text.py    From aipa with MIT License 6 votes vote down vote up
def main():
	# obtain audio from the microphone
	while True:
	    r = sr.Recognizer()
	    with sr.Microphone() as source:
		print("listening...")		
		audio = r.listen(source)
		print("sa")
	    # 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("as")		
		command = r.recognize_google(audio)
		print("You said: " + command)
		
	    #exception handling
	    except sr.UnknownValueError:
		print("I could not understand audio")
	    except sr.RequestError as e:
		print("Could not request results from Speech Recognition service; {0}".format(e)) 
Example #18
Source File: neuraldata.py    From SaltwashAR with GNU General Public License v3.0 6 votes vote down vote up
def create_data(text_to_speech):

    # ask user to say the word Yes or No
    text_to_speech("Say the word Yes or No")

    # save spoken word as wav data
    recognizer = sr.Recognizer()   
    
    with sr.Microphone() as source:
        print "listening..."
        audio = recognizer.listen(source)

    with open(WAV_FILE, "wb") as f:
        f.write(audio.get_wav_data())

    # get target data (and bail out if not Yes or No)
    target = _get_target(recognizer, audio)
    if target == None: return (None,None)

    # get input data
    input = _get_input()

    return (input,target)

# save input and target data 
Example #19
Source File: VoiceEngineServer.py    From rpi-course with MIT License 6 votes vote down vote up
def Listener():
    global broadcastMSG
    while(1):
        # obtain audio from the microphone
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("Say something!")
            audio = r.listen(source)

        # recognize speech using Google Speech Recognition
        try:
            recognizedCommand = r.recognize_google(audio);
            print("Google Speech Recognition thinks you said " + recognizedCommand)
            if(recognizedCommand == "start"):
                broadcastMSG = "start"
            elif(recognizedCommand == "stop"):
                broadcastMSG = "stop"
            elif(recognizedCommand == "clockwise"):
                broadcastMSG = "clockwise"
            elif(recognizedCommand == "counter clockwise"):
                broadcastMSG = "counter clockwise"
        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)) 
Example #20
Source File: rebreakcaptcha.py    From rebreakcaptcha with MIT License 6 votes vote down vote up
def speech_to_text(self, audio_source):
        # Initialize a new recognizer with the audio in memory as source
        recognizer = sr.Recognizer()
        with sr.AudioFile(audio_source) as source:
            audio = recognizer.record(source) # read the entire audio file

        audio_output = ""
        # recognize speech using Google Speech Recognition
        try:
            audio_output = recognizer.recognize_google(audio)
            print("[{0}] Google Speech Recognition: ".format(self.current_iteration) + audio_output)
            # Check if we got harder audio captcha
            if any(character.isalpha() for character in audio_output):
                # Use Houndify to detect the harder audio captcha
                print("[{0}] Fallback to Houndify!".format(self.current_iteration))
                audio_output = self.string_to_digits(recognizer.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY))
                print("[{0}] Houndify: ".format(self.current_iteration) + audio_output)
        except sr.UnknownValueError:
            print("[{0}] Google Speech Recognition could not understand audio".format(self.current_iteration))
        except sr.RequestError as e:
            print("[{0}] Could not request results from Google Speech Recognition service; {1}".format(self.current_iteration).format(e))
            
        return audio_output 
Example #21
Source File: brain.py    From Jarvis with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self):
		self.status = 1
		self.version = "0.0.7"
		self.array = []
		self.numbers = []
		self.path = os.path.abspath(os.path.dirname(sys.argv[0]))
		try:
			self.con = sqlite3.connect(self.path + "config/Jarbas.db")
		except:
			g = self.path.split("core")
			dbpath = g[0] + "/config/Jarbas.db"
			self.con = sqlite3.connect(dbpath)
		self.serialport = self.arduino_check()
		self.rec = sr.Recognizer()
		self.engine = pyttsx.init()
		self.rate = self.engine.getProperty('rate')
		self.engine.setProperty('rate', self.rate-60)
		self.voices = self.engine.getProperty('voices')
		self.engine.setProperty('voice',self.voices[16].id) #1,9,10,11,16,22,25
		self.ser = serial.Serial()
		self.ser.port = self.serialport
		self.ser.baudrate = 9600 
Example #22
Source File: alexa_audio.py    From python-alexa-voice-service with MIT License 5 votes vote down vote up
def get_audio(self, timeout=None):
        """ Get audio from the microphone. The SpeechRecognition package is used to automatically stop listening
            when the user stops speaking. A timeout can also be specified. If the timeout is reached, the function
            returns None.

            This function can also be used for debugging purposes to read an example audio file.

        :param timeout: timeout in seconds, when to give up if the user did not speak.
        :return: the raw binary audio string (PCM)
        """
        # Create a speech recognizer
        r = speech_recognition.Recognizer()
        # Open the microphone (and release is when done using "with")
        with speech_recognition.Microphone() as source:
            if timeout is None:
                # Prompt user to say something
                print("You can start talking now...")
                # TODO add sounds to prompt the user to do something, rather than text
                # Record audio until the user stops talking
                audio = r.listen(source)
            else:
                print("Start talking now, you have %d seconds" % timeout)
                # TODO add sounds to prompt the user to do something, rather than text
                try:
                    audio = r.listen(source, timeout=timeout)
                except speech_recognition.WaitTimeoutError:
                    return None
        # Convert audio to raw_data (PCM)
        raw_audio = audio.get_raw_data()

        # Rather than recording, read a pre-recorded example (for testing)
        # with open('files/example_get_time.pcm', 'rb') as f:
        #     raw_audio = f.read()
        return raw_audio 
Example #23
Source File: google_stt.py    From selene-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        super(GoogleSTTEndpoint, self).__init__()
        self.google_stt_key = self.config['GOOGLE_STT_KEY']
        self.recognizer = Recognizer()
        self.account = None
        self.account_shares_data = False 
Example #24
Source File: mixingdesk.py    From SaltwashAR with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, text_to_speech, speech_to_text):
        Feature.__init__(self)
        Speaking.__init__(self, text_to_speech)
        self.speech_to_text = speech_to_text
        self.recognizer = sr.Recognizer()
        pygame.mixer.init(frequency=8000) 
Example #25
Source File: speechtotext.py    From SaltwashAR with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.recognizer = sr.Recognizer()
 
    # convert speech to text 
Example #26
Source File: voice.py    From Hindi-DateTime-Parser with MIT License 5 votes vote down vote up
def voice_input():
    
    #!/usr/bin/env python3
    # Requires PyAudio and PySpeech.
    
	#sudo apt-get install portaudio19-dev

	#pip install --allow-unverified=pyaudio pyaudio 
	
	#requires internet too

    import speech_recognition as sr
     
    # Record Audio
    r = sr.Recognizer()
    m = sr.Microphone()

    with sr.Microphone() as source:
        print("Say something!")
        audio = r.listen(source)
     
    # Speech recognition 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)`
        a=r.recognize_google(audio)
        #time.sleep(1)
        
        #stop_listening = r.listen_in_background(m, callback)
       
    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))

    print (a)
    return a 
Example #27
Source File: speech_recognition.py    From macaw with MIT License 5 votes vote down vote up
def __init__(self, params):
        super().__init__(params)
        self.asr = sr.Recognizer() 
Example #28
Source File: speech_to_text.py    From respeaker_ros with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        # format of input audio data
        self.sample_rate = rospy.get_param("~sample_rate", 16000)
        self.sample_width = rospy.get_param("~sample_width", 2L)
        # language of STT service
        self.language = rospy.get_param("~language", "ja-JP")
        # ignore voice input while the robot is speaking
        self.self_cancellation = rospy.get_param("~self_cancellation", True)
        # time to assume as SPEAKING after tts service is finished
        self.tts_tolerance = rospy.Duration.from_sec(
            rospy.get_param("~tts_tolerance", 1.0))

        self.recognizer = SR.Recognizer()

        self.tts_action = None
        self.last_tts = None
        self.is_canceling = False
        if self.self_cancellation:
            self.tts_action = actionlib.SimpleActionClient(
                "sound_play", SoundRequestAction)
            if self.tts_action.wait_for_server(rospy.Duration(5.0)):
                self.tts_timer = rospy.Timer(rospy.Duration(0.1), self.tts_timer_cb)
            else:
                rospy.logerr("action '%s' is not initialized." % rospy.remap_name("sound_play"))
                self.tts_action = None

        self.pub_speech = rospy.Publisher(
            "speech_to_text", SpeechRecognitionCandidates, queue_size=1)
        self.sub_audio = rospy.Subscriber("audio", AudioData, self.audio_cb) 
Example #29
Source File: youtube_helpers.py    From KTSpeechCrawler with MIT License 5 votes vote down vote up
def _get_transcript_google_web_asr(t):
    import tempfile
    try:
        with tempfile.NamedTemporaryFile(suffix=".wav") as f:
            extract_audio_part_segment(t["video_file"], t["ts_start"], t["ts_end"], f.name)

            r = sr.Recognizer()
            with sr.AudioFile(f.name) as source:
                audio = r.record(source)

                return r.recognize_google(audio)
    except Exception as e:
        print(e)
        return None 
Example #30
Source File: client.py    From honk with MIT License 5 votes vote down vote up
def __init__(self, server_endpoint, qa_endpoint, goose_window, watson_api=None):
        self.watson_api = watson_api
        self.server_endpoint = server_endpoint
        self.qa_endpoint = qa_endpoint
        self.chunk_size = 16000
        self.recognizer = sr.Recognizer()
        self.goose_window = goose_window
        if not watson_api:
            self._tts = pyttsx3.init()
            self._tts.connect("started-word", self._make_tts_cb())