简体   繁体   中英

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

I am trying to make a speech recognition assistant using the speech recognition library in python and when I am trying to make it listen in background for certain keywords and then use what the user said to execute a command based on what the user said. But every time I try this I get this error:

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

I have tried to find answers and look through the docs of the library but I don't know how to do this.

Here is my code:

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)

and TTS is a python file I made if it help here is the contents of that file:

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()

So How do I fix this problem I have looked everywhere and still those answers don't help me. Thanks in advance

This may not be the problem but speech recognition already has a function called listen, and you defined a new function with the same name. Try changing the name of the function you defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM