我正在创建和学习如何创建 Python 个人助理。 我不明白我的代码有什么问题。我连接了我的麦克风,但它无法识别。你能帮助我吗。 我的代码:
import speech_recognition as sr
import pyttsx3
import datetime
import time
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)
engine.setProperty("rate",150)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def Wishme():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("good morning Boss!")
print("Good morning Boss!")
elif hour >= 12 and hour <16:
speak("Good afternoon Boss!")
print("good afternoon Boss!")
else:
speak("Good Evening Boss!")
print("Good Evening Boss!")
speak("what can I do for you?")
print("what can I do for you?")
def Takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Wait for few moments')
query = r.recognize_google(audio,language="en-in")
print("user said", query)
except Exception as e :
print(e)
speak("Please say that again.")
print("Please say that again.")
return "None"
return query
if __name__== '__main__':
Wishme()
Takecommand()
我的输出: 老板下午好! 我能为你做什么? 听着...
它不听我的命令。