-
Notifications
You must be signed in to change notification settings - Fork 131
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 #917 from luxonis/message_groups
Message groups
- Loading branch information
Showing
25 changed files
with
680 additions
and
17 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
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
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,63 @@ | ||
#include <chrono> | ||
#include <iostream> | ||
|
||
#include "depthai/depthai.hpp" | ||
|
||
int main() { | ||
dai::Pipeline pipeline; | ||
|
||
auto script1 = pipeline.create<dai::node::Script>(); | ||
script1->setScript( | ||
R"SCRPT( | ||
from time import sleep | ||
while True: | ||
sleep(1) | ||
b = Buffer(512) | ||
b.setData(bytes(4 * [i for i in range(0, 128)])) | ||
b.setTimestamp(Clock.now()) | ||
node.io['out'].send(b) | ||
)SCRPT"); | ||
|
||
auto script2 = pipeline.create<dai::node::Script>(); | ||
script2->setScript( | ||
R"SCRPT( | ||
from time import sleep | ||
while True: | ||
sleep(0.3) | ||
b = Buffer(512) | ||
b.setData(bytes(4 * [i for i in range(128, 256)])) | ||
b.setTimestamp(Clock.now()) | ||
node.io['out'].send(b) | ||
)SCRPT"); | ||
|
||
auto sync = pipeline.create<dai::node::Sync>(); | ||
sync->setSyncThreshold(std::chrono::milliseconds(100)); | ||
|
||
auto demux = pipeline.create<dai::node::MessageDemux>(); | ||
|
||
auto xout1 = pipeline.create<dai::node::XLinkOut>(); | ||
xout1->setStreamName("xout1"); | ||
auto xout2 = pipeline.create<dai::node::XLinkOut>(); | ||
xout2->setStreamName("xout2"); | ||
|
||
script1->outputs["out"].link(sync->inputs["s1"]); | ||
script2->outputs["out"].link(sync->inputs["s2"]); | ||
sync->out.link(demux->input); | ||
demux->outputs["s1"].link(xout1->input); | ||
demux->outputs["s2"].link(xout2->input); | ||
|
||
dai::Device device(pipeline); | ||
std::cout << "Start" << std::endl; | ||
auto queue1 = device.getOutputQueue("xout1", 10, true); | ||
auto queue2 = device.getOutputQueue("xout2", 10, true); | ||
while(true) { | ||
auto bufS1 = queue1->get<dai::Buffer>(); | ||
auto bufS2 = queue2->get<dai::Buffer>(); | ||
std::cout << "Buffer 1 timestamp: " << bufS1->getTimestamp().time_since_epoch().count() << std::endl; | ||
std::cout << "Buffer 2 timestamp: " << bufS2->getTimestamp().time_since_epoch().count() << std::endl; | ||
std::cout << "----------" << std::endl; | ||
std::this_thread::sleep_for(std::chrono::milliseconds(200)); | ||
} | ||
} |
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,68 @@ | ||
#include <iostream> | ||
|
||
// Includes common necessary includes for development using depthai library | ||
#include "depthai/depthai.hpp" | ||
|
||
int main() { | ||
// Create pipeline | ||
dai::Pipeline pipeline; | ||
|
||
// Define sources and outputs | ||
auto monoLeft = pipeline.create<dai::node::MonoCamera>(); | ||
auto monoRight = pipeline.create<dai::node::MonoCamera>(); | ||
auto color = pipeline.create<dai::node::ColorCamera>(); | ||
auto stereo = pipeline.create<dai::node::StereoDepth>(); | ||
auto sync = pipeline.create<dai::node::Sync>(); | ||
|
||
auto xoutGrp = pipeline.create<dai::node::XLinkOut>(); | ||
|
||
// XLinkOut | ||
xoutGrp->setStreamName("xout"); | ||
|
||
// Properties | ||
monoLeft->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P); | ||
monoLeft->setCamera("left"); | ||
monoRight->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P); | ||
monoRight->setCamera("right"); | ||
|
||
stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_ACCURACY); | ||
|
||
color->setCamera("color"); | ||
|
||
sync->setSyncThreshold(std::chrono::milliseconds(100)); | ||
|
||
// Linking | ||
monoLeft->out.link(stereo->left); | ||
monoRight->out.link(stereo->right); | ||
|
||
stereo->disparity.link(sync->inputs["disparity"]); | ||
color->video.link(sync->inputs["video"]); | ||
|
||
sync->out.link(xoutGrp->input); | ||
|
||
// Connect to device and start pipeline | ||
dai::Device device(pipeline); | ||
|
||
auto queue = device.getOutputQueue("xout", 10, true); | ||
|
||
float disparityMultiplier = 255 / stereo->initialConfig.getMaxDisparity(); | ||
|
||
while(true) { | ||
auto msgGrp = queue->get<dai::MessageGroup>(); | ||
for(auto& frm : *msgGrp) { | ||
auto imgFrm = std::dynamic_pointer_cast<dai::ImgFrame>(frm.second); | ||
cv::Mat img = imgFrm->getCvFrame(); | ||
if(frm.first == "disparity") { | ||
img.convertTo(img, CV_8UC1, disparityMultiplier); // Extend disparity range | ||
cv::applyColorMap(img, img, cv::COLORMAP_JET); | ||
} | ||
cv::imshow(frm.first, img); | ||
} | ||
|
||
int key = cv::waitKey(1); | ||
if(key == 'q' || key == 'Q') { | ||
return 0; | ||
} | ||
} | ||
return 0; | ||
} |
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,56 @@ | ||
#include <chrono> | ||
#include <iostream> | ||
|
||
#include "depthai/depthai.hpp" | ||
|
||
int main() { | ||
dai::Pipeline pipeline; | ||
|
||
auto script1 = pipeline.create<dai::node::Script>(); | ||
script1->setScript( | ||
R"SCRPT( | ||
from time import sleep | ||
while True: | ||
sleep(1) | ||
b = Buffer(512) | ||
b.setData(bytes(4 * [i for i in range(0, 128)])) | ||
b.setTimestamp(Clock.now()) | ||
node.io['out'].send(b) | ||
)SCRPT"); | ||
|
||
auto script2 = pipeline.create<dai::node::Script>(); | ||
script2->setScript( | ||
R"SCRPT( | ||
from time import sleep | ||
while True: | ||
sleep(0.3) | ||
b = Buffer(512) | ||
b.setData(bytes(4 * [i for i in range(128, 256)])) | ||
b.setTimestamp(Clock.now()) | ||
node.io['out'].send(b) | ||
)SCRPT"); | ||
|
||
auto sync = pipeline.create<dai::node::Sync>(); | ||
sync->setSyncThreshold(std::chrono::milliseconds(100)); | ||
|
||
auto xout = pipeline.create<dai::node::XLinkOut>(); | ||
xout->setStreamName("xout"); | ||
|
||
sync->out.link(xout->input); | ||
script1->outputs["out"].link(sync->inputs["s1"]); | ||
script2->outputs["out"].link(sync->inputs["s2"]); | ||
|
||
dai::Device device(pipeline); | ||
std::cout << "Start" << std::endl; | ||
auto queue = device.getOutputQueue("xout", 10, true); | ||
while(true) { | ||
auto grp = queue->get<dai::MessageGroup>(); | ||
std::cout << "Buffer 1 timestamp: " << grp->get<dai::Buffer>("s1")->getTimestamp().time_since_epoch().count() << std::endl; | ||
std::cout << "Buffer 2 timestamp: " << grp->get<dai::Buffer>("s2")->getTimestamp().time_since_epoch().count() << std::endl; | ||
std::cout << "Time interval between messages: " << static_cast<double>(grp->getIntervalNs()) / 1e6 << "ms" << std::endl; | ||
std::cout << "----------" << std::endl; | ||
std::this_thread::sleep_for(std::chrono::milliseconds(200)); | ||
} | ||
} |
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,79 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <memory> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "depthai-shared/datatype/RawMessageGroup.hpp" | ||
#include "depthai/pipeline/datatype/Buffer.hpp" | ||
|
||
namespace dai { | ||
|
||
/** | ||
* MessageGroup message. Carries multiple messages in one. | ||
*/ | ||
class MessageGroup : public Buffer { | ||
std::shared_ptr<RawBuffer> serialize() const override; | ||
RawMessageGroup& rawGrp; | ||
std::unordered_map<std::string, std::shared_ptr<ADatatype>> group; | ||
|
||
public: | ||
/// Construct MessageGroup message | ||
MessageGroup(); | ||
explicit MessageGroup(std::shared_ptr<RawMessageGroup> ptr); | ||
virtual ~MessageGroup() = default; | ||
|
||
/// Group | ||
std::shared_ptr<ADatatype> operator[](const std::string& name); | ||
template <typename T> | ||
std::shared_ptr<T> get(const std::string& name) { | ||
return std::dynamic_pointer_cast<T>(group[name]); | ||
} | ||
void add(const std::string& name, const std::shared_ptr<ADatatype>& value); | ||
template <typename T> | ||
void add(const std::string& name, const T& value) { | ||
static_assert(std::is_base_of<ADatatype, T>::value, "T must derive from ADatatype"); | ||
group[name] = std::make_shared<T>(value); | ||
rawGrp.group[name] = {value.getRaw(), 0}; | ||
} | ||
|
||
// Iterators | ||
std::unordered_map<std::string, std::shared_ptr<ADatatype>>::iterator begin(); | ||
std::unordered_map<std::string, std::shared_ptr<ADatatype>>::iterator end(); | ||
|
||
/** | ||
* True if all messages in the group are in the interval | ||
* @param thresholdNs Maximal interval between messages | ||
*/ | ||
bool isSynced(int64_t thresholdNs) const; | ||
|
||
/** | ||
* Retrieves interval between the first and the last message in the group. | ||
*/ | ||
int64_t getIntervalNs() const; | ||
|
||
int64_t getNumMessages() const; | ||
|
||
/** | ||
* Gets the names of messages in the group | ||
*/ | ||
std::vector<std::string> getMessageNames() const; | ||
|
||
/** | ||
* Sets image timestamp related to dai::Clock::now() | ||
*/ | ||
MessageGroup& setTimestamp(std::chrono::time_point<std::chrono::steady_clock, std::chrono::steady_clock::duration> timestamp); | ||
|
||
/** | ||
* Sets image timestamp related to dai::Clock::now() | ||
*/ | ||
MessageGroup& setTimestampDevice(std::chrono::time_point<std::chrono::steady_clock, std::chrono::steady_clock::duration> timestamp); | ||
|
||
/** | ||
* Retrieves image sequence number | ||
*/ | ||
MessageGroup& setSequenceNum(int64_t sequenceNum); | ||
}; | ||
|
||
} // namespace dai |
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
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,28 @@ | ||
#pragma once | ||
|
||
#include "depthai-shared/properties/MessageDemuxProperties.hpp" | ||
#include "depthai/pipeline/Node.hpp" | ||
|
||
namespace dai { | ||
namespace node { | ||
|
||
class MessageDemux : public NodeCRTP<Node, MessageDemux, MessageDemuxProperties> { | ||
public: | ||
constexpr static const char* NAME = "MessageDemux"; | ||
MessageDemux(const std::shared_ptr<PipelineImpl>& par, int64_t nodeId); | ||
|
||
MessageDemux(const std::shared_ptr<PipelineImpl>& par, int64_t nodeId, std::unique_ptr<Properties> props); | ||
|
||
/** | ||
* Input message of type MessageGroup | ||
*/ | ||
Input input{*this, "input", Input::Type::SReceiver, {{DatatypeEnum::MessageGroup, false}}}; | ||
|
||
/** | ||
* A map of outputs, where keys are same as in the input MessageGroup | ||
*/ | ||
OutputMap outputs; | ||
}; | ||
|
||
} // namespace node | ||
} // namespace dai |
Oops, something went wrong.