Python-TypeError:listen()缺少1个必需的位置参数:'self'

时间:2019-04-20 03:46:44

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

我一直在使用PyCharm开发AI,但是似乎在语音调用识别中遇到错误,试图调用一种方法来获取音频输入:

/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()

我已经尝试解决了一个星期的错误,但似乎无法解决该错误

1 个答案:

答案 0 :(得分:2)

代替此

speech = sr.Recognizer

尝试

speech = sr.Recognizer()