Advertisement
Guest User

Untitled

a guest
Aug 27th, 2017
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 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 Wit.ai
  33. WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
  34. try:
  35. print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
  36. except sr.UnknownValueError:
  37. print("Wit.ai could not understand audio")
  38. except sr.RequestError as e:
  39. print("Could not request results from Wit.ai service; {0}".format(e))
  40.  
  41. # recognize speech using Microsoft Bing Voice Recognition
  42. BING_KEY = "INSERT BING API KEY HERE" # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings
  43. try:
  44. print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
  45. except sr.UnknownValueError:
  46. print("Microsoft Bing Voice Recognition could not understand audio")
  47. except sr.RequestError as e:
  48. print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
  49.  
  50. # recognize speech using api.ai
  51. API_AI_CLIENT_ACCESS_TOKEN = "INSERT API.AI API KEY HERE" # api.ai keys are 32-character lowercase hexadecimal strings
  52. try:
  53. print("api.ai thinks you said " + r.recognize_api(audio, client_access_token=API_AI_CLIENT_ACCESS_TOKEN))
  54. except sr.UnknownValueError:
  55. print("api.ai could not understand audio")
  56. except sr.RequestError as e:
  57. print("Could not request results from api.ai service; {0}".format(e))
  58.  
  59. # recognize speech using IBM Speech to Text
  60. IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  61. IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
  62. try:
  63. print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
  64. except sr.UnknownValueError:
  65. print("IBM Speech to Text could not understand audio")
  66. except sr.RequestError as e:
  67. print("Could not request results from IBM Speech to Text service; {0}".format(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement