1#!/usr/env/python3
2import speech_recognition as sr
3
4r = sr.Recognizer()
5m = sr.Microphone()
6
7try:
8 print("A moment of silence, please...")
9 with m as source: r.adjust_for_ambient_noise(source)
10 print("Set minimum energy threshold to {}".format(r.energy_threshold))
11 while True:
12 print("Say something!")
13 with m as source: audio = r.listen(source)
14 print("Got it! Now to recognize it...")
15 try:
16 value = r.recognize_google(audio)
17
18 if str is bytes:
19 print(u"You said {}".format(value).encode("utf-8"))
20 else:
21 print("You said {}".format(value))
22 except sr.UnknownValueError:
23 print("Oops! Didn't catch that")
24 except sr.RequestError as e:
25 print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e))
26except KeyboardInterrupt:
27 pass