-
Notifications
You must be signed in to change notification settings - Fork 0
/
voiceParse.py
54 lines (49 loc) · 2.06 KB
/
voiceParse.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#Let's attempt to import the speech recgonition library.
try:
import speech_recognition as sr
except:
sys.exit("Error importing libraries. Install 'speech_recognition'")
#When it works, we can initialize the main objects for the recognition
try:
recognize = sr.Recognizer()
microphone = sr.Microphone()
except:
sys.exit("Could not load objects required for speech recognition.")
#Listen timeout in seconds
TIMEOUT = 5
#A simple function that sets the ambient noise level to ease recognition.
def initSpeech():
with microphone as source:
recognize.adjust_for_ambient_noise(source)
#A function that parses the voice of the user.
def parseVoice():
#This is where we stored the text
parsedText = ""
voice = ""
try:
#Now we can listen. We stored the resulting audio in the voice variable.
with microphone as source:
voice = recognize.listen(source, TIMEOUT, 10)
#Now, let's attempt to parse the result using the Google speech recognition
#API. The fall back for all cases is to just have the user input variable
#keyboard what they want to say.
except sr.WaitTimeoutError:
print("listening error found")
try:
print("listening...")
parsedText = recognize.recognize_google(voice)
except sr.UnknownValueError:
print("The Google Speech Recognition API could not recognize speech.(sr.UnknownValueError)")
#parsedText = input("Please type message instead:")
parsedText = parseVoice()
except sr.RequestError as error:
print("Could not contact Google Speech Recognition API. Error: {0}"\
.format(error))
#parsedText = input("Please type message instead: ")
parsedText = parseVoice()
except sr.WaitTimeoutError as error:
print("The Google Speech Recognition API could not recognize speech.{0}".format(error))
parsedText = parseVoice()
#parsedText = input("Please type message instead:")
#We return the recognized or inputted text.
return parsedText