Python-类型错误 : listen() missing 1 required positional argument: 'self'

标签 python python-3.x pycharm speech-recognition pyttsx

我一直在 PyCharm 中开发人工智能,但我似乎在尝试调用方法来尝试获取音频输入时遇到了 voice_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-类型错误 : listen() missing 1 required positional argument: 'self' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770064/

相关文章:

python - 如何在 Python 控制台中使用其选项运行 .py 文件?

python - 如何只减去 numpy 数组中一行的第一个数字

python - 我如何在Python中分割字典中的文本文件

python - SQLAlchemy 选择存在限制

python - 尝试/排除类中的每个方法?

python - 如何在 Python 3 中像在 Python 2 中一样将 chr(0xdfff) 转换为 utf-8 字节?

python - 使用两个数据帧之间的 "VLOOKUP"创建新数据帧

python - 使用列表生成器时 Python 3 中的 pdb 模块中可能存在错误

python - PyCharm noinspection 对整个文件?

python - 特定版本pycharm的离线文档