python - 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)
我从字面上不知道输入属性。这就是为什么我对这种情况没有任何想法。
解决方案
__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
推荐阅读
- javascript - 将多个对象属性添加到同一个数组?
- excel - 如何根据单元格值链接到工作簿中的工作表?
- authorization - 将授权信息(如资源、范围、策略和权限)包含到 keycloak JWT 中
- mysql - 优化在聊天应用程序中加载消息历史记录的查询
- reactjs - 使用 JWT 令牌将帖子与 React 和 Django 中的用户相关联
- java - 做反序列化的接收器jvm如何知道序列化时使用了哪个serialuuid
- apache-flink - 当 Flink+Redisson 运行异常,反复重试时,Redis 连接数会不断增长,最终达到上限
- google-api - Google reCaptcha 通过 API 创建凭据
- postgresql - 使用 timescaledb 和 postgresql 12 进行复制时,使用 pg_basebackup 时容器关闭
- pandas - 如何在数据框中的两列中找到部分匹配项