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