Error when i want to use fromMatToNDArray
Closed this issue · 6 comments
When I want to use fromMatToNDArray
to convert Mat
to PyObject
error occur!
Access violation reading location 0x0000000000000010.
and this is my code :
#include <Python.h>
#include <iostream>
#include "pyhelper.hpp"
#include <opencv2/opencv.hpp>
#include "pyboostcvconverter\pyboostcvconverter.hpp"
#include <boost/python.hpp>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_ARRAY_UNIQUE_SYMBOL pbcvt_ARRAY_API
using namespace cv;
using namespace std;
using namespace boost::python;
int main(int argc, char** argv) {
Py_Initialize();
PySys_SetArgv(argc,(wchar_t**)argv);
PyRun_SimpleString("import numpy\n");
//initialize converters
to_python_converter<cv::Mat,pbcvt::matToNDArrayBoostConverter>();
pbcvt::matFromNDArrayBoostConverter();
PyObject pName = PyUnicode_FromString("py_function");
PyObject pModule = PyImport_Import(pName);
PyObject *pArgs, *pValue;
if (pModule)
{
PyObject pFunc = PyObject_GetAttrString(pModule, "opencvTest");
if (pFunc && PyCallable_Check(pFunc))
{
Mat image = imread("test2.jpg");
pArgs = PyTuple_New(1);
PyObject pValue1 = pbcvt::fromMatToNDArray(image);
PyTuple_SetItem(pArgs, 0, pValue1);
pValue = PyObject_CallObject(pFunc, pArgs);
if (pValue)
{
cv::Mat out = pbcvt::fromNDArrayToMat(pValue);
cv::imshow("res", out);
cv::waitKey(0);
}
else
{
printf("ERROR: Returned Value is NULL\n");
}
}
else
{
printf("ERROR: function opencvTest()\n");
}
}
else
{
printf_s("ERROR: Module not imported\n");
}
std::getchar();
return 0;
}
The error raise on line 108 of pyboost_cv3_converter.cpp
, line :
PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
I have a question. Why do you have a "main" function in your C++ code? It's supposed to be a C++ library imported into Python, not a C++ executable somehow trying to use the Python API from within C++... People have been trying to use my code backwards a gazillion times, I should include a note about this in the README perhaps...
So you say I cant use this library to add python to my c++ project?
I want use embedding Python in c++.
@aligoglos
The first question that comes to my mind is "why?" Why would you ever want to go to the trouble to embed the obscure C API of python / numpy into C++ code without using the C++ code from python? What can you possibly stand to gain from this?
But maybe I misunderstand you. Maybe you do want to use your C++ code from within python?
Because I want to develop software in c++ and some part of it use some powerful python libraries (like deep learning libraries in python) so I need call python in my c++ program.
this link explain why would you want to use python in another language.
Ah, I see, that makes sense now. I'm guessing, PyTorch?
Unfortunately, Boost Python is for going the other way around. And so are the basic OpenCV-based converters you'll find in my code. I can only guess that perhaps the basic ones (which don't use Boost, ones you have to manually call to get a Mat from a Python object and vice-versa) can give you a starting point, but even they will probably need to be tweaked, since you're not using an interpreter. I see you are already using them in your code, in that case it probably doesn't make sense to try to use the Boost-based converters.
As for your particular error, I would probably try to run some basic code that calls PyArray_SimpleNew(...) on some valid arguments, see if that works first. If it does, then you'll have to figure out what are the values to the arguments in my usage by debugging, and see which of the values are invalid in this case and why.
I hope this helps!
@aligoglos , I am going to close this issue now.
If you would like, you are welcome to contribute any code to support embedded python development from C++ with the pbcvt tool.