audio_transcribe.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. import speech_recognition as sr
  3. # obtain path to "english.wav" in the same folder as this script
  4. from os import path
  5. AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "english.wav")
  6. #AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
  7. #AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")
  8. # use the audio file as the audio source
  9. r = sr.Recognizer()
  10. with sr.AudioFile(AUDIO_FILE) as source:
  11. audio = r.record(source) # read the entire audio file
  12. # recognize speech using Sphinx
  13. try:
  14. print("Sphinx thinks you said " + r.recognize_sphinx(audio))
  15. except sr.UnknownValueError:
  16. print("Sphinx could not understand audio")
  17. except sr.RequestError as e:
  18. print("Sphinx error; {0}".format(e))
  19. # recognize speech using Google Speech Recognition
  20. try:
  21. # for testing purposes, we're just using the default API key
  22. # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
  23. # instead of `r.recognize_google(audio)`
  24. print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
  25. except sr.UnknownValueError:
  26. print("Google Speech Recognition could not understand audio")
  27. except sr.RequestError as e:
  28. print("Could not request results from Google Speech Recognition service; {0}".format(e))
  29. # recognize speech using Wit.ai
  30. WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
  31. try:
  32. print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
  33. except sr.UnknownValueError:
  34. print("Wit.ai could not understand audio")
  35. except sr.RequestError as e:
  36. print("Could not request results from Wit.ai service; {0}".format(e))
  37. # recognize speech using Microsoft Bing Voice Recognition
  38. BING_KEY = "INSERT BING API KEY HERE" # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings
  39. try:
  40. print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
  41. except sr.UnknownValueError:
  42. print("Microsoft Bing Voice Recognition could not understand audio")
  43. except sr.RequestError as e:
  44. print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
  45. # recognize speech using Houndify
  46. HOUNDIFY_CLIENT_ID = "INSERT HOUNDIFY CLIENT ID HERE" # Houndify client IDs are Base64-encoded strings
  47. HOUNDIFY_CLIENT_KEY = "INSERT HOUNDIFY CLIENT KEY HERE" # Houndify client keys are Base64-encoded strings
  48. try:
  49. print("Houndify thinks you said " + r.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY))
  50. except sr.UnknownValueError:
  51. print("Houndify could not understand audio")
  52. except sr.RequestError as e:
  53. print("Could not request results from Houndify service; {0}".format(e))
  54. # recognize speech using IBM Speech to Text
  55. IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  56. IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
  57. try:
  58. print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
  59. except sr.UnknownValueError:
  60. print("IBM Speech to Text could not understand audio")
  61. except sr.RequestError as e:
  62. print("Could not request results from IBM Speech to Text service; {0}".format(e))