13 #define NPY_NO_DEPRECATED_API NPY_API_VERSION 14 #include <numpy/arrayobject.h> 30 UERROR(
"Cannot initialize Python detector, the path is not valid: \"%s\"=\"%s\"",
31 Parameters::kPyDetectorPath().c_str(),
path_.c_str());
38 if(!matcherPythonDir.empty())
40 PyRun_SimpleString(
"import sys");
41 PyRun_SimpleString(
uFormat(
"sys.path.append(\"%s\")", matcherPythonDir.c_str()).c_str());
48 UDEBUG(
"PyImport_Import() beg");
50 UDEBUG(
"PyImport_Import() end");
56 UERROR(
"Module \"%s\" could not be imported! (File=\"%s\")", scriptName.c_str(),
path_.c_str());
93 UASSERT(!image.empty() && image.channels() == 1 && image.depth() == CV_8U);
94 std::vector<cv::KeyPoint> keypoints;
95 cv::Mat imgRoi(image, roi);
101 UERROR(
"Python detector module not loaded!");
109 PyObject * pFunc = PyObject_GetAttrString(
pModule_,
"init");
112 if(PyCallable_Check(pFunc))
114 PyObject * result = PyObject_CallFunction(pFunc,
"i",
cuda_?1:0);
118 UERROR(
"Call to \"init(...)\" in \"%s\" failed!",
path_.c_str());
131 UERROR(
"Cannot find method \"detect(...)\" in %s",
path_.c_str());
143 UERROR(
"Cannot call method \"init(...)\" in %s",
path_.c_str());
151 UERROR(
"Cannot find method \"init(...)\"");
160 npy_intp dims[2] = {imgRoi.rows, imgRoi.cols};
161 PyObject* pImageBuffer = PyArray_SimpleNewFromData(2, dims, NPY_UBYTE, (
void*)imgRoi.data);
164 UDEBUG(
"Preparing data time = %fs", timer.
ticks());
166 PyObject *pReturn = PyObject_CallFunctionObjArgs(
pFunc_, pImageBuffer,
NULL);
169 UERROR(
"Failed to call match() function!");
174 UDEBUG(
"Python detector time = %fs", timer.
ticks());
176 if (PyTuple_Check(pReturn) && PyTuple_GET_SIZE(pReturn) == 2)
178 PyObject *kptsPtr = PyTuple_GET_ITEM(pReturn, 0);
179 PyObject *descPtr = PyTuple_GET_ITEM(pReturn, 1);
180 if(PyArray_Check(kptsPtr) && PyArray_Check(descPtr))
182 PyArrayObject *arrayPtr =
reinterpret_cast<PyArrayObject*
>(kptsPtr);
183 int nKpts = PyArray_SHAPE(arrayPtr)[0];
184 int kptSize = PyArray_SHAPE(arrayPtr)[1];
185 int type = PyArray_TYPE(arrayPtr);
186 UDEBUG(
"Kpts array %dx%d (type=%d)", nKpts, kptSize, type);
188 UASSERT_MSG(type == NPY_FLOAT,
uFormat(
"Returned matches should type FLOAT=11, received type=%d", type).c_str());
190 float* c_out =
reinterpret_cast<float*
>(PyArray_DATA(arrayPtr));
191 keypoints.reserve(nKpts);
192 for (
int i = 0; i < nKpts*kptSize; i+=kptSize)
194 cv::KeyPoint kpt(c_out[i], c_out[i+1], 8, -1, c_out[i+2]);
195 keypoints.push_back(kpt);
198 arrayPtr =
reinterpret_cast<PyArrayObject*
>(descPtr);
199 int nDesc = PyArray_SHAPE(arrayPtr)[0];
201 int dim = PyArray_SHAPE(arrayPtr)[1];
202 type = PyArray_TYPE(arrayPtr);
203 UDEBUG(
"Desc array %dx%d (type=%d)", nDesc, dim, type);
204 UASSERT_MSG(type == NPY_FLOAT,
uFormat(
"Returned matches should type FLOAT=11, received type=%d", type).c_str());
206 c_out =
reinterpret_cast<float*
>(PyArray_DATA(arrayPtr));
207 for (
int i = 0; i < nDesc*dim; i+=dim)
209 cv::Mat descriptor = cv::Mat(1, dim, CV_32FC1, &c_out[i]).clone();
216 UWARN(
"Expected tuple (Kpts 3 x N, Descriptors dim x N), returning empty features.");
220 Py_DECREF(pImageBuffer);
GLM_FUNC_DECL genIType mask(genIType const &count)
static std::string homeDir()
static bool parse(const ParametersMap ¶meters, const std::string &key, bool &value)
PyDetector(const ParametersMap ¶meters=ParametersMap())
virtual void parseParameters(const ParametersMap ¶meters)
static std::string getDir(const std::string &filePath)
std::map< std::string, std::string > ParametersMap
Some conversion functions.
std::string getExtension()
std::string getTraceback()
#define UASSERT(condition)
Wrappers of STL for convenient functions.
std::list< std::string > uSplit(const std::string &str, char separator=' ')
#define UASSERT_MSG(condition, msg_str)
std::string UTILITE_EXP uReplaceChar(const std::string &str, char before, char after)
virtual std::vector< cv::KeyPoint > generateKeypointsImpl(const cv::Mat &image, const cv::Rect &roi, const cv::Mat &mask=cv::Mat())
ULogger class and convenient macros.
#define PyUnicode_FromString
std::string UTILITE_EXP uFormat(const char *fmt,...)
virtual void parseParameters(const ParametersMap ¶meters)
virtual cv::Mat generateDescriptorsImpl(const cv::Mat &image, std::vector< cv::KeyPoint > &keypoints) const