基本上,我所看到的是当我将麦克风作为信号源运行时,它仅每隔几秒钟循环一次while循环,如“。”所示。打印出来。
如果将源更改为音频文件(我包括一个小文件),则确实会循环播放整个BUNCH,如滚动“'”所示。打印到终端。它确实处理了音频(我发现很奇怪),但是被它吹了。
如果您按下CTRL C,它将最终停止,但是它与文件处理后让它运行多长时间完全相同。
还有一个奇怪的是,我创建了一个函数来将在每个循环中生成的音频数据实例保存到wav文件中,并且它们都是44字节(仅在几秒钟的执行时间内就超过了200个字节。但是,当我这样做时)从麦克风采样的音频也是如此,它可以按预期工作,并且仅生成10秒的文件。
代码如下。谢谢大家的时间。
import speech_recognition as sr
from threading import Thread
try:
from queue import Queue # Python 3 import
except ImportError:
from Queue import Queue # Python 2 import
audio_queue = Queue()
r = sr.Recognizer()
def audio_worker():
# this runs in a background thread
while True:
audio = audio_queue.get() # retrieve the next audio processing job from the main thread
if audio is None:
break # stop processing if the main thread is done
# received audio data,now we'll recognize it using Google Speech Recognition
try:
# for testing purposes,we're just using the default API key
# to use another API key,use `r.recognize_google(audio,key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio))
incoming = r.recognize_google(audio)
franzWordLizst = incoming.split()
print("Google Speech Recognition thinks you said " + incoming)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
print("taskdone")
audio_queue.task_done() # mark the audio processing job as completed in the queue
# start a new thread to recognize audio,while this thread focuses on listening
audio_thread = Thread(target=audio_worker)
audio_thread.daemon = True
audio_thread.start()
# with sr.AudioFile("test100.wav") as source:
with sr.microphone(0) as source:
# r.adjust_for_ambient_noise(source)
try:
while True: # repeatedly listen for phrases and put the resulting audio on the audio processing job queue
print(".")
audio_queue.put(r.listen(source,phrase_time_limit=10))
except KeyboardInterrupt: # allow Ctrl + C to shut down the program
pass
audio_queue.join() # block until all current audio processing jobs are done
audio_queue.put(None) # tell the recognize_thread to stop