calibrate_energy_threshold.py 968 B

1234567891011121314151617181920212223
  1. #!/usr/bin/env python3
  2. # NOTE: this example requires PyAudio because it uses the Microphone class
  3. import speech_recognition as sr
  4. # obtain audio from the microphone
  5. r = sr.Recognizer()
  6. with sr.Microphone() as source:
  7. r.adjust_for_ambient_noise(source) # listen for 1 second to calibrate the energy threshold for ambient noise levels
  8. print("Say something!")
  9. audio = r.listen(source)
  10. # recognize speech using Google Speech Recognition
  11. try:
  12. # for testing purposes, we're just using the default API key
  13. # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
  14. # instead of `r.recognize_google(audio)`
  15. print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
  16. except sr.UnknownValueError:
  17. print("Google Speech Recognition could not understand audio")
  18. except sr.RequestError as e:
  19. print("Could not request results from Google Speech Recognition service; {0}".format(e))