microphone_recognition.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. print("Say something!")
  8. audio = r.listen(source)
  9. # recognize speech using Sphinx
  10. try:
  11. print("Sphinx thinks you said " + r.recognize_sphinx(audio))
  12. except sr.UnknownValueError:
  13. print("Sphinx could not understand audio")
  14. except sr.RequestError as e:
  15. print("Sphinx error; {0}".format(e))
  16. # recognize speech using Google Speech Recognition
  17. try:
  18. # for testing purposes, we're just using the default API key
  19. # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
  20. # instead of `r.recognize_google(audio)`
  21. print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
  22. except sr.UnknownValueError:
  23. print("Google Speech Recognition could not understand audio")
  24. except sr.RequestError as e:
  25. print("Could not request results from Google Speech Recognition service; {0}".format(e))
  26. # recognize speech using Wit.ai
  27. WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
  28. try:
  29. print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
  30. except sr.UnknownValueError:
  31. print("Wit.ai could not understand audio")
  32. except sr.RequestError as e:
  33. print("Could not request results from Wit.ai service; {0}".format(e))
  34. # recognize speech using Microsoft Bing Voice Recognition
  35. BING_KEY = "INSERT BING API KEY HERE" # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings
  36. try:
  37. print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
  38. except sr.UnknownValueError:
  39. print("Microsoft Bing Voice Recognition could not understand audio")
  40. except sr.RequestError as e:
  41. print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
  42. # recognize speech using Houndify
  43. HOUNDIFY_CLIENT_ID = "INSERT HOUNDIFY CLIENT ID HERE" # Houndify client IDs are Base64-encoded strings
  44. HOUNDIFY_CLIENT_KEY = "INSERT HOUNDIFY CLIENT KEY HERE" # Houndify client keys are Base64-encoded strings
  45. try:
  46. print("Houndify thinks you said " + r.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY))
  47. except sr.UnknownValueError:
  48. print("Houndify could not understand audio")
  49. except sr.RequestError as e:
  50. print("Could not request results from Houndify service; {0}".format(e))
  51. # recognize speech using IBM Speech to Text
  52. IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  53. IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
  54. try:
  55. print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
  56. except sr.UnknownValueError:
  57. print("IBM Speech to Text could not understand audio")
  58. except sr.RequestError as e:
  59. print("Could not request results from IBM Speech to Text service; {0}".format(e))