12 #define NPY_NO_DEPRECATED_API NPY_API_VERSION
13 #include <numpy/arrayobject.h>
32 pybind11::gil_scoped_acquire acquire;
47 std::string previousPath =
path_;
55 pybind11::gil_scoped_acquire acquire;
59 if(!previousPath.empty() && previousPath.compare(
path_)!=0)
61 UDEBUG(
"we changed script (old=%s), we need to reload (new=%s)",
62 previousPath.c_str(),
path_.c_str());
81 if(!matcherPythonDir.empty())
83 PyRun_SimpleString(
"import sys");
84 PyRun_SimpleString(
uFormat(
"sys.path.append(\"%s\")", matcherPythonDir.c_str()).c_str());
91 UDEBUG(
"PyImport_Import() beg");
93 UDEBUG(
"PyImport_Import() end");
99 UERROR(
"Module \"%s\" could not be imported! (File=\"%s\")", scriptName.c_str(),
path_.c_str());
104 PyObject * pFunc = PyObject_GetAttrString(
pModule_,
"init");
107 if(PyCallable_Check(pFunc))
109 PyObject *
result = PyObject_CallFunction(pFunc,
"i",
dim_);
113 UERROR(
"Call to \"init(...)\" in \"%s\" failed!",
path_.c_str());
125 UERROR(
"Cannot find method \"extract(...)\" in %s",
path_.c_str());
136 UERROR(
"Cannot call method \"init(...)\" in %s",
path_.c_str());
143 UERROR(
"Cannot find method \"init(...)\"");
159 UERROR(
"Python module not loaded!");
165 UERROR(
"Python function not loaded!");
169 pybind11::gil_scoped_acquire acquire;
171 if(!
data.imageRaw().empty())
173 std::vector<unsigned char> descriptorsQueryV(
data.imageRaw().total()*
data.imageRaw().channels());
174 memcpy(descriptorsQueryV.data(),
data.imageRaw().data,
data.imageRaw().total()*
data.imageRaw().channels()*
sizeof(
char));
175 npy_intp dimsFrom[3] = {
data.imageRaw().rows,
data.imageRaw().cols,
data.imageRaw().channels()};
176 PyObject* pImageQuery = PyArray_SimpleNewFromData(3, dimsFrom, NPY_BYTE, (
void*)
data.imageRaw().data);
179 UDEBUG(
"Preparing data time = %fs",
timer.ticks());
181 PyObject *pReturn = PyObject_CallFunctionObjArgs(
pFunc_, pImageQuery,
NULL);
184 UERROR(
"Failed to call extract() function!");
189 UDEBUG(
"Python extraction time = %fs",
timer.ticks());
191 PyArrayObject *np_ret =
reinterpret_cast<PyArrayObject*
>(pReturn);
194 int len1 = PyArray_SHAPE(np_ret)[0];
195 int dim = PyArray_SHAPE(np_ret)[1];
196 int type = PyArray_TYPE(np_ret);
201 float* d_out =
reinterpret_cast<float*
>(PyArray_DATA(np_ret));
209 Py_DECREF(pImageQuery);
213 UERROR(
"Invalid inputs! Missing image.");