pyboostcvconverter icon indicating copy to clipboard operation
pyboostcvconverter copied to clipboard

Use pbctv in embedded Python

Open linux-fan-dave opened this issue 3 years ago • 5 comments

I try to use this code in a embedded python interpreter. Embedded means that I start a programm which embeds python via boost python. The pbctv as well as the Testcode use a c++ dll inside of python instead. I use the static library and added the initialisation code to my code.

My problem is that I get an segfault whenever I use pbctv. It segfaults in the call to pyboost_cv4_converter.cpp:105 in the call to PyArray_SimpleNew. The problem seems to be that I did not initialize/import numpy correctly. I use the following code to init the libraries (init_ar is copied from the example. This init code is executed before I use the converter for the first time.

PythonAdapter::PythonAdapter()
{
    // Register the module with the interpreter
    if (PyImport_AppendInittab("embedded_extension", PyInit_embedded_extension) == -1)
	throw std::runtime_error("Failed to add embedded_extension to the interpreter's "
	         "builtin modules");
    Py_Initialize();
    boost::python::numpy::initialize();
    //using namespace XM;
    init_ar();

    //initialize converters
    boost::python::to_python_converter<cv::Mat,pbcvt::matToNDArrayBoostConverter>();
    pbcvt::matFromNDArrayBoostConverter();

}

The following code shows how I use pbctv in my code:

    pbcvt::matToNDArrayBoostConverter conv;
    m_extManager.handleImage(boost::python::object(boost::python::handle<>(conv.convert(frame))));

Is there something I'm missing? I tried as well without the call to boost::python::numpy::initialize().

I forgot to mention that I can succesfully compile and use pbctv correctly as a python plugin dll.

linux-fan-dave avatar Jul 12 '21 08:07 linux-fan-dave

I changed my code to pass cv::Mat to python and not explicitely call the converter. This works just fine without any segfault. I therefore close this issue.

linux-fan-dave avatar Jul 12 '21 08:07 linux-fan-dave

The converter was not used because the function with cv::Mat was never called. When I call this function the segfault can be observed again.

linux-fan-dave avatar Jul 12 '21 08:07 linux-fan-dave

Hi, @linux-fan-dave. Sorry you are having trouble with pbcvt. I am not currently supporting embedded python development (I'm very far away from either embedded things or OpenCV at this point in time), but you're welcome to contribute anything that helps to get this issue resolved. The only thing I can think of is: try to understand / debug why pbcvt works in a regular python setting, but segfaults in the embedded setting.

Algomorph avatar Jul 12 '21 19:07 Algomorph

where is the configure?

KingfaLuis avatar Aug 03 '22 03:08 KingfaLuis

@KingfaLuis I'm not sure what you mean by "configure". The configure script of autotools? This project uses CMake.

Algomorph avatar Aug 03 '22 23:08 Algomorph