“AssertionError:此音频源已在上下文管理器中”,使用python语言进行语音识别

2024-09-30 06:26:06 发布

您现在位置: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()

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


Tags: textinsourceisasenginettsword
1条回答
网友
1楼 · 发布于 2024-09-30 06:26:06

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

相关问题 更多 >

    热门问题