我很难理解与块生成器和转录过程相关的 python 脚本摘录的动态。
这是完整的代码:https://cloud.google.com/speech-to-text/docs/samples/speech-transcribe-streaming-mic#code-sample
我没有得到脚本的这一部分:
with MicrophoneStream(RATE, CHUNK) as stream:
audio_generator = stream.generator()
requests = (
speech.StreamingRecognizeRequest(audio_content=content)
for content in audio_generator
)
responses = client.streaming_recognize(streaming_config, requests)
# Now, put the transcription responses to use.
listen_print_loop(responses)
我可以管理“audio_generator”变量接收一种函数返回(来自麦克风的音频流块)。但是,“request”变量获取多个“speech.StreamingRecognizeRequest”对象,这些对象被馈送到“client.streaming_recognize”请求中。然后,“responses”对象在“listen_print_loop”中处理以显示文本结果。但是,我不明白为什么脚本能够无限循环以及它如何通过说“退出”结束。
我尝试搜索线程解释(因为队列使用)和函数返回的“yield”,但我不明白这部分是如何工作的。
请参阅您发送的链接,“listen_print_loop”函数:
def listen_print_loop(responses: object) -> str:
"""Iterates through server responses and prints them.
The responses passed is a generator that will block until a response
is provided by the server.
In this case, responses are provided for interim results as well. If the
response is an interim one, print a line feed at the end of it, to allow
the next result to overwrite it, until the response is a final one. For the
final one, print a newline to preserve the finalized transcription.
Args:
responses: List of server responses
Returns:
The transcribed text.
"""
num_chars_printed = 0
for response in responses:
if not response.results:
continue
# The `results` list is consecutive. For streaming, we only care about
# the first result being considered, since once it's `is_final`, it
# moves on to considering the next utterance.
result = response.results[0]
if not result.alternatives:
continue
# Display the transcription of the top alternative.
transcript = result.alternatives[0].transcript
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
#
# If the previous result was longer than this one, we need to print
# some extra spaces to overwrite the previous result
overwrite_chars = " " * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + "\r")
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript + overwrite_chars)
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r"\b(exit|quit)\b", transcript, re.I):
print("Exiting..")
break
num_chars_printed = 0
return transcript
特别是这部分:
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r"\b(exit|quit)\b", transcript, re.I):
print("Exiting..")
break
如果检测到关键字“exit”或“quit”,则会中断程序的for循环并返回结果。