如何从hotword.py代码中获取语音文本并对已识别的文本执行自己的操作,而不是谷歌对该文本做出反应?
我已经在Pi3上安装了GA,并且在使用usb mic /模拟音频设置和某些Python文件失踪之后,这让我开始了:When installing Google Assistant, I an error "...googlesamples.assistant' is a package and cannot be directly executed..."然后我跟着谷歌接下来的步骤: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处理它。我怎么做?
最终我会问“好的谷歌,打开BBC1”或“好的谷歌,播放我的播放列表”或“好的谷歌,显示流量”和hotword.py将运行其他应用程序来完成这些任务。
谢谢,史蒂夫
有关所有可用方法,请参阅此处的文档 - 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()
我最近将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中编写自定义代码
"""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