Om te beginnen: Ik heb nog geen verstand van programmeren. Ik ben pas sinds een half jaar bezig met C/C++. Met Python heb ik nog geen enkele ervaring. Excuses alvast als ik verkeerde terminologie gebruik.
Ik ben de Google Assistant SDK aan het uitproberen.
Volgens de handleiding is het mogelijk om de door Google herkende tekst uit een JSON string object te halen.
https://developers.google.../sdk/prototype/next-steps
De handleiding geeft aan:
Hoe kan ik zorgen dat mijn code het gesproken woord haalt uit de JSON string (bijvoorbeeld 'hello') zodat ik daar vervolgens een actie aan kan koppelen (bijvoorbeeld het versturen van een MQTT message)
Mijn totale code:
Ik ben de Google Assistant SDK aan het uitproberen.
Volgens de handleiding is het mogelijk om de door Google herkende tekst uit een JSON string object te halen.
https://developers.google.../sdk/prototype/next-steps
De handleiding geeft aan:
De output ziet er als volgt uit als Google het woordje 'hello' herkent.For the Google Assistant library, the transcript is located in a JSON string object in the ON_RECOGNIZING_SPEECH_FINISHED event.
Na enig onderzoek moet ik dus de informatie uit het JSON string object halen. Dat probeer ik als volgt te doen middels json.load en json.loads. Ik krijg echter de volgende output. Ik weet niet meer zo goed wat ik nu kan proberen.ON_RECOGNIZING_SPEECH_FINISHED:
{'text': 'hello'}
Mijn vraag is dus:if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
data = json.loads(event.type)
TypeError: the JSON object must be str, not 'EventType'
if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
data = json.load(event.type)
AttributeError: 'EventType' object has no attribute 'read'
Hoe kan ik zorgen dat mijn code het gesproken woord haalt uit de JSON string (bijvoorbeeld 'hello') zodat ik daar vervolgens een actie aan kan koppelen (bijvoorbeeld het versturen van een MQTT message)
Mijn totale code:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| #!/usr/bin/env python # Copyright (C) 2017 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import print_function import argparse import os.path import json import google.oauth2.credentials from google.assistant.library import Assistant from google.assistant.library.event import EventType from google.assistant.library.file_helpers import existing_file 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() print(event) if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and event.args and not event.args['with_follow_on_turn']): print() if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED: data = json.loads(event.type) def main(): parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('--credentials', type=existing_file, metavar='OAUTH2_CREDENTIALS_FILE', default=os.path.join( os.path.expanduser('~/.config'), 'google-oauthlib-tool', 'credentials.json' ), help='Path to store and read OAuth2 credentials') args = parser.parse_args() with open(args.credentials, 'r') as f: credentials = google.oauth2.credentials.Credentials(token=None, **json.load(f)) with Assistant(credentials) as assistant: for event in assistant.start(): process_event(event) if __name__ == '__main__': main() |