首页 > 解决方案 > AttributeError: __enter__ 语音识别

问题描述

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

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

我无法识别这个问题。对于任何建议,我都会非常高兴.. 这是我的代码:

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)

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

标签: pythonpandasspeech-recognitionspotipy

解决方案


__enter__只是一个非强制对象方法,当with在所述对象上调用 a 时调用它。更加具体:

object.__enter__(self):输入与该对象相关的运行时上下文。with 语句将此方法的返回值绑定到语句的 as 子句中指定的目标(如果有)。

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

在您的情况下,那是您的Microphone, named m。由于它没有__enter__方法,因此程序无法调用它with m as source:并抛出错误。

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

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


推荐阅读