几天前..请帮助
我想记录并打印输入麦克风中的内容。我有一个正在工作的内置麦克风。 (并且我已经检查过)。但是,当我打电话给recogniser.listen
时,它永远不会停止收听。我已经在互联网上签到,有人说要添加到参数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"
按照文档的说明,当您退出with时,录制会停止。您可能会在之后打印一些信息,以知道录制已停止。这是10秒后停止录制的方法。
import speech_recognition as sr
recognizer = sr.Recognizer()
mic = sr.Microphone(device_index=1)
with mic as source:
recognizer.adjust_for_ambient_noise(source)
captured_audio = recognizer.record(source=mic, duration=10)
(将持续时间更改为任何合适的时间,因为您将其用作超时示例,因此我使用了10,]]