0

如何从 hotword.py 代码中获取口语文本并对识别的文本执行我自己的操作,而不是 Google 对文本做出反应?

我已经在 Pi3 上安装了 GA,在 USB 麦克风/模拟音频设置出现一些初始问题并且某些 Python 文件丢失之后,我开始了: 安装 Google 助手时,出现错误“...googlesamples.assistant”是一个包和不能直接执行......” 然后我按照谷歌下一步步骤:https ://developers.google.com/assistant/sdk/prototype/getting-started-pi-python/run-sample并创建了一个新项目“myga /" 带有一个包含以下内容的 hotword.py 文件:

def process_event(event):
"""Pretty prints events.

Prints all events that occur with two spaces between each new
conversation and a single space between turns of a conversation.

Args:
    event(event.Event): The current event to process.
"""
if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
    print()
    #GPIO.output(25,True)           see https://stackoverflow.com/questions/44219740/how-can-i-get-an-led-to-light-on-google-assistant-listening

if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
    print("got some work to do here with the phrase or text spoken!")

print(event)

if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
        event.args and not event.args['with_follow_on_turn']):
    print()
    #GPIO.output(25,False)          or also see https://blog.arevindh.com/2017/05/20/voice-activated-google-assistant-on-raspberry-pi-with-visual-feedback/

我希望代码对我认为的 ON_RECOGNIZING_SPEECH_FINISHED 事件做出反应,并且要么通过匹配简单的请求来执行我自己的操作,要么如果该短语不在我的列表中,则让 Google 处理它。我怎么做?

最终我会问“OK Google,打开 BBC1”或“OK Google,播放我的播放列表”或“OK Google,显示流量”,hotword.py 将运行其他应用程序来完成这些任务。

谢谢,史蒂夫

4

3 回答 3

2

有关所有可用方法,请参阅此处的文档 - https://developers.google.com/assistant/sdk/reference/library/python/

您可以使用该stop_conversation()方法停止 Google 助理处理该请求并自行采取行动。

这是您需要在高水平上做的事情 -

  1. 建立您自己想要处理的命令字典 - “打开 BBC1”、“播放我的播放列表”等。

  2. EventType.ON_RECOGNIZING_SPEECH_FINISHED事件中检查识别的命令是否存在于您的字典中。

  3. 如果您的字典中存在已识别的命令,请调用该assistant.stop_conversation()方法并自行处理该命令。如果不是什么都不做(让谷歌处理它)

伪代码 -

local_commands  = ['turnBBCOn', 'playLocalPlaylist']

function turnBBCOn() :
#handle locally


function playLocalPlaylist() :
#handle locally


def process_event(event):

    if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        print()

    if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
        print(event.args['text'])
        if event.args['text'] in local_commands:
            assistant.stop_conversation()
            if(event.args['text']='turn BBC1 on')
                turnBBCOn()
            elif(event.args['text']='play my playlist')
                playLocalPlaylist()

    if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
        event.args and not event.args['with_follow_on_turn']):
        print()
于 2017-06-09T13:36:00.480 回答
0

文本包含在事件参数中。通过调用 event.args 你可以使用文本。这是一个例子。

https://github.com/shivasiddharth/GassistPi/blob/master/src/main.py

于 2017-09-15T15:17:24.853 回答
0

我最近将谷歌助手 SDK 与 Raspberry Pi 3 集成。我参考了下面的 git 存储库并创建了 action.py 和 actionbase.py 类可以处理我的自定义命令。我发现创建自己的自定义命令的方式非常简洁灵活。

您可以在 action.py 文件中注册您的自定义命令,如下所示

actor = actionbase.Actor()

actor.add_keyword(
    _('ip address'), SpeakShellCommandOutput(
        say, "ip -4 route get 1 | head -1 | cut -d' ' -f8",
        _('I do not have an ip address assigned to me.')))

return actor

动作.py

在 action.py 中编写您的自定义代码

"""Speaks out the output of a shell command."""

def __init__(self, say, shell_command, failure_text):
    self.say = say
    self.shell_command = shell_command
    self.failure_text = failure_text

def run(self, voice_command):
    output = subprocess.check_output(self.shell_command, shell=True).strip()
    if output:
        self.say(output.decode('utf-8'))
    elif self.failure_text:
        self.say(self.failure_text)

您可以在此处获取完整的源代码。https://github.com/aycgit/google-assistant-hotword

于 2017-08-02T09:58:51.783 回答