Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import speech_recognition as sr
import subprocess
import time
import pyowm
import json
owm = pyowm.OWM('api_key')
subprocess.run(["spd-say", "hello, I am Matilda", "-t", "female2"])
time.sleep(2)
print('ready');
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
# recognize speech using Sphinx
try:
#print("Sphinx thinks you said " + r.recognize_sphinx(audio))
sphinxes_opinion = r.recognize_sphinx(audio)
except sr.UnknownValueError:
sphinxes_opinion = ''
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
# recognize speech using Google Speech Recognition
try:
googles_opinion = r.recognize_google(audio)
except sr.UnknownValueError:
googles_opinion = ''
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
if (googles_opinion == "weather forecast in Auckland"):
observation = owm.weather_at_place('Auckland')
w = observation.get_weather()
forecast = w.get_temperature('celsius')
subprocess.run(["spd-say", "It's " + str(int(forecast['temp'])) + ' degrees', "-t", "female2"])
elif (googles_opinion == "weather forecast in Rio"):
observation = owm.weather_at_place('Rio de Janeiro')
w = observation.get_weather()
forecast = w.get_temperature('celsius')
subprocess.run(["spd-say", "It's " + str(int(forecast['temp'])) + ' degrees', "-t", "female2"])
elif (googles_opinion == "weather forecast in Sao Paulo"):
observation = owm.weather_at_place('Sao Paulo')
w = observation.get_weather()
forecast = w.get_temperature('celsius')
subprocess.run(["spd-say", "It's " + str(int(forecast['temp'])) + ' degrees', "-t", "female2"])
else:
if (sphinxes_opinion == googles_opinion):
subprocess.run(["spd-say", "I heard you say: " + googles_opinion, "-t", "female2"])
else:
subprocess.run(["spd-say", "I am not sure I heard you properly, but I guess you said " + googles_opinion, "-t", "female2"])
print(googles_opinion)
print(sphinxes_opinion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment