pzo*_*pzo 5 c++ python binding pybind11
我有我自己的 C++,我正在尝试使用 Pybind11 生成 python 绑定:
auto markerParams = MarkerDetector::Params::create(MarkerType::chessboard);
markerDetector.detect(image, markerParams);
Run Code Online (Sandbox Code Playgroud)
我有为MarkerDetector::Paramsstruct生成绑定的问题,因为它是一个内部结构,使用工厂方法构造,以 enum 作为参数:
enum MarkerType { chessboard, tag };
typedef std::vector<cv::Point> ContourType;
typedef std::vector<cv::Point2d> ContourTyped;
// contour full, contour approximate, area, corners
typedef std::tuple<ContourType, ContourType, double, ContourTyped> MarkerDescriptor;
class MarkerDetector {
public:
std::vector<MarkerDescriptor> detect(Mat image, const Params params);
struct Params {
int rows, cols;
ColorRange borderColor;
ShapeDetector::Params borderShape;
cv::Size borderSize;
cv::Size Size;
static Params create(MarkerType markerType) {
static Params markerTypes[] = {
{ 3, 6, ColorRange::GREEN, ShapeDetector::Params::RECTANGLE, cv::Size(30,30), cv::Size(140, 140) }
};
return markerTypes[markerType];
}
};
};
Run Code Online (Sandbox Code Playgroud)
有谁知道如何处理这种更高级的情况?
我已经根据您的设计使用内部结构来运行您的代码的基本实现。为简洁起见,我只包含了 MarkerDetector 和 Params 的相关详细信息,但它应该与您所做的相匹配。
C++代码:
#include <pybind11/pybind11.h>
namespace py = pybind11;
enum MarkerType { chessboard, tag };
class MarkerDetector {
public:
MarkerDetector() { }
struct Params {
int rows;
int cols;
static Params create(MarkerType markerType) {
static Params markerTypes[] = {
{ 1, 2 },
{ 3, 4 }
};
return markerTypes[markerType];
}
};
};
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example"; // optional module docstring
py::enum_<MarkerType>(m, "MarkerType")
.value("chessboard", MarkerType::chessboard)
.value("tag", MarkerType::tag)
.export_values();
py::class_<MarkerDetector>(m, "MarkerDetector")
.def(py::init<>())
;
py::class_<MarkerDetector::Params>(m, "MarkerDetectorParams")
.def(py::init<>())
.def_readwrite("rows", &MarkerDetector::Params::rows)
.def_readwrite("cols", &MarkerDetector::Params::cols)
.def("create", (MarkerDetector::Params (*)(MarkerType)) &MarkerDetector::Params::create)
;
}
Run Code Online (Sandbox Code Playgroud)
(如果你有兴趣,命令行编译上面:)
c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` -L /usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/ -lpython3.6
Run Code Online (Sandbox Code Playgroud)
蟒蛇代码:
import sys
sys.path.append('/Volumes/Programs/workspaces/workspace 1/Project CPP 2')
import example
mtype = example.MarkerType.chessboard
print (mtype)
x = example.MarkerDetectorParams.create(example.MarkerType.chessboard)
print (x)
print (x.rows)
print (x.cols)
y = example.MarkerDetectorParams.create(example.MarkerType.tag)
print (y)
print (y.rows)
print (y.cols)
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出,根据设计显示正确:
MarkerType.chessboard
<example.MarkerDetectorParams object at 0x1043e35a8>
1
2
<example.MarkerDetectorParams object at 0x1043e3768>
3
4
Run Code Online (Sandbox Code Playgroud)
我希望这能给你一些工作。问候, AS
| 归档时间: |
|
| 查看次数: |
2037 次 |
| 最近记录: |