Skip to content

Commit 4ac79a9

Browse files
author
Matevz Morato
committed
Fix up the POC
1 parent d4cdf6b commit 4ac79a9

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

python_host_node.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
import time
33

44
class Printer(dai.HostNode):
5+
def __init__(self):
6+
dai.HostNode.__init__(self)
7+
print("I'm created - a new Printer object")
8+
59
def run(self):
610
print("hello world")
711

812
p = dai.Pipeline()
9-
p.create(Printer)
13+
printer = Printer()
14+
a = p.add(printer)
1015
p.start()
1116
p.wait()

src/pipeline/PipelineBindings.cpp

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
// depthai/
3838
#include "depthai/properties/GlobalProperties.hpp"
39+
#include <memory>
3940

4041
std::shared_ptr<dai::Node> createNode(dai::Pipeline& p, py::object class_){
4142
auto nodeCreateMap = NodeBindings::getNodeCreateMap();
@@ -87,7 +88,8 @@ void PipelineBindings::bind(pybind11::module& m, void* pCallstack){
8788
pipeline
8889
.def(py::init<>(), DOC(dai, Pipeline, Pipeline))
8990
//.def(py::init<const Pipeline&>())
90-
.def("getGlobalProperties", &Pipeline::getGlobalProperties, DOC(dai, Pipeline, getGlobalProperties))
91+
.def("getGlobalProperties", &Pipeline::getGlobalProperties,
92+
DOC(dai, Pipeline, getGlobalProperties))
9193
//.def("create", &Pipeline::create<node::XLinkIn>)
9294
.def("remove", &Pipeline::remove, py::arg("node"), DOC(dai, Pipeline, remove))
9395
.def("getAllNodes", static_cast<std::vector<std::shared_ptr<Node>> (Pipeline::*)() const>(&Pipeline::getAllNodes), DOC(dai, Pipeline, getAllNodes))
@@ -116,27 +118,47 @@ void PipelineBindings::bind(pybind11::module& m, void* pCallstack){
116118
.def("setBoardConfig", &Pipeline::setBoardConfig, DOC(dai, Pipeline, setBoardConfig))
117119
.def("getBoardConfig", &Pipeline::getBoardConfig, DOC(dai, Pipeline, getBoardConfig))
118120
// 'Template' create function
119-
.def("create", [](dai::Pipeline& p, py::object class_) {
120-
if (py::cast<std::string>(class_.attr("__base__").attr("__name__")) == "HostNode") {
121-
#if 0
122-
py::cast<std::shared_ptr<HostNode>>(class_())->run();
123-
#else
124-
auto host_node = py::cast<std::shared_ptr<HostNode>>(class_());
125-
//std::shared_ptr<HostNode> host_node = py::cast<std::shared_ptr<HostNode>>(class_());
126-
//std::shared_ptr<HostNode> host_node = class_().cast<std::shared_ptr<HostNode>>();
127-
host_node->run();
128-
#endif
129-
130-
//p.add(host_node);
131-
//return (std::shared_ptr<Node>) host_node;
132-
return (std::shared_ptr<Node>) nullptr;
133-
}
134-
auto node = createNode(p, class_);
135-
if(node == nullptr){
136-
throw std::invalid_argument(std::string(py::str(class_)) + " is not a subclass of depthai.node");
137-
}
138-
return node;
139-
})
121+
.def("add",
122+
[](Pipeline &p, std::shared_ptr<Node> hostNode) {
123+
p.add(hostNode);
124+
// TODO(Morato) TMP TMP only a test
125+
if (std::dynamic_pointer_cast<HostNode>(hostNode) != nullptr) {
126+
std::dynamic_pointer_cast<HostNode>(hostNode)->run();
127+
}
128+
})
129+
// 'Template' create function
130+
.def("create",
131+
[](dai::Pipeline &p, py::object class_) {
132+
// TODO(zimen) re-introduce create function for custom host nodes
133+
// if
134+
// (py::cast<std::string>(class_.attr("__base__").attr("__name__"))
135+
// == "HostNode") { Call the constructor of the class auto
136+
// host_node =
137+
// py::cast<std::shared_ptr<Node>>(class_.attr("__new__")(class_));
138+
// std::cout << py::str(class_.attr("a")) << std::endl;
139+
// class_().attr("run")();
140+
// auto host_node = py::cast<std::shared_ptr<Node>>(class_());
141+
// std::shared_ptr<HostNode> host_node =
142+
// py::cast<std::shared_ptr<HostNode>>(class_());
143+
// std::shared_ptr<HostNode> host_node =
144+
// class_().cast<std::shared_ptr<HostNode>>();
145+
// host_node->run();
146+
// return class_();
147+
148+
// p.add(host_node);
149+
// return (std::shared_ptr<Node>) host_node;
150+
// return (std::shared_ptr<Node>) nullptr;
151+
// }
152+
auto node = createNode(p, class_);
153+
if (node == nullptr) {
154+
throw std::invalid_argument(
155+
std::string(py::str(class_)) +
156+
" is not a subclass of depthai.node");
157+
}
158+
// Cast the node to a py::object
159+
py::object obj = py::cast(node);
160+
return obj;
161+
})
140162
// TODO(themarpe) DEPRECATE, use pipeline.create([class name])
141163
// templated create<NODE> function
142164
.def("createXLinkIn", &Pipeline::create<node::XLinkIn>)
@@ -166,8 +188,11 @@ void PipelineBindings::bind(pybind11::module& m, void* pCallstack){
166188
// .def("createCamera", &Pipeline::create<node::Camera>)
167189
// .def("createWarp", &Pipeline::create<node::Warp>)
168190
.def("start", &Pipeline::start)
169-
.def("wait", &Pipeline::wait)
170-
;
191+
.def("wait", [](Pipeline &p) {
192+
py::gil_scoped_release release;
193+
p.wait();
194+
});
195+
;
171196

172197

173198
}

0 commit comments

Comments
 (0)