From cc50b19a5c653a2053020898e6cf93f1bbf67ed9 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Sun, 15 Jan 2017 16:37:42 -0500 Subject: [PATCH 1/2] Test opencv wrappers - Fails with symbol not found --- setup.py | 9 ++++++-- src/main.cpp | 58 ++++++++++++++++++++++++---------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/setup.py b/setup.py index 75392c9..522ec29 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,4 @@ +from os.path import basename, exists, join from setuptools import setup, Extension from setuptools.command.build_ext import build_ext import sys @@ -23,12 +24,14 @@ def __str__(self): ext_modules = [ Extension( - 'python_example', + 'test_package', ['src/main.cpp'], include_dirs=[ # Path to pybind11 headers get_pybind_include(), - get_pybind_include(user=True) + get_pybind_include(user=True), + "include", + "/home/virtuald/src/frc/ext/opencv-python/opencv/build/prefix/include", ], language='c++' ), @@ -80,6 +83,8 @@ def build_extensions(self): opts = self.c_opts.get(ct, []) if ct == 'unix': opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version()) + opts.append('-s') # strip + opts.append('-g0') opts.append(cpp_flag(self.compiler)) if has_flag(self.compiler, '-fvisibility=hidden'): opts.append('-fvisibility=hidden') diff --git a/src/main.cpp b/src/main.cpp index d81046d..186081b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,40 +1,36 @@ #include +namespace py = pybind11; -int add(int i, int j) { - return i + j; -} - -int subtract(int i, int j) { - return i - j; +#include +#include +#include + +PyObject * draw_rect(PyObject * in) { + + cv::Mat mat; + + if (!PyOpenCV_NdarrayToMat(in, mat, "in")) + return NULL; + + cv::Point pt1, pt2; + pt1.x = 10; + pt1.y = 10; + pt2.x = 100; + pt2.y = 100; + + rectangle(mat, pt1, pt2, cv::Scalar(255, 0, 0)); + + return PyOpenCV_MatToNdarray(mat); } -namespace py = pybind11; PYBIND11_PLUGIN(python_example) { - py::module m("python_example", R"pbdoc( - Pybind11 example plugin - ----------------------- - - .. currentmodule:: python_example - - .. autosummary:: - :toctree: _generate - - add - subtract - )pbdoc"); - - m.def("add", &add, R"pbdoc( - Add two numbers - - Some other explanation about the add function. - )pbdoc"); - - m.def("subtract", &subtract, R"pbdoc( - Subtract two numbers - - Some other explanation about the subtract function. - )pbdoc"); + + import_opencv(); + + py::module m("python_example"); + + m.def("draw_rect", &draw_rect); #ifdef VERSION_INFO m.attr("__version__") = py::str(VERSION_INFO); From b9796e37d108bb8cf51c08ed94ceb7c7eaa1e5a0 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Thu, 19 Jan 2017 00:13:42 -0500 Subject: [PATCH 2/2] Changes --- setup.py | 6 +++++- src/main.cpp | 11 ++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 522ec29..ce63843 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,7 @@ def __str__(self): import pybind11 return pybind11.get_include(self.user) +import cv2 ext_modules = [ Extension( @@ -33,7 +34,10 @@ def __str__(self): "include", "/home/virtuald/src/frc/ext/opencv-python/opencv/build/prefix/include", ], - language='c++' + library_dirs=[cv2.distutils.get_lib_path()], + libraries=['opencv_core', 'opencv_imgproc'], + language='c++', + runtime_library_dirs=['$ORIGIN/cv2'], ), ] diff --git a/src/main.cpp b/src/main.cpp index 186081b..32e2edb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,16 @@ #include +#include namespace py = pybind11; #include #include #include -PyObject * draw_rect(PyObject * in) { +PyObject * draw_rect(py::array in) { cv::Mat mat; - if (!PyOpenCV_NdarrayToMat(in, mat, "in")) + if (!PyOpenCV_NdarrayToMat(in.ptr(), mat, "in")) return NULL; cv::Point pt1, pt2; @@ -24,11 +25,10 @@ PyObject * draw_rect(PyObject * in) { } -PYBIND11_PLUGIN(python_example) { +PYBIND11_PLUGIN(test_package) { - import_opencv(); - py::module m("python_example"); + py::module m("test_package"); m.def("draw_rect", &draw_rect); @@ -38,5 +38,6 @@ PYBIND11_PLUGIN(python_example) { m.attr("__version__") = py::str("dev"); #endif + import_opencv(); return m.ptr(); }