"AssertionError: This audio source is already inside a context manager" with Speech Recognition in python

"AssertionError: This audio source is already inside a context manager" with Speech Recognition in python

我正在尝试使用 python 中的语音识别库制作语音识别助手,当我试图让它在后台收听某些关键字,然后使用用户所说的执行命令时根据用户所说的。但是每次我尝试这个我都会得到这个错误:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\speech_recognition\__init__.py", line 690, in threaded_listen
    with source as s:
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\speech_recognition\__init__.py", line 134, in __enter__
    assert self.stream is None, "This audio source is already inside a context manager"
AssertionError: This audio source is already inside a context manager

我试图找到答案并查看图书馆的文档,但我不知道该怎么做。

这是我的代码:

import speech_recognition as sr
import re
import TTS

# recognizer
recognizer = sr.Recognizer()

# microphone
microphone = sr.Microphone()

# User
user = ""

# Keywords
keywords = [("hey Gideon", 1), ("Gideon", 1)]

# background listening
listening = False

TTS.speak("Hello user, this is Gideon")
TTS.speak("Please wait for 10 seconds as I am calibrating your microphone")

with microphone as source:
    recognizer.adjust_for_ambient_noise(source, 10)

TTS.speak("Done calibrating your microphone")
TTS.speak("Tell me a name you would like me to call you by")

with microphone as source:
    audio = recognizer.listen(source)
    text = recognizer.recognize_google(audio)
    print("User: " + text)

    user = text

TTS.speak("Welcome " + user)
TTS.speak("I will be listening in the background all you need to say is 'hey Gideon' or 'Gideon' to summon me")
listening = True


def command(word):
    if word == "What is my name":
        print(user + ": What is my name")
        TTS.speak("Your name is " + user)


def listen(word):
    new_text = ""

    if "hey Gideon" in word:
        new_text = word.replace('hey Gideon', '')
    elif "Gideon" in word:
        new_text = word.replace('Gideon', '')

    command(new_text)


def callback(recognition, micro):
    try:
        word = recognition.recognize_google(micro)

        if "hey Gideon" or "Gideon" in word:
            listen(word)
    except sr.UnknownValueError:
        print("I did not understand can you can you please repeat")
    except sr.RequestError as e:
        print("Error: {}".format(e))


while listening:
    stop_listening = recognizer.listen_in_background(microphone, callback)

while not listening:
    stop_listening(wait_for_stop=False)

TTS 是我制作的 python 文件,如果对这里有帮助,请查看该文件的内容:

import pyttsx3

# Voice
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0")
voiceRate = 145
engine.setProperty("rate", voiceRate)


# Speak Function
def speak(text):
    engine.say(text)
    print("Gideon: " + text)
    engine.runAndWait()

那么我该如何解决这个问题?我到处都找过了,但这些答案仍然对我没有帮助。 提前致谢

这可能不是问题,但语音识别已经有一个名为 listen 的函数,并且您定义了一个同名的新函数。尝试更改您定义的函数的名称。