如何将已识别的文本与 Google 助手的 hotword.py 代码一起使用
how to use the recognized text with Google Assistant's hotword.py code
如何从 hotword.py 代码中获取语音文本并对识别的文本执行我自己的操作而不是 Google 离开并对文本做出反应?
我已经在 Pi3 上安装了 GA,在 usb mic/analogue 音频设置和某些 Python 文件丢失的一些初始问题之后,我开始了:
然后我按照 Google 后续步骤: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
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, turn BBC1 on" 或 "OK Google, play my playlist" 或 "OK Google, show traffic" 并且 hotword.py 会 运行 其他应用程序来执行这些任务。
谢谢,史蒂夫
有关所有可用方法,请参阅此处的文档 -
https://developers.google.com/assistant/sdk/reference/library/python/
您可以使用 stop_conversation()
方法停止 Google 助理处理该请求并自行采取行动。
这是您需要在高层次上做的事情 -
建立你自己的你想要处理的命令字典 -
"turn BBC1 on"、"play my playlist"等
On EventType.ON_RECOGNIZING_SPEECH_FINISHED
事件检查是否
您的字典中存在可识别的命令。
如果您的字典中存在可识别的命令,请调用 assistant.stop_conversation()
方法并自行处理该命令。如果什么都不做(让google处理)
伪代码-
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()
我最近将 google 助手 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
文本包含在事件参数中。通过调用 event.args 您可以使用文本。这是一个例子。
https://github.com/shivasiddharth/GassistPi/blob/master/src/main.py
如何从 hotword.py 代码中获取语音文本并对识别的文本执行我自己的操作而不是 Google 离开并对文本做出反应?
我已经在 Pi3 上安装了 GA,在 usb mic/analogue 音频设置和某些 Python 文件丢失的一些初始问题之后,我开始了:
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
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, turn BBC1 on" 或 "OK Google, play my playlist" 或 "OK Google, show traffic" 并且 hotword.py 会 运行 其他应用程序来执行这些任务。
谢谢,史蒂夫
有关所有可用方法,请参阅此处的文档 - https://developers.google.com/assistant/sdk/reference/library/python/
您可以使用 stop_conversation()
方法停止 Google 助理处理该请求并自行采取行动。
这是您需要在高层次上做的事情 -
建立你自己的你想要处理的命令字典 - "turn BBC1 on"、"play my playlist"等
On
EventType.ON_RECOGNIZING_SPEECH_FINISHED
事件检查是否 您的字典中存在可识别的命令。如果您的字典中存在可识别的命令,请调用
assistant.stop_conversation()
方法并自行处理该命令。如果什么都不做(让google处理)
伪代码-
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()
我最近将 google 助手 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
文本包含在事件参数中。通过调用 event.args 您可以使用文本。这是一个例子。
https://github.com/shivasiddharth/GassistPi/blob/master/src/main.py