Я хочу записать и напечатать что-нибудь с входного микрофона. У меня есть встроенный микрофон, который работает. но когда я звоню recogniser.listen
, он никогда не перестает слушать. Я проверил inte rnet, и кто-то сказал добавить к параметрам timeout=10
, так что я сделал, и он останавливается, но я получаю:
raise WaitTimeoutError("listening timed out while waiting for phrase to start")
speech_recognition.WaitTimeoutError: listening timed out while waiting for phrase to start
, хотя я говорю. мой код:
import pyaudio
import wave
import speech_recognition as sr
# OBATINED THE AUDIO
def play_audio(filename):
chunk = 1024
wf = wave.open(filename, "rb")
pa = pyaudio.PyAudio()
# OPENING THE FILE WITH PYAUDIO.
stream = pa.open(
format=pa.get_format_from_width(wf.getsampwidth()), #GETTING THE FORMAT
channels=wf.getnchannels(), #GETTING THE CHANEL
rate=wf.getframerate(), #GETTING THE FRAME RATE
output=True)
data_stream = wf.readframes(chunk) #CREATING A DATA STREAM, WITH CHUNK SIZE
# WHILE THERE IS SOME DATA LEFT
while data_stream:
#WRITING THE DATA
stream.write(data_stream)
#CONTINUE READING
data_stream = wf.readframes(chunk)
#closing the stream
stream.close()
#terminating the pyaudio
pa.terminate()
def init_speech():
print("Listening...")
play_audio('sounds/when.wav')
r = sr.Recognizer()
# OPENING THE MICROPHONE AS source
with sr.Microphone() as source:
print("Say something")
#listening with the recognizer object, and passing it the sorce.
r.adjust_for_ambient_noise(source)
audio = r.listen(source=source,timeout=10)
play_audio('sounds/fill.wav')
command = ""
try:
command = r.recognize_sphinx(audio)
except:
print("Couldn't understand you")
print("Your Command",command)
init_speech ()
Я пытался сделать r.record(source)
, он не падает, но всегда идет к except
, и печатать "Couldn't Understand you"