Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import speech_recognition
  2.  
  3.  
  4. def listen(rec, mic):
  5. with mic as source:
  6. print('adjusting mic for ambient noise..')
  7. rec.adjust_for_ambient_noise(source)
  8. print('listening..')
  9. # timeout is not max record time but max time it will wait for audio to start
  10. audio = rec.listen(source, timeout=5)
  11. try:
  12. print('sending audio to google..')
  13. outputString = rec.recognize_google(audio).lower()
  14. except speech_recognition.UnknownValueError:
  15. print('google was not able to transcribe the audio')
  16. return None
  17. except speech_recognition.RequestError:
  18. print('google API unreachable')
  19. return None
  20. except speech_recognition.WaitTimeoutError:
  21. print('timout expired while waiting for audio command to start')
  22. return None
  23. print('transcription '' + outputString + ''')
  24. return outputString
  25.  
  26.  
  27. rec = sr.Recognizer()
  28. mic = sr.Microphone()
  29. outputString = listen(rec, mic)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement