我在做贾维斯的助理,但是我有这个错误
Traceback (most recent call last):
File "d:\project\Jarvis\jarvis.py", line 43, in <module>
query = takeCommand().lower()
AttributeError: 'NoneType' object has no attribute 'lower而且我还安装了py音频,它正在工作。
我的代码在下面,请看一下
import pyttsx3
import datetime
import speech_recognition as sr
import pyaudio
import Wikipedia
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
def speak(audio):
engine.setProperty("rate", 130)
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>= 0 and hour<12:
speak("Good Morning")
elif hour>= 12 and hour<18:
speak("Good Afternoon")
else:
speak("Good Evening")
speak("I am Jarvis sir! How may I help you")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language="en-in")
print(f"User said: {query}\n")
except Exception as e:
print("Say that again please...")
return "None"
return query
if __name__ == "__main__":
wishMe()
while True:
query = takeCommand().lower()
if 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)我想不出如何修正这个错误。我是用CodeWithHarry制作的youtube上的一个教程来做的
发布于 2022-02-18 14:15:18
目前,takecommand()函数在识别音频时不返回任何内容。但是,您确实有一个返回语句(返回查询),它看起来是在错误的位置,如果您将它移到除了行之前,它应该可以工作。所以这个函数应该如下所示:
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language="en-in")
print(f"User said: {query}\n")
return query
except Exception as e:
print("Say that again please...")
return "None"https://stackoverflow.com/questions/71058553
复制相似问题