You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Assistant及UP2板Ubilinux环境中获取ON_RECOGNIZING_SPEECH_FINISHED的text值

How to Extract the text Field from ON_RECOGNIZING_SPEECH_FINISHED Event in Google Assistant SDK

Hey there! Let's walk through how to grab that recognized text from the ON_RECOGNIZING_SPEECH_FINISHED event—whether you're working on an UP2 board running Ubilinux or dealing with the full event flow you mentioned, the core approach is identical.

Core Concept

When using the Google Assistant SDK, your code listens for events emitted by the Assistant service. Each event includes a type identifier and a payload/arguments dictionary with event-specific data. For ON_RECOGNIZING_SPEECH_FINISHED, the text field lives right in that payload.

Example Code (Using Google Assistant Library)

This is the most common setup for embedded devices like the UP2. Here's how to handle the event and extract the text:

from google.assistant.library import Assistant
from google.assistant.library.event import EventType

def handle_assistant_event(event):
    # Target the speech recognition finish event
    if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
        # Safely extract the text from event arguments
        recognized_text = event.args.get('text')
        if recognized_text:
            print(f"Recognized speech: {recognized_text}")
            # Use the text however you need—log it, trigger actions, etc.
            # Example: Save to a local file
            with open("recognized_speech_log.txt", "a") as log_file:
                log_file.write(f"{recognized_text}\n")
    
    # Optional: Handle other events from your flow
    elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        print("Conversation turn started")
    elif event.type == EventType.ON_END_OF_UTTERANCE:
        print("End of user utterance detected")
    elif event.type == EventType.ON_RESPONDING_STARTED:
        is_error = event.args.get('is_error_response', False)
        print(f"Assistant started responding (error: {is_error})")
    elif event.type == EventType.ON_RESPONDING_FINISHED:
        print("Assistant finished responding")
    elif event.type == EventType.ON_CONVERSATION_TURN_FINISHED:
        follow_on = event.args.get('with_follow_on_turn', False)
        print(f"Conversation turn finished (follow-up needed: {follow_on})")

# Initialize Assistant with your credentials (set this up first!)
with Assistant(credentials) as assistant:
    print("Assistant ready—speak now!")
    for event in assistant.start():
        handle_assistant_event(event)

Key Details

  • event.type lets you pinpoint the exact event—we check for EventType.ON_RECOGNIZING_SPEECH_FINISHED to target the right one.
  • event.args.get('text') is safer than direct indexing (event.args['text']) because it won’t throw an error if the text key is missing unexpectedly.
  • This code works directly on your UP2 board running Ubilinux—just ensure you’ve installed the Google Assistant Library and set up your API credentials correctly.

For gRPC-Based SDK Setup

If you’re using the lower-level gRPC version of the SDK, the process is similar but involves unpacking the protobuf payload:

from google.assistant.embedded.v1alpha2 import embedded_assistant_pb2

def process_assistant_response(response):
    for event in response.events:
        if event.name == 'ON_RECOGNIZING_SPEECH_FINISHED':
            payload = embedded_assistant_pb2.RecognizingSpeechFinished()
            event.payload.Unpack(payload)
            recognized_text = payload.text
            print(f"Recognized text: {recognized_text}")

That’s all there is to it! Once you extract the text field, you can integrate it into your UP2 board’s workflows—whether that’s automating tasks, logging user queries, or connecting to other local services.

内容的提问来源于stack exchange,提问作者afifizain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:33:33