def myCommand(self): #listens for commands r = Recognizer() with Microphone() as source: print('Say something...') r.pause_threshold = 1 r.adjust_for_ambient_noise(source, duration=1) audio = r.listen(source) try: command = r.recognize_google(audio).lower() print('You said: ' + command + '\n') # loop back to continue to listen for commands if unrecognizable speech is received except UnknownValueError: print('....') command = speech.myCommand(self) return command
def record_and_recognise(r: sr.Recognizer) -> str: with sr.Microphone() as source: print("Слушаю..") r.pause_threshold = 1 r.adjust_for_ambient_noise(source, duration=1) audio = r.listen(source, phrase_time_limit=5) try: recognized = r.recognize_google(audio, language='ru-RU') if type(recognized) == list: recognized_string = " ".join(recognized) return recognized_string else: return recognized except sr.UnknownValueError: print( "Unknown sounds came from your mouth. Or perhaps we can't hear you." ) except sr.RequestError as e: print("Sound recognition exception; {0}".format(e))
from logging import getLogger from speech_recognition import Recognizer, Microphone, UnknownValueError, RequestError from dream_journal.config import AUDIO_PAUSE_THRESHOLD logger = getLogger('__main__') r = Recognizer() r.pause_threshold = AUDIO_PAUSE_THRESHOLD micro = Microphone() def adjust_source(): with micro as source: r.adjust_for_ambient_noise(source) def recognize_audio(time=None): logger.debug('listening for for audio..') with micro as source: audio = r.listen(source, phrase_time_limit=time) logger.debug('recorded new audio') try: # recognize speech using Google Speech Recognition return r.recognize_google(audio)