Script JSON communication
Demo
Command Line
1~/depthai-python/examples/Script$ python3 script_json_communication.py
2dict {'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'}
5changedDict {'one': 2, 'foo': 'baz'}Setup
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.pySource 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.