我正试图从这里测试个人助理:https://pythonspot.com/en/personal-assistant-jarvis-in-python/
我确信我已经安装了所有模块和库等(花了20分钟来设置!)
问题在于,当我运行它时,它会说"嗨弗兰克什么能为你做什么?"但后来它抛出了这个错误:
/usr/bin/python3.5 /home/me/PycharmProjects/PersonalAssistant/PersonalAssistant.py
Hi Frank, what can I do for you?
/home/me/.local/lib/python3.5/site-packages/urllib3/connectionpool.py:852: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
now maintained by Nanakos Chrysostomos and others.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
tcgetattr(): Inappropriate ioctl for device
Playing MPEG stream from audio.mp3 ...
MPEG 2.0 layer III, 32 kbit/s, 24000 Hz mono
[0:02] Decoding of audio.mp3 finished.
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
Say something!
Traceback (most recent call last):
File "/home/me/PycharmProjects/PersonalAssistant/PersonalAssistant.py", line 58, in <module>
data = recordAudio()
File "/home/me/PycharmProjects/PersonalAssistant/PersonalAssistant.py", line 23, in recordAudio
audio = r.listen(source)
File "/home/me/.local/lib/python3.5/site-packages/speech_recognition/__init__.py", line 437, in listen
assert source.stream is not None, "Audio source must be opened before recording - see documentation for `AudioSource`"
AssertionError: Audio source must be opened before recording - see documentation for `AudioSource`
Process finished with exit code 1
这是参考源代码:
#!/usr/bin/env python3
# Requires PyAudio and PySpeech.
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
os.system("mpg321 audio.mp3")
def recordAudio():
# Record Audio
r = sr.Recognizer()
with sr.Microphone() 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 jarvis(data):
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if "where is" in data:
data = data.split(" ")
location = data[2]
speak("Hold on Frank, I will show you where " + location + " is.")
os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")
# initialization time.sleep(2) speak("Hi Frank, what can I do for you?") while 1:
data = recordAudio()
jarvis(data)
答案 0 :(得分:0)
此代码:
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
应该是
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
请注意缩进以及示例here。当麦克风发生故障时,不会使用适当的缩进执行侦听代码。
你的下一个问题是麦克风失败,这是一个pyaudio问题,你需要正确配置音响系统,详见PyAudio prints ALSA warnings and does not work