【问题标题】:What is this error ? It's an digital assistant这是什么错误?这是一个数字助理
【发布时间】:2020-10-16 22:20:16
【问题描述】:

这是我的代码:

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。

【问题讨论】:

    标签: python stream pyaudio robot errno


    【解决方案1】:
    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
    

    我不知道错误背后的原因,但我认为您需要添加上面的代码。我猜它无法清楚地识别音频,因此请尝试调整环境噪音。我希望它有所帮助。我也是初学者。和平

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多