Skip to content

Commit

Permalink
Fixed script json communication example
Browse files Browse the repository at this point in the history
  • Loading branch information
themarpe committed Jan 4, 2022
1 parent 6fae450 commit 3ca0373
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions examples/Script/script_json_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ int main() {
xin->out.link(script->inputs["in"]);
script->setScript(R"(
import json
# Recieve bytes from the host
data = node.io['in'].get().getData()
jsonStr = str(data, 'utf-8')
dict = json.loads(jsonStr)
# Change initial dictionary a bit
node.warn('Original: ' + str(dict))
dict['one'] += 1
dict['foo'] = "baz"
node.warn('Changed: ' + str(dict))
b = Buffer(30)
b.setData(json.dumps(dict).encode('utf-8'))
node.io['out'].send(b)
Expand All @@ -41,12 +42,16 @@ int main() {
// Connect to device with pipeline
dai::Device device(pipeline);

// This dict will be serialized (JSON), sent to device (Script node),
// edited a bit and sent back to the host
nlohmann::json dict{{"one", 1}, {"foo", "bar"}};
cout << "dict: " << dict << "\n";
auto buffer = dai::Buffer();
auto data = dict.dump();
buffer.setData({data.begin(), data.end()});
device.getInputQueue("in")->send(buffer);

// Wait for the script to send the changed dictionary back
auto jsonData = device.getOutputQueue("out")->get<dai::Buffer>();
auto changedDict = nlohmann::json::parse(jsonData->getData());
cout << "changedDict: " << changedDict << "\n";
Expand Down

0 comments on commit 3ca0373

Please sign in to comment.