Skip to content

Instantly share code, notes, and snippets.

@SankaitLaroiya
Last active February 17, 2018 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SankaitLaroiya/200c4c916b897fec5f1d8b4a4833dd04 to your computer and use it in GitHub Desktop.
Save SankaitLaroiya/200c4c916b897fec5f1d8b4a4833dd04 to your computer and use it in GitHub Desktop.
(Python) Speech recognition using IBM Speech to Text
#!/usr/bin/env python3
# NOTE 1: Add the username and password to your IBM Watson account below
# (More info: https://www.ibm.com/watson/services/text-to-speech/)
# NOTE 2: this example requires PyAudio because it uses the Microphone class
# pip3 install pyaudio
# if getting an error do
# brew remove portaudio
# brew install portaudio
# pip3 install pyaudio
import speech_recognition as sr
from datetime import datetime
startTime = datetime.now()
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source, 2)
# recognize speech using IBM Speech to Text
IBM_USERNAME = "YOUR USERNAME HERE"
IBM_PASSWORD = "YOUR PASSWORD HERE"
try:
print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
print(datetime.now() - startTime)
except sr.UnknownValueError:
print("IBM Speech to Text could not understand audio")
except sr.RequestError as e:
print("Could not request results from IBM Speech to Text service; {0}".format(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment