AttributeError:'module'对象没有属性'Recognizer'

问题描述 投票:-2回答:2

我正在使用python的Speach Recognition,我的代码给了我一个AttributeError

import os
import pyaudio
import speech_recognition as sr

def excel():
        os.system("start excel.exe")

def internet():
        os.system("start chrome.exe")

def media():
        os.system("start wmplayer.exe")

def mainfunction(source):
    audio = r.listen(source)
    user = r.recognize_google(audio)
    print(user)
    if user == "Excel":
        excel()
    elif user == "Internet":
        internet()
    elif user == "music":
        media()

if __name__ == "__main__":
    r = sr.Recognizer()    #this is the line that fails
    with sr.Microphone() as source:
        while 1:
            mainfunction(source)

错误消息是:

AttributeError: 'module' object has no attribute 'Recognizer'
(test-dev-pro)➜  ~  python speech_recognition.py
Traceback (most recent call last):
  File "speech_recognition.py", line 26, in <module>
    r = sr.Recognizer()
AttributeError: 'module' object has no attribute 'Recognizer'

但是Recognizer用在我看过的所有例子中,所以我不明白为什么我会收到这个错误。

python speech-recognition pyaudio
2个回答
3
投票

虽然这不是重复,但您遇到与此人相同的问题:Python "import random" Error

通过命名您的文件speach_recognition,您最终导入自己的文件而不是库文件。要解决这个问题,只需更改文件的名称,就可以了! :)

编辑:请务必查看该问题的评论以及清理.pyc文件的信息。


0
投票

我认为你的源文件名是speech_recognition.py,这与语音识别包中的方法有冲突。所以只需更改文件的名称即可。如果发生另一个错误就像

AttributeError: 'Recognizer' object has no attribute 'recognize

然后只需将recogn()方法更改为google_recognize()

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.