首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JARVIS语音助手的回溯错误

JARVIS语音助手的回溯错误
EN

Stack Overflow用户
提问于 2022-02-10 00:49:36
回答 1查看 31关注 0票数 0

我在做贾维斯的助理,但是我有这个错误

代码语言:javascript
复制
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音频,它正在工作。

我的代码在下面,请看一下

代码语言:javascript
复制
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上的一个教程来做的

EN

回答 1

Stack Overflow用户

发布于 2022-02-18 14:15:18

目前,takecommand()函数在识别音频时不返回任何内容。但是,您确实有一个返回语句(返回查询),它看起来是在错误的位置,如果您将它移到除了行之前,它应该可以工作。所以这个函数应该如下所示:

代码语言:javascript
复制
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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71058553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档