繁体   English   中英

AttributeError: __enter__ 语音识别

[英]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__

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

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__只是一个非强制性的 object 方法,当在所述 object 上调用with时调用该方法。 更加具体:

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

来自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)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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