【问题标题】:Running Python script lets error appear in console运行 Python 脚本让错误出现在控制台中
【发布时间】:2022-01-04 15:39:11
【问题描述】:

我知道版本已经过时,但我需要知道问题所在。如果我想运行我的脚本,就会出现这个错误。

  File "c:/Users/auth/Downloads/JARVIS/JARVIS/jarvis.py", line 319, in <module>
    query = takeCommandMic().lower()
  File "c:/Users/auth/Downloads/JARVIS/JARVIS/jarvis.py", line 118, in takeCommandMic
    with sr.Microphone() as source:
  File "C:\Users\auth\AppData\Roaming\Python\Python36\site-packages\speech_recognition\__init__.py", line 141, in __enter__
    input=True,  # stream is an input stream
  File "C:\Users\auth\AppData\Roaming\Python\Python36\site-packages\pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Users\auth\AppData\Roaming\Python\Python36\site-packages\pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9999] Unanticipated host error
Error in sys.excepthook:

Original exception was:

问题应该在那里:

def takeCommandMic():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 0.5
        audio = r.listen(source)
    try:

        print("recognizing...")
        query = r.recognize_google(audio , language="de-DE")
        print(query)
    except Exception as e:
        print(e)
        return "Nichts"
    return query

还有

if __name__ == "__main__":
    getvoices(1)
    speak("Hallo, hier ist jarvis")
    #wishme()
    
while True:    
    turnOnLightsAtTime()
    query = takeCommandMic().lower()

【问题讨论】:

  • 您能提供一个代码示例吗?
  • 在您的问题中可能已格式化

标签: python python-3.x speech-recognition text-to-speech pyaudio


【解决方案1】:

这更适合发表评论,但我没有发表评论所需的 50 个代表:

看来您正在使用pyaudio 库。根据这个问题:pyaudio-OSError: [Errno -9999] Unanticipated host error(你的可能相当于),这可能是由于没有设置正确的权限来访问麦克风。

更改此设置的方法取决于操作系统,但在我的 Windows 设备上,它位于“设置”中的“隐私”下。

【讨论】:

  • Windows 说它已激活。我什至不能改变它
【解决方案2】:

我没有足够的代表发表评论并因此发布答案。 是否有任何额外的堆栈跟踪?因为我试过你的代码,它对我有用。只是我必须添加更多功能才能使其适合我。 由于没有太多关于你使用的 TTS 的详细信息,所以我冒昧地使用了 pyttsx3,我用的很放心。

import speech_recognition as sr
import pyttsx3

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
rate = engine.getProperty('rate')
engine.setProperty('voice', voices[0].id)
engine.setProperty('rate', 150)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def takeCommandMic():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 0.5
        audio = r.listen(source)
    try:

        print("recognizing...")
        query = r.recognize_google(audio , language="de-DE")
        print(query)
    except Exception as e:
        print(e)
        return "Nichts"
    return query


def turnOnLightsAtTime():
    speak("Lights are on. Speak up!")

if __name__ == "__main__":
    speak("Hallo, hier ist jarvis")
    # wishme()

while True:
    turnOnLightsAtTime()
    query = takeCommandMic().lower()
    print("the query is: ", query)
    if 'den' in query:
        speak("yes it is working now")
        print("Yes it is working now")
    elif 'sleep' in query:
        print("the query is: ", query)
        speak("good bye")
        print('good bye!')
        break

不确定是否在此处发布录音,但现在我正在粘贴我得到的快照。请注意,打印语句实际上是通过声音说出的。

【讨论】:

  • 您好,感谢您的帮助。事情是:在我的家用电脑上,(整个)代码正在运行。但是在学校的笔记本电脑上出现了这个错误。我可能需要管理员权限吗?
猜你喜欢
  • 2017-12-29
  • 2020-08-23
  • 2019-12-22
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
相关资源
最近更新 更多