-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_back.py
32 lines (27 loc) · 993 Bytes
/
speech_back.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import speech_recognition as sr
#quiet the endless 'insecurerequest' warning
import urllib3
import os
import win32com.client as wincl
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
speak = wincl.Dispatch("SAPI.SpVoice")
while (True == True):
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
#print("Please wait. Calibrating microphone...")
# listen for 1 second and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio = r.listen(source,phrase_time_limit=5)
# recognize speech using Sphinx/Google
try:
#response = r.recognize_sphinx(audio)
response = r.recognize_google(audio)
print("I think you said '" + response + "'")
speak.Speak(response)
# os.remove(responseFile)
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))