how to use the recognized text with Google Assistant's hotword.py code

时间:2017-06-09 12:49:21

标签: extract google-assistant-sdk

How do I get the spoken text from the hotword.py code & do my own actions on the recognised text rather than Google going off and reacting to the text?

I've installed GA on the Pi3 & after some initial issues with usb mic/analogue audio settings and certain Python files missing this got me going: When installing Google Assistant, I an error "...googlesamples.assistant' is a package and cannot be directly executed..." I then followed the Google Next steps : https://developers.google.com/assistant/sdk/prototype/getting-started-pi-python/run-sample and created a new project "myga/" with a hotword.py file that contains:

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/

I'd like code to react to the ON_RECOGNIZING_SPEECH_FINISHED event I think and at either do my own action by matching simple requests or if the phrase is not in my list then let Google handle it. How do I do that?

Eventually I'd be asking "OK Google, turn BBC1 on" or "OK Google, play my playlist" or "OK Google, show traffic" and hotword.py would run other applications to do those tasks.

Thanks, Steve

3 个答案:

答案 0 :(得分: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()方法并自行处理命令。如果不做任何事情(让谷歌处理它)

  4. 伪代码 -

    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()
    

答案 1 :(得分: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

action.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

答案 2 :(得分:0)

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

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