AttributeError: __enter__ Speech Recognition

AttributeError: __enter__ Speech Recognition

我正在尝试使用 python 制作语音助手。我从 github 获得了资源。一切似乎都正确,但是当我尝试 运行 项目时,它说:

File "c:\Users\icell\Desktop\Programlama\Python\python_calışma\jarvis.py", line 45, in <module>
    with m as source:
AttributeError: __enter__

我无法识别 problem.For 任何建议我都会非常非常高兴.. 这是我的代码:

import pandas as pd
from speech_recognition import Microphone, Recognizer, UnknownValueError
import spotipy as sp
from spotipy.oauth2 import SpotifyOAuth
from pepper import *


setup = pd.read_csv('setup/setup.txt', sep='=',index_col=0, squeeze=True, header=None)
client_id = setup['client_id']
client_secret = setup['client_secret']
device_name = setup['device_name']
redirect_uri = setup['redirect_uri']
username = setup['username']
scope = setup['scope']

auth_manager = SpotifyOAuth(
    client_id=client_id,
    client_secret=client_secret,
    redirect_uri=redirect_uri,
    scope=scope,
    username=username)
spotify = sp.Spotify(auth_manager=auth_manager)


devices = spotify.devices()
deviceID = None
for d in devices['devices']:
    d['name'] = d['name'].replace('’', '\'')
    if d['name'] == device_name:
        deviceID = d['id']
        break
r = Recognizer()
m = None
input_mic = 'Rampage'
for i, microphone_name in enumerate(Microphone.list_microphone_names()):
    if microphone_name == input_mic:
        m = Microphone(device_index=i)

while True:
    with m as source:
        r.adjust_for_ambient_noise(source=source)
        audio = r.listen(source=source)

    command = None
    try:
        command = r.recognize_google(audio_data=audio).lower()
    except UnknownValueError:
        continue

    print(command)
    words = command.split()
    if len(words) <= 1:
        print('Could not understand. Try again')
        continue

    name = ' '.join(words[1:])
    try:
        if words[0] == 'album':
            uri = get_album_uri(spotify=spotify, name=name)
            play_album(spotify=spotify, device_id=deviceID, uri=uri)
        elif words[0] == 'artist':
            uri = get_artist_uri(spotify=spotify, name=name)
            play_artist(spotify=spotify, device_id=deviceID, uri=uri)
        elif words[0] == 'play':
            uri = get_track_uri(spotify=spotify, name=name)
            play_track(spotify=spotify, device_id=deviceID, uri=uri)
        else:
            print('Specify either "album", "artist" or "play". Try Again')
    except InvalidSearchError:
        print('InvalidSearchError. Try Again')

这行错误:

with m as source:
        r.adjust_for_ambient_noise(source=source)
        audio = r.listen(source=source)

我真的不知道输入属性。这就是为什么我对这种情况没有任何想法。

__enter__ 只是一个非强制对象方法,当对所述对象调用 with 时调用该方法。更具体地说:

object.__enter__(self): Enter the runtime context related to this object. The with statement will bind this method’s return value to the target(s) specified in the as clause of the statement, if any.

来自 https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers.

在您的例子中,就是您的 Microphone,名为 m。由于它没有 __enter__ 方法,程序无法在 with m as source: 中调用它并抛出错误。

# with m as source: # Unnecessary
r.adjust_for_ambient_noise(source=m)
audio = r.listen(source=m)

但是,您不一定非得在 m 上调用此 with 才能让您的程序运行。您可以简单地删除这一行,将这两行中的 source 替换为 m,它应该可以正常工作。

m = None 我将此行更改为: m= Microphone(device_index=0)