Script JSON communication
This example shows how Script node can communicate with the outside world (host computer) using JSON serialization by sending a Buffer message. Similarly, you could use SPIIn and SPIOut to send JSON between MCU and Script node via SPI.How would this be useful:2-way communication between Script node and host/MCU could be used to eg. alter the flow of an application.For example, we might want to use 2 different NN models on the device, one until noon and the other from noon till midnight. Host (eg. RPi) could check the current time and when it's noon, it would send a simple message to the Script node which would start forwarding all frames to the other NeuralNetwork node (which has different NN model).What it does:Host creates a dictionary, serializes it, sends it to the Script node. Script node receives the Buffer message, deserializes the dictionary, changes values a bit, serializes the dictionary again and sends it to the host, which deserializes the changed dictionary and prints the new values.Demo
Command Line
1~/depthai-python/examples/Script$ python3 script_json_communication.py
2 dict {'one': 1, 'foo': 'bar'}
3 [14442C1041B7EFD000] [3.496] [Script(1)] [warning] Original: {'one': 1, 'foo': 'bar'}
4 [14442C1041B7EFD000] [3.496] [Script(1)] [warning] Changed: {'one': 2, 'foo': 'baz'}
5 changedDict {'one': 2, 'foo': 'baz'}
Setup
Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the scriptCommand Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
Source code
Python
C++
Python
PythonGitHub
1#!/usr/bin/env python3
2import depthai as dai
3import json
4
5pipeline = dai.Pipeline()
6
7xin = pipeline.create(dai.node.XLinkIn)
8xin.setStreamName('in')
9
10script = pipeline.create(dai.node.Script)
11xin.out.link(script.inputs['in'])
12script.setScript("""
13 import json
14
15 # Receive bytes from the host
16 data = node.io['in'].get().getData()
17 jsonStr = str(data, 'utf-8')
18 dict = json.loads(jsonStr)
19
20 # Change initial dictionary a bit
21 dict['one'] += 1
22 dict['foo'] = "baz"
23
24 b = Buffer(30)
25 b.setData(json.dumps(dict).encode('utf-8'))
26 node.io['out'].send(b)
27""")
28
29xout = pipeline.create(dai.node.XLinkOut)
30xout.setStreamName('out')
31script.outputs['out'].link(xout.input)
32
33# Connect to device with pipeline
34with dai.Device(pipeline) as device:
35 # This dict will be serialized (JSON), sent to device (Script node),
36 # edited a bit and sent back to the host
37 dict = {'one':1, 'foo': 'bar'}
38 print('dict', dict)
39 data = json.dumps(dict).encode('utf-8')
40 buffer = dai.Buffer()
41 buffer.setData(list(data))
42 device.getInputQueue("in").send(buffer)
43
44 # Wait for the script to send the changed dictionary back
45 jsonData = device.getOutputQueue("out").get()
46 jsonText = str(jsonData.getData(), 'utf-8')
47 changedDict = json.loads(jsonText)
48 print('changedDict', changedDict)
Pipeline
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.