Python Speech Recognition: AttributeError: module 'speech_recognition' has no attribute 'Recognizer'
Python Speech Recognition: AttributeError: module 'speech_recognition' has no attribute 'Recognizer'
我知道我的问题有几个线索,但没有任何答案对我有帮助,我尝试了我能想到的任何东西并在此处看到。
我得到的错误代码:
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)
我更改了一些代码,因为我不希望它被读取。我从网站复制脚本并修改它以便我可以使用它。
我尝试过的事情:
- Changing the filename
- Compiling the script to a executable via PYInstaller
- Reinstall Speech Recognizer
- Reinstall PyAudio
- Reinstall Python_VLC (while I don't think the error is because of that library, It's worth a atleast.)
我将不胜感激任何帮助,如果问题得到解决,我希望其他有同样问题的人能从这个帖子中受益,谢谢。
您似乎有一个文件 /home/pi/Documents/SB/speech_recognition.py
,因此它正在您的文件中寻找 Recognizer()
(与实际模块 speech_recognition
相对)。尝试将您的 speech_recognition.py
文件重命名为其他名称。
我知道我的问题有几个线索,但没有任何答案对我有帮助,我尝试了我能想到的任何东西并在此处看到。
我得到的错误代码:
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)
我更改了一些代码,因为我不希望它被读取。我从网站复制脚本并修改它以便我可以使用它。
我尝试过的事情:
- Changing the filename
- Compiling the script to a executable via PYInstaller
- Reinstall Speech Recognizer
- Reinstall PyAudio
- Reinstall Python_VLC (while I don't think the error is because of that library, It's worth a atleast.)
我将不胜感激任何帮助,如果问题得到解决,我希望其他有同样问题的人能从这个帖子中受益,谢谢。
您似乎有一个文件 /home/pi/Documents/SB/speech_recognition.py
,因此它正在您的文件中寻找 Recognizer()
(与实际模块 speech_recognition
相对)。尝试将您的 speech_recognition.py
文件重命名为其他名称。