Guest User

Untitled

a guest
Oct 15th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # NOTE: this example requires PyAudio because it uses the Microphone class
  4.  
  5. import speech_recognition as sr
  6.  
  7. # obtain audio from the microphone
  8. r = sr.Recognizer()
  9. with sr.Microphone() as source:
  10. print("Say something!")
  11. audio = r.listen(source)
  12.  
  13. # recognize speech using Sphinx
  14. try:
  15. print("Sphinx thinks you said " + r.recognize_sphinx(audio))
  16. except sr.UnknownValueError:
  17. print("Sphinx could not understand audio")
  18. except sr.RequestError as e:
  19. print("Sphinx error; {0}".format(e))
  20.  
  21. # recognize speech using Google Speech Recognition
  22. try:
  23. # for testing purposes, we're just using the default API key
  24. # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
  25. # instead of `r.recognize_google(audio)`
  26. print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
  27. except sr.UnknownValueError:
  28. print("Google Speech Recognition could not understand audio")
  29. except sr.RequestError as e:
  30. print("Could not request results from Google Speech Recognition service; {0}".format(e))
  31.  
  32. # recognize speech using Google Cloud Speech
  33. GOOGLE_CLOUD_SPEECH_CREDENTIALS = r"""INSERT THE CONTENTS OF THE GOOGLE CLOUD SPEECH JSON CREDENTIALS FILE HERE"""
  34. try:
  35. print("Google Cloud Speech thinks you said " + r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS))
  36. except sr.UnknownValueError:
  37. print("Google Cloud Speech could not understand audio")
  38. except sr.RequestError as e:
  39. print("Could not request results from Google Cloud Speech service; {0}".format(e))
  40.  
  41. # recognize speech using Wit.ai
  42. WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
  43. try:
  44. print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
  45. except sr.UnknownValueError:
  46. print("Wit.ai could not understand audio")
  47. except sr.RequestError as e:
  48. print("Could not request results from Wit.ai service; {0}".format(e))
  49.  
  50. # recognize speech using Microsoft Bing Voice Recognition
  51. BING_KEY = "INSERT BING API KEY HERE" # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings
  52. try:
  53. print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
  54. except sr.UnknownValueError:
  55. print("Microsoft Bing Voice Recognition could not understand audio")
  56. except sr.RequestError as e:
  57. print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
  58.  
  59. # recognize speech using Houndify
  60. HOUNDIFY_CLIENT_ID = "INSERT HOUNDIFY CLIENT ID HERE" # Houndify client IDs are Base64-encoded strings
  61. HOUNDIFY_CLIENT_KEY = "INSERT HOUNDIFY CLIENT KEY HERE" # Houndify client keys are Base64-encoded strings
  62. try:
  63. print("Houndify thinks you said " + r.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY))
  64. except sr.UnknownValueError:
  65. print("Houndify could not understand audio")
  66. except sr.RequestError as e:
  67. print("Could not request results from Houndify service; {0}".format(e))
  68.  
  69. # recognize speech using IBM Speech to Text
  70. IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  71. IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
  72. try:
  73. print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
  74. except sr.UnknownValueError:
  75. print("IBM Speech to Text could not understand audio")
  76. except sr.RequestError as e:
  77. print("Could not request results from IBM Speech to Text service; {0}".format(e))
Add Comment
Please, Sign In to add comment