Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import speech_recognition
- def listen(rec, mic):
- with mic as source:
- print('adjusting mic for ambient noise..')
- rec.adjust_for_ambient_noise(source)
- print('listening..')
- # timeout is not max record time but max time it will wait for audio to start
- audio = rec.listen(source, timeout=5)
- try:
- print('sending audio to google..')
- outputString = rec.recognize_google(audio).lower()
- except speech_recognition.UnknownValueError:
- print('google was not able to transcribe the audio')
- return None
- except speech_recognition.RequestError:
- print('google API unreachable')
- return None
- except speech_recognition.WaitTimeoutError:
- print('timout expired while waiting for audio command to start')
- return None
- print('transcription '' + outputString + ''')
- return outputString
- rec = sr.Recognizer()
- mic = sr.Microphone()
- outputString = listen(rec, mic)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement