这是我的代码:
import speech_recognition
import pyttsx3
from datetime import date, datetime
robot_ear = speech_recognition.Recognizer()
robot_mouth = pyttsx3.init()
robot_brain = ""
while True:
with speech_recognition.Microphone() as mic:
print("Robot: I'm Listening")
audio = robot_ear.listen(mic)
print("Robot:...")
try:
you = robot_ear.recognize_google(audio)
except:
you = ""
print ("You: " + you)
you = "hello"
if you == "":
robot_brain = "I can't hear you, try again!"
elif"hello" in you:
robot_brain = "Hello Huan"
elif "today" in you:
today = date.today()
robot_brain = today.strftime("%B %d, %Y")
elif "time" in you:
now = datetime.now()
robot_brain = now.strftime("%H hours %M minutes %S seconds")
elif "president" in you:
robot_brain = "Donald Trump"
elif "bye" in you:
robot_brain = "Bye Duong Gia Huan"
print("Robot: " + robot_brain)
robot_mouth.say(robot_brain)
robot_mouth.runAndWait()
break
else:
robot_brain = "I'm fine, Thank you, and you ?"
print("Robot: " + robot_brain)
robot_mouth.say(robot_brain)
robot_mouth.runAndWait()
当我运行它时,出现这样的错误:
Robot: I'm Listening
Traceback (most recent call last):
File "trolyao.py", line 12, in <module>
audio = robot_ear.listen(mic)
File "C:\Users\huana\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 652, in listen
buffer = source.stream.read(source.CHUNK)
File "C:\Users\huana\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 161, in read
return self.pyaudio_stream.read(size, exception_on_overflow=False)
File "C:\Users\huana\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio.py", line 608, in read
return pa.read_stream(self._stream, num_frames, exception_on_overflow)
OSError: [Errno -9999] Unanticipated host error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "trolyao.py", line 12, in <module>
audio = robot_ear.listen(mic)
File "C:\Users\huana\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 151, in __exit__
self.stream.close()
File "C:\Users\huana\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 166, in close
if not self.pyaudio_stream.is_stopped():
File "C:\Users\huana\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio.py", line 543, in is_stopped
return pa.is_stream_stopped(self._stream)
OSError: [Errno -9988] Stream closed
帮我解决这个问题!!!我是Python的初学者! 请帮我解决它
你能帮我吗? 我不知道那是什么? 我认为我们应该用麦克风修复音频,对吧? Python 编程语言简介。 Python 由 Guido van Rossum 开发。 Guido van Rossum 于 1989 年开始实现 Python。Python 是一种非常简单的编程语言,因此即使您是编程新手,也可以轻松学习 Python。
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-us')
except Exception as e:
print("Say that again please...")
return "None"
return query
我不知道错误背后的原因,但我认为你需要添加上面的代码。我猜它不能清楚地识别音频,所以尝试调整环境噪音。我希望它有帮助。我也是初学者。和平