-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from luxonis/json_comm_example
Added json communication example
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <chrono> | ||
#include <iostream> | ||
#include <thread> | ||
|
||
// Includes common necessary includes for development using depthai library | ||
#include "depthai/depthai.hpp" | ||
|
||
// Include nlohmann json | ||
#include "nlohmann/json.hpp" | ||
|
||
int main() { | ||
using namespace std; | ||
|
||
dai::Pipeline pipeline; | ||
|
||
auto xin = pipeline.create<dai::node::XLinkIn>(); | ||
xin->setStreamName("in"); | ||
|
||
auto script = pipeline.create<dai::node::Script>(); | ||
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) | ||
)"); | ||
|
||
auto xout = pipeline.create<dai::node::XLinkOut>(); | ||
xout->setStreamName("out"); | ||
script->outputs["out"].link(xout->input); | ||
|
||
// Connect to device with pipeline | ||
dai::Device device(pipeline); | ||
|
||
nlohmann::json dict{{"one", 1}, {"foo", "bar"}}; | ||
auto buffer = dai::Buffer(); | ||
auto data = dict.dump(); | ||
buffer.setData({data.begin(), data.end()}); | ||
device.getInputQueue("in")->send(buffer); | ||
|
||
auto jsonData = device.getOutputQueue("out")->get<dai::Buffer>(); | ||
auto changedDict = nlohmann::json::parse(jsonData->getData()); | ||
cout << "changedDict: " << changedDict << "\n"; | ||
|
||
return 0; | ||
} |