python - Python - TypeError:listen()缺少1个必需的位置参数:'self'
问题描述
我一直在 PyCharm 中研究 AI,但我似乎遇到了 Speech_recognition 尝试调用方法以尝试获取音频输入的错误:
/Users/waynedeng/Desktop/AI/venv/bin/python
/Users/waynedeng/Desktop/AI/dawg_2.0.py
Listening...
Traceback (most recent call last):
File "/Users/waynedeng/Desktop/AI/dawg_2.0.py", line 37, in <module>
input = read_input()
File "/Users/waynedeng/Desktop/AI/dawg_2.0.py", line 20, in read_input
audio = speech.listen(source=source, timeout=10, phrase_time_limit=5)
TypeError: listen() missing 1 required positional argument: 'self'
Process finished with exit code 1
我试图用谷歌搜索我的错误,但没有一个解决方案能帮助我解决问题。这是我的代码:
import speech_recognition as sr
import os
from playsound import playsound
import webbrowser
import random
speech = sr.Recognizer
speech.energy_threshold = 4000
greeting_dictionary = {'sup' : 'whats good','ay' : 'wassup'}
def play_sound(mp3_list):
mp3 = random.choice(mp3_list)
play_sound(mp3)
def read_input():
voice_text = ''
print('Listening...')
with sr.Microphone() as source:
audio = speech.listen(source=source, timeout=10, phrase_time_limit=5) #The error is here
try:
voice_text = speech.recognize_google(audio)
except sr.UnknownValueError:
pass
except sr.RequestError as e:
print('Network error')
except sr.WaitTimeoutError:
pass
return voice_text
if __name__ == '__main__':
playsound('mp3/dawg/greet.mp3')
while True:
input = read_input()
print('You: '.format(input))
if 'hello' in input:
continue
elif 'open' in input:
os.system('explorer ~/Desktop {}'.format(input.replace('Open ', '')))
elif 'bye' in input:
exit()
我已经尝试解决该错误一周,但我似乎无法修复此错误
解决方案
而不是这个
speech = sr.Recognizer
试试这个
speech = sr.Recognizer()
推荐阅读
- python - 如何调整这个数据框?
- html - 在 SVG 中,你可以重用一个常见的线性渐变,但改变 CSS 的颜色吗?
- java - 在不使用 split() 的情况下拆分不知道其长度的字符串
- entity - DDD 在一个聚合中的实体之间以一对多而不是一对多的聚合边界
- java - 考虑在你的配置中定义一个 's.safontseva.mynotes.repository.UserRepository' 类型的 bean
- python - 不能第二次输入
- java - Java 11:获取“空 [客户端] 证书链”而不是“空 [服务器] 证书链”
- input - 如何在 Makefile 中只读取一个字符
- python - 修改交互式 python 脚本以在开始时接受参数
- flutter - 颤振任务:android_intent:compileDebugJavaWithJavac FAILED