boost-python

C++ conversion from NumPy array to Mat (OpenCV)

扶醉桌前 提交于 2019-11-26 22:25:05
问题 I am writing a thin wrapper around ArUco augmented reality library (which is based on OpenCV). An interface I am trying to build is very simple: Python passes image to C++ code; C++ code detects markers and returns their locations and other info to Python as tuple of dicts. However, I couldn't figure out how to represent an image in Python to pass it to C++. For GUI and camera management I am going to use PyQt, so initially it is going to be QImage, but I can't simply pass it to OpenCV (or I

how to return numpy.array from boost::python?

China☆狼群 提交于 2019-11-26 22:19:06
I would like to return some data from c++ code as a numpy.array object. I had a look at boost::python::numeric , but its documentation is very terse. Can I get an example of e.g. returning a (not very large) vector<double> to python? I don't mind doing copies of data. UPDATE: the library described in my original answer ( https://github.com/ndarray/Boost.NumPy ) has been integrated directly into Boost.Python as of Boost 1.63, and hence the standalone version is now deprecated. The text below now corresponds to the new, integrated version (only the namespace has changed). Boost.Python now

Boost.Python: Wrap functions to release the GIL

别来无恙 提交于 2019-11-26 20:47:33
问题 I am currently working with Boost.Python and would like some help to solve a tricky problem. Context When a C++ method/function is exposed to Python, it needs to release the GIL (Global Interpreter Lock) to let other threads use the interpreter. This way, when the python code calls a C++ function, the interpreter can be used by other threads. For now, each C++ function looks like this: // module.cpp int myfunction(std::string question) { ReleaseGIL unlockGIL; return 42; } To pass it to boost

Python embedded in CPP: how to get data back to CPP

与世无争的帅哥 提交于 2019-11-26 20:39:43
问题 While working on a C++ project, I was looking for a third party library for something that is not my core business. I found a really good library, doing exactly what's needed, but it is written in Python. I decided to experiment with embedding Python code in C++, using the Boost.Python library. The C++ code looks something like this: #include <string> #include <iostream> #include <boost/python.hpp> using namespace boost::python; int main(int, char **) { Py_Initialize(); try { object module(

std::vector to boost::python::list

岁酱吖の 提交于 2019-11-26 19:46:37
I have a method in c++ that gets called from python and needs to return a python list object. I have already created the method, and its attached to an exposed class and callable from python right now... (it returns void). So the question is, how do I create a python list from this: std::vector<std::string> results; I am not really understanding how the constructor works from this documentation: http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/list.html Also... I don't really want to return kind of wrapped vector... I just want to create a new python list with the string values from the

Boost-python How to pass a c++ class instance to a python class

六月ゝ 毕业季﹏ 提交于 2019-11-26 19:25:57
问题 I am new to boost python. I have to first init a cpp class instance in cpp code, and then pass this cpp instance to python code, use a python class instance to invoke it(the cpp instance). I have tried the Python/C API way, but failed, so I wonder how to pass a c++ class instance to a python class. The following is my code, changed from the boost python demo. in main.cpp #include <python2.6/Python.h> #include <boost/python.hpp> #include <iostream> using namespace boost::python; using

Writing Python bindings for C++ code that use OpenCV

自作多情 提交于 2019-11-26 18:57:13
问题 I'm trying to write a python wrapper for some C++ code that make use of OpenCV but I'm having difficulties returning the result, which is a OpenCV C++ Mat object, to the python interpreter. I've looked at OpenCV's source and found the file cv2.cpp which has conversions functions to perform conversions to and fro between PyObject* and OpenCV's Mat. I made use of those conversions functions but got a segmentation fault when I tried to use them. I basically need some suggestions/sample code

Boost.Python call by reference : TypeError: No to_python (by-value) converter found for C++ type:

自作多情 提交于 2019-11-26 16:49:32
问题 I'm trying to expose my C++ Classes to Python using Boost.Python. Here is a simplyfied version of what i'm trying to do: I have a class A deriving from boost::noncopyable and a second class B with a method that takes a reference to A as an argument. class A : boost::noncopyable { /*...*/ }; class B { public: virtual void do_something(A& a) { /*...*/ } }; I'm exposing the classes as follows: /* Wrapper for B, so B can be extended in python */ struct BWrap : public B, wrapper<B> { void do

Feeding a Python list into a function taking in a vector with Boost Python

妖精的绣舞 提交于 2019-11-26 15:38:26
问题 I've got a function with the signature: function(std::vector<double> vector); And I've exposed it, but it doesn't take in Python lists. I've looked through the other SO answers, and most involve changing the function to take in boost::python::lists, but I don't want to change the function. I imagine I can use the vector_indexing_suite to write a simple wrapper around this function, but I have many functions of this form and would rather not write a wrapper for every single one. Is there a way

boost.python not supporting parallelism?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 11:16:42
问题 I am trying to wrap a piece of C++ code into python lib using boost.python, however, I found out that multiple instances cannot run at the same time: code (C++): class Foo{ public: Foo(){} void run(){ int seconds = 2; clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } }; BOOST_PYTHON_MODULE(run_test) { using namespace boost::python; class_<Foo>(\"test\", init<>()) .def(\"run\", &Foo::run) ; } which is compile using CMake (CMake): add_library(run