Python - TypeError: listen() missing 1 required positional argument: 'self'

ⅰ亾dé卋堺 提交于 2021-01-28 11:42:10

问题


I have been working on an AI in PyCharm but and I have seem to have encountered an error with speech_recognition trying to call a method to try to get audio input:

/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

I have tried to google my error but none of the solutions help my situation. Here is my code:

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

I have tried tackling the error for a week but I can't seem to fix this error


回答1:


Instead of this

speech = sr.Recognizer

try this

speech = sr.Recognizer()


来源:https://stackoverflow.com/questions/55770064/python-typeerror-listen-missing-1-required-positional-argument-self

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!