📅  最后修改于: 2022-03-11 14:45:05.785000             🧑  作者: Mango
import speech_recognition as sr
import pyttsx3
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak anything")
speak("Speak anything")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("you said : {}".format(text))
speak("you said : {}".format(text))
except:
print("Sorry could not recognize your voice")
speak("Sorry could not recognize your voice")