Python speech_recognition.Microphone() Examples

The following are 30 code examples of speech_recognition.Microphone(). 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: 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 #4
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 #5
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 #6
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 #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: speechtotext.py    From SaltwashAR with GNU General Public License v3.0 7 votes vote down vote up
def convert(self):

        with sr.Microphone() as source:
            print "listening..."
            audio = self.recognizer.listen(source)

        text = None

        try:
            text = self.recognizer.recognize_google(audio)
            print text
        except sr.UnknownValueError:
            print "Google Speech Recognition could not understand audio"
        except sr.RequestError:
            print "Could not request results from Google Speech Recognition service"

        return text 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
Source File: mixingdesk.py    From SaltwashAR with GNU General Public License v3.0 6 votes vote down vote up
def _thread(self, args):
    
        # mixing desk asks for vocal
        self._text_to_speech("Sing the vocal now...")
    
        # user sings
        with sr.Microphone() as source:
            print "listening..."
            wav_data = self.recognizer.listen(source).get_wav_data()

        # check whether to stop thread
        if self.is_stop: return
      
        # mixing desk asks for instruments
        self._text_to_speech("What instruments do you want?")

        # user gives instruments
        instruments = self._speech_to_text()

        # mixing desk gets mixing...
        pygame.mixer.set_num_channels(3)
        
        vocals = pygame.mixer.Channel(0)
        vocals.set_volume(0.8)       
        vocals.play(pygame.mixer.Sound(wav_data))

        if self.GUITAR in instruments:
            guitar = pygame.mixer.Channel(1)
            guitar.set_volume(0.3)
            guitar.play(pygame.mixer.Sound(self.GUITAR_FILENAME))

        if self.DRUMS in instruments:
            drums = pygame.mixer.Channel(2)
            drums.set_volume(0.6)
            drums.play(pygame.mixer.Sound(self.DRUMS_FILENAME))

        while vocals.get_busy():
            continue

        if self.GUITAR in instruments:
            guitar.stop()

        if self.DRUMS in instruments:
            drums.stop()

        sleep(4) 
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: 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 #20
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 #21
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 #22
Source File: recognize_speech.py    From robovision with GNU General Public License v3.0 5 votes vote down vote up
def get_audio():
    """
    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()
    print (l)
    
    r = sr.Recognizer()

    di = l.index("default")
    print ("di", di)

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

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

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

    return text 
Example #23
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 #24
Source File: brain.py    From Jarvis with GNU General Public License v3.0 5 votes vote down vote up
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.") 
Example #25
Source File: Utils.py    From kalliope with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, audio_file=None):
        """
        Thread used to caught n audio from the microphone and pass it to a callback method
        """
        super(SpeechRecognition, self).__init__()
        self.recognizer = sr.Recognizer()
        self.microphone = sr.Microphone()
        self.callback = None
        self.stop_thread = None
        self.kill_yourself = False
        self.audio_stream = None

        # get global configuration
        sl = SettingLoader()
        self.settings = sl.settings

        if audio_file is None:
            # audio file not set, we need to capture a sample from the microphone
            with self.microphone as source:
                if self.settings.options.adjust_for_ambient_noise_second > 0:
                    # threshold is calculated from capturing ambient sound
                    logger.debug("[SpeechRecognition] threshold calculated by "
                                 "capturing ambient noise during %s seconds" %
                                 self.settings.options.adjust_for_ambient_noise_second)
                    Utils.print_info("[SpeechRecognition] capturing ambient sound during %s seconds" %
                                     self.settings.options.adjust_for_ambient_noise_second)
                    self.recognizer.adjust_for_ambient_noise(source,
                                                             duration=self.settings.
                                                             options.adjust_for_ambient_noise_second)
                else:
                    # threshold is defined manually
                    logger.debug("[SpeechRecognition] threshold defined by settings: %s" %
                                 self.settings.options.energy_threshold)
                    self.recognizer.energy_threshold = self.settings.options.energy_threshold

                Utils.print_info("[SpeechRecognition] Threshold set to: %s" % self.recognizer.energy_threshold)
        else:
            # audio file provided
            with sr.AudioFile(audio_file) as source:
                self.audio_stream = self.recognizer.record(source)  # read the entire audio file 
Example #26
Source File: client.py    From honk with MIT License 5 votes vote down vote up
def _recognize_speech(self):
        self._stop_listening()
        try:
            with sr.Microphone() as source:
                audio = self.recognizer.listen(source)
            return self.recognizer.recognize_google(audio)
        except sr.UnknownValueError:
            return None
        finally:
            self._start_listening() 
Example #27
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 #28
Source File: friday.py    From Friday with MIT License 4 votes vote down vote up
def __init__(self):
        self.ai = ai_interface.API(apiai.ApiAI(settings['CLIENT_ACCESS_TOKEN'],
                                               settings['SUBSCRIPTION_KEY']))
        self.debugging = settings['debugging']
        self.spoken_language = settings['spoken language']
        self.input_system = settings['input system']  # google, local, text
        self.output_system = settings['output system']  # both, audio, text
        self.speech_file_location = settings['speech file location']  # .
        self.speech_file_name = settings['speech file name']  # audio response
        self.speak = settings['speak']  # True
        # The question that the assistant hears
        self.question = None
        # The chosen, spoken response given to the user.
        self.response = None
        # Whether Friday is active
        self.is_active = True
        # What type of question is being asked.
        self.request_type = None
        if settings['input system'] != 'text':
            self.recognizer = sr.Recognizer()
            self.microphone = sr.Microphone(device_index=settings['DEVICE'],
                                            sample_rate=settings['RATE'],
                                            chunk_size=settings['CHUNK'])

            if settings['input_system'] == 'google':
                with self.microphone as source:
                    if settings['debugging']:
                        click.echo("Adjusting to ambient noise.")
                        # we only need to calibrate once, before we start listening
                    self.recognizer.adjust_for_ambient_noise(source)

        # Build the manager
        self.manager = PluginManager()
        # Tell it the default place(s) where to find plugins
        self.manager.setPluginPlaces(settings["plugin folder"])
        # Load all plugins
        self.manager.locatePlugins()
        self.manager.loadPlugins()

        self.plugins = {}
        # Activate all loaded plugins
        for plugin in self.manager.getAllPlugins():
            self.plugins[plugin.name] = plugin.plugin_object 
Example #29
Source File: boot.py    From stephanie-va with MIT License 4 votes vote down vote up
def initiate(self):
        print("Stephanie is on and loading, wait for the beep sound to give your command.")
        if self.c.config.getboolean("APPLICATION", "update_check"):
            self.updater.check_for_update()
        self.status = True
        if self.c.config.getboolean("SYSTEM", "wake_up_engine"):
            self.status = False
            self.active = False
        if self.c.config.getboolean("SYSTEM", "always_on_engine"):
            self.status = False
            self.active = False
        r = sr.Recognizer()
        act = Activity(sr, r, self.events)
        assistant = VirtualAssistant(sr, r, self.events)
        if self.c.config.getboolean("SYSTEM", "wake_up_engine"):
            while not self.active:
                with sr.Microphone() as source:
                    self.active = act.check(source)
                    self.status = self.active
                    self.events.sleep_status = not self.status
                    if self.active:
                        self.speaker.speak("How may I help you?")
                        while self.status:
                            with sr.Microphone() as source:
                                assistant.main(source)
                                if self.events.active_status:
                                    self.status = False
                                    self.active = True
                                elif self.events.sleep_status:
                                    self.status = False
                                    self.active = False
        elif self.c.config.getboolean("SYSTEM", "always_on_engine"):
            while not self.active:
                with sr.Microphone() as source:
                    self.active = act.check_always_on(source)
                    self.status = self.active
                    if self.active:
                        while self.status:
                            with sr.Microphone() as source:
                                assistant.main(source)
                                self.status = False
                                self.active = False
                                if self.events.active_status:
                                    self.status = False
                                    self.active = True
        else:
            self.speaker.speak("How may I help you?")
            while self.status:
                with sr.Microphone() as source:
                    assistant.main(source)
                    if self.events.active_status:
                        self.status = False 
Example #30
Source File: guessing_game.py    From python-speech-recognition with MIT License 4 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)
        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"
    except sr.UnknownValueError:
        # speech was unintelligible
        response["error"] = "Unable to recognize speech"

    return response