-2

This is my code:

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()

when I run it, It's have an error like this :

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

Help me fix it !!!I'm a begginer of Python ! Help me fix it, pls

Can you help me ? I don't know what it is ? I think we should fix the audio with the microphone, Right ? ntroduction to Python Programming language. Python is developed by Guido van Rossum. Guido van Rossum started implementing Python in 1989. Python is a very simple programming language so even if you are new to programming, you can learn python without facing any issues.

1 Answers1

0
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

I don't know the reason behind the error but i think you need add the above code. I guess it is not recognizing audio clearly so try to adjust the ambient noise. I hope it helps. I am a beginner too. Peace