· 6 years ago · Nov 27, 2019, 04:17 PM
1import speech_recognition as sr
2
3def recog_google(audio):
4 # recognize speech using Google Speech Recognition
5 r = sr.Recognizer()
6 try:
7 # for testing purposes, we're just using the default API key
8 # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
9 # instead of `r.recognize_google(audio)`
10 res = r.recognize_google(audio)
11 print("Google Speech Recognition thinks you said " + res)
12 return res
13 except sr.UnknownValueError:
14 print("Google Speech Recognition could not understand audio")
15 except sr.RequestError as e:
16 print("Could not request results from Google Speech Recognition service; {0}".format(e))
17
18 return ""
19
20
21def listen_micro():
22 r = sr.Recognizer()
23 with sr.Microphone() as source:
24 print("Say something!")
25 audio = r.listen(source)
26 print(audio)
27
28 return audio