Algomorph/pyboostcvconverter

I Call the fromMatToNDArray function,but it returns an error.

Closed this issue · 11 comments

I used pyboostcvconverter when I was using Qt. I came in with an image read by openCV and wrote the format as CV_16UC3 according to your example. I got an error when converting this image from the fromMatToNDArray function to PyObject*, the program flashed back, I don't know why this happened.

Could you please post: your operating system, OpenCV version, python version, Qt version, stack trace (if any) and error message. Without this information, I won't be able to replicate your error.
Thanks!

My operating system is Ubuntu16.04,OpenCV version is 3.4.0,python version is 2.7,Qt version is 5.11.
I have added pbcvt.so to LIBS and included pyboostcvconverter.hpp to the head file. The program is successfully compiled. But the program crashed just before the parameter CV_16UC3 Mat converts to the function "fromMatToNDArray". (I have added output command just before and just after the function to confirm that, only the output command before the function have its output.)
Qt didn't report some useful information , it told
"The process was ended forcefully."
"***.exe crashed."

I added three output commands in the “fromMatToNDArray” function, like this :

PyObject* fromMatToNDArray(const Mat& m) {
cout<<"111"<<endl;
if (!m.data)
Py_RETURN_NONE;
Mat temp,
cout<<"222"<<endl;
p = (Mat) &m;
if (!p->u || p->allocator != &g_numpyAllocator) {
temp.allocator = &g_numpyAllocator;
ERRWRAP2(m.copyTo(temp));
p = &temp;
}
cout<<"333"<<endl;
PyObject* o = (PyObject*) p->u->userdata;
Py_INCREF(o);
return o;
}

However, none of the output command was visible in the result,which might mean that even the function didn't run.

When you say you added pbcvt.so to LIBS, what exactly do you mean?
What are you trying to do, link pbcvt to another library dynamically, and then call that other library from python?

Qt program have a ***.pro file, I should added pbcvt.so to LIBS like: "LIBS += /usr/lib/pbcvt.so ", "pbcvt.so" is the file generated after cmake .
I just when to send a cv::Mat variable to a python program and change it into numpy.NDArray.
Codes is shown below:

Py_Initialize();

PyObject* moduleName = PyString_FromString("ab");
PyObject* pModule = PyImport_Import(moduleName);
PyObject* pv = PyObject_GetAttrString(pModule, "test_add");

PyObject* args = PyTuple_New(2);
Mat probe_color_change=Mat::zeros(probe_color_read.rows,probe_color_read.cols,CV_16UC3);
cout << "[INFO] ready to send probe_color." << endl;
PyObject* arg1 = pbcvt::fromMatToNDArray(probe_color_change);
cout << "[INFO] send probe_color to (test_add) succeed!!!" << endl;

PyObject* arg2 = PyInt_FromLong(3);
PyTuple_SetItem(args, 0, arg1);
PyTuple_SetItem(args, 1, arg2);

PyObject* pRet = PyObject_CallObject(pv, args);
if (pRet)
{
long result = PyInt_AsLong(pRet);
cout << "result:" << result << endl;
}
Py_Finalize(); // end

The result just have an output "[INFO] ready to send probe_color." , and then crashed.

Ah, so you're trying to link the library directly to your other program that you're making in qtcreator and then call the C API directly... I am confused about what exactly you're trying to do, but in any case, this usage is not supported.

First of all, do not dynamically link my library, it's not intended to be used this way. Instead, modify your own library that you intend to use within python, such that it becomes a Boost python module (see src/python_module.cpp for an example on how to do this). You can use my headers directly for the converter functions.

You means to let me change my C++ program ( the Qt interface) into boost python module so that python program could call it ?
But I aim to let C++ program ( the Qt interface) call a python function,and I send a picture (cv::Mat) to the python function so that it could do some mathematical operation,then the python function returns its results to my C++ program ( the Qt interface).
Is there any method I could achieve my intent ?
Thanks for your patiently help~

It sounds like you want to call python from C++, instead of the other way around... Why? Is there something available to you in python that is not available in C++?

Yeah~ I have a Face Identification algorithm in python,but I want to combine it into my Qt interface. So I have to let Qt interface call this algorithm... so...Could your library achieve my demand..?

@hongpq , my library is not for that, it's boilerplate code for calling C++ code using OpenCV from python.
Generally, what you're trying to do seems ill-advised. I would recommend either re-implementing your face identification algorithm in C++ and using it directly or writing the Qt interface in PyQt or PySide instead (in Python) directly.

OK... thank you very much.