Python语音识别:AttributeError:模块“ speech_recognition”没有属性“ Recognizer”

时间:2018-08-01 23:45:22

标签: python speech-recognition

我知道我的问题有多个线索,但是没有答案可以帮助我,我尝试了所有我能想到的并在这里看到的东西。

错误代码正在获取:

Traceback (most recent call last):
  File "/home/pi/Documents/SB/sp_recog.py", line 4, in <module>
    import speech_recognition as sr
  File "/home/pi/Documents/SB/speech_recognition.py", line 59, in <module>
    data = recordAudio()
  File "/home/pi/Documents/SB/speech_recognition.py" line 20, in recordAudio
    r = sr.Recognizer()
AttributeError: module 'speech_recognition' has no attribute 'Recognizer'

Python脚本即时通讯使用:

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr
import vlc
import time
import os
from time import ctime
from gtts import gTTS

def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='de')
    tts.save("audio.mp3")
    os.system("mpg321 audio.mp3")

def recordAudio():
    # Record Audio
    r = sr.Recognizer()
    p = vlc.MediaPlayer("PRIVATE")
    with p.play() as source:
        print("Say something!")
        audio = r.listen(source)

    # Speech recognition using Google Speech Recognition
    data = ""
    try:
        # Uses the default API key
        # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        data = r.recognize_google(audio)
        print("You said: " + data)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return data


def PRIVATE(data):
    if "PRIVATE" in data:
        speak("PRIVATE")

    if "PRIVATE" in data:
        speak(ctime())

    if "PRIVATE" in data:
        data = data.split(" ")
        location = data[2]
        speak("PRIVATE")


# initialization
time.sleep(2)
speak("PRIVATE")
while 1:
    data = recordAudio()
    PRIVATE(data)

我更改了一些代码,因为我不想读取它。我从站点复制了脚本并进行了修改,以便可以使用它。

我尝试过的事情:

  
      
  • 更改文件名
  •   
  • 通过PYInstaller将脚本编译为可执行文件
  •   
  • 重新安装语音识别器
  •   
  • 重新安装PyAudio
  •   
  • 重新安装Python_VLC(虽然我不认为错误是由于该库造成的,但至少值得这样做。)
  •   

我将不胜感激,希望能解决该问题,使其他有相同问题的人也能从此线程中受益,谢谢。

1 个答案:

答案 0 :(得分:3)

似乎您有一个文件/home/pi/Documents/SB/speech_recognition.py,因此它正在文件中寻找Recognizer()(而不是实际的模块speech_recognition)。尝试将speech_recognition.py文件重命名为其他名称。