script_json_communication.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 #include <thread>
4 
5 // Includes common necessary includes for development using depthai library
6 #include "depthai/depthai.hpp"
7 
8 // Include nlohmann json
9 #include "nlohmann/json.hpp"
10 
11 int main() {
12  using namespace std;
13 
14  dai::Pipeline pipeline;
15 
16  auto xin = pipeline.create<dai::node::XLinkIn>();
17  xin->setStreamName("in");
18 
19  auto script = pipeline.create<dai::node::Script>();
20  xin->out.link(script->inputs["in"]);
21  script->setScript(R"(
22  import json
23 
24  # Receive bytes from the host
25  data = node.io['in'].get().getData()
26  jsonStr = str(data, 'utf-8')
27  dict = json.loads(jsonStr)
28 
29  # Change initial dictionary a bit
30  dict['one'] += 1
31  dict['foo'] = "baz"
32 
33  b = Buffer(30)
34  b.setData(json.dumps(dict).encode('utf-8'))
35  node.io['out'].send(b)
36  )");
37 
38  auto xout = pipeline.create<dai::node::XLinkOut>();
39  xout->setStreamName("out");
40  script->outputs["out"].link(xout->input);
41 
42  // Connect to device with pipeline
43  dai::Device device(pipeline);
44 
45  // This dict will be serialized (JSON), sent to device (Script node),
46  // edited a bit and sent back to the host
47  nlohmann::json dict{{"one", 1}, {"foo", "bar"}};
48  cout << "dict: " << dict << "\n";
49  auto buffer = dai::Buffer();
50  auto data = dict.dump();
51  buffer.setData({data.begin(), data.end()});
52  device.getInputQueue("in")->send(buffer);
53 
54  // Wait for the script to send the changed dictionary back
55  auto jsonData = device.getOutputQueue("out")->get<dai::Buffer>();
56  auto changedDict = nlohmann::json::parse(jsonData->getData());
57  cout << "changedDict: " << changedDict << "\n";
58  const nlohmann::json expectedDict{{"one", 2}, {"foo", "baz"}};
59  if(expectedDict != changedDict) return 1;
60  return 0;
61 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
DAI_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.hpp:177
nanorpc::core::type::buffer
std::vector< std::uint8_t > buffer
Definition: type.h:28
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
depthai.hpp
dai::node::Script::setScript
void setScript(const std::string &script, const std::string &name="")
Definition: Script.cpp:32
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::XLinkIn::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkIn.cpp:12
dai::node::Script
Definition: Script.hpp:15
dai::Buffer
Base message - buffer of binary data.
Definition: Buffer.hpp:13
main
int main()
Definition: script_json_communication.cpp:11
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19