· 6 years ago · Jan 19, 2020, 07:48 PM
1import speech_recognition as sr
2
3# Record Audio
4r = sr.Recognizer()
5with sr.Microphone() as source:
6 print("Say something!")
7 audio = r.listen(source)
8
9# Speech recognition using Google Speech Recognition
10try:
11 # for testing purposes, we're just using the default API key
12 # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
13 # instead of `r.recognize_google(audio)`
14 print("You said: " + r.recognize_google(audio))
15except sr.UnknownValueError:
16 print("Google Speech Recognition could not understand audio")
17except sr.RequestError as e:
18 print("Could not request results from Google Speech Recognition service; {0}".format(e))
19
20# Other code
21import pyttsx3
22engine = pyttsx3.init()
23text = input("What Do You Want Me To Say? ")
24engine.say(text)
25engine.runAndWait()