extended_results.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. from pprint import pprint
  3. import speech_recognition as sr
  4. # obtain path to "english.wav" in the same folder as this script
  5. from os import path
  6. AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "english.wav")
  7. #AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
  8. #AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")
  9. # use the audio file as the audio source
  10. r = sr.Recognizer()
  11. with sr.AudioFile(AUDIO_FILE) as source:
  12. audio = r.record(source) # read the entire audio file
  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. # recognize speech using Google Speech Recognition
  21. try:
  22. # for testing purposes, we're just using the default API key
  23. # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY", show_all=True)`
  24. # instead of `r.recognize_google(audio, show_all=True)`
  25. print("Google Speech Recognition results:")
  26. pprint(r.recognize_google(audio, show_all=True)) # pretty-print the recognition result
  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. # recognize speech using Wit.ai
  32. WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
  33. try:
  34. print("Wit.ai recognition results:")
  35. pprint(r.recognize_wit(audio, key=WIT_AI_KEY, show_all=True)) # pretty-print the recognition result
  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. # recognize speech using Microsoft Bing Voice Recognition
  41. BING_KEY = "INSERT BING API KEY HERE" # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings
  42. try:
  43. print("Bing recognition results:")
  44. pprint(r.recognize_bing(audio, key=BING_KEY, show_all=True))
  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. # recognize speech using Houndify
  50. HOUNDIFY_CLIENT_ID = "INSERT HOUNDIFY CLIENT ID HERE" # Houndify client IDs are Base64-encoded strings
  51. HOUNDIFY_CLIENT_KEY = "INSERT HOUNDIFY CLIENT KEY HERE" # Houndify client keys are Base64-encoded strings
  52. try:
  53. print("Houndify recognition results:")
  54. pprint(r.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY, show_all=True))
  55. except sr.UnknownValueError:
  56. print("Houndify could not understand audio")
  57. except sr.RequestError as e:
  58. print("Could not request results from Houndify service; {0}".format(e))
  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 results:")
  64. pprint(r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD, show_all=True)) # pretty-print the recognition result
  65. except sr.UnknownValueError:
  66. print("IBM Speech to Text could not understand audio")
  67. except sr.RequestError as e:
  68. print("Could not request results from IBM Speech to Text service; {0}".format(e))