我如何从hotword.py代码中获得口语文本&我自己的行为是在认可的文本上,而不是谷歌离开并对文本做出反应?
我已经在Pi3上安装了GA &在usb /模拟音频设置出现了一些初始问题之后,某些Python文件丢失了,这让我开始了工作:When installing Google Assistant, I an error "...googlesamples.assistant' is a package and cannot be directly executed...",然后按照Google步骤:https://developers.google.com/assistant/sdk/prototype/getting-started-pi-python/run-sample创建了一个新项目"myga/“,其中包含:
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谷歌,打开BBC1“或"OK谷歌,播放我的播放列表”或"OK谷歌,显示流量“,hotword.py会运行其他应用程序来完成这些任务。
谢谢,史蒂夫
发布于 2017-06-09 13:36:00
关于所有可用的方法,请参阅这里的文档- https://developers.google.com/assistant/sdk/reference/library/python/
您可以使用stop_conversation()方法阻止Google处理该请求并自行行动。
这是你需要在高层次上做的-
EventType.ON_RECOGNIZING_SPEECH_FINISHED事件上,检查字典中是否存在已识别的命令。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-08-02 09:58:51
我最近将google与Raspberry 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 actoraction.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-09-15 15:17:24
文本包含在事件参数中。通过调用event.args,您可以使用文本。下面是一个例子。
https://github.com/shivasiddharth/GassistPi/blob/master/src/main.py
https://stackoverflow.com/questions/44458295
复制相似问题