2016-08-07 00:46:17 +08:00
|
|
|
#ifdef HAVE_OPENCV_FLANN
|
|
|
|
typedef cvflann::flann_distance_t cvflann_flann_distance_t;
|
|
|
|
typedef cvflann::flann_algorithm_t cvflann_flann_algorithm_t;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const cvflann_flann_algorithm_t& value)
|
|
|
|
{
|
|
|
|
return PyInt_FromLong(int(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const cvflann_flann_distance_t& value)
|
|
|
|
{
|
|
|
|
return PyInt_FromLong(int(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-11-15 22:29:51 +08:00
|
|
|
bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const ArgInfo& info)
|
2016-08-07 00:46:17 +08:00
|
|
|
{
|
2017-03-10 21:09:31 +08:00
|
|
|
if (!o || o == Py_None)
|
2023-07-21 17:44:56 +08:00
|
|
|
{
|
2017-03-10 21:09:31 +08:00
|
|
|
return true;
|
2023-07-21 17:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!PyDict_Check(o))
|
|
|
|
{
|
|
|
|
failmsg("Argument '%s' is not a dictionary", info.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* key_obj = NULL;
|
|
|
|
PyObject* value_obj = NULL;
|
|
|
|
Py_ssize_t key_pos = 0;
|
2017-03-10 21:09:31 +08:00
|
|
|
|
2023-07-21 17:44:56 +08:00
|
|
|
while(PyDict_Next(o, &key_pos, &key_obj, &value_obj))
|
|
|
|
{
|
|
|
|
// get key
|
|
|
|
std::string key;
|
|
|
|
if (!getUnicodeString(key_obj, key))
|
2019-03-21 15:45:02 +08:00
|
|
|
{
|
2023-07-21 17:44:56 +08:00
|
|
|
failmsg("Key at pos %lld is not a string", static_cast<int64_t>(key_pos));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// key_arg_info.name is bound to key lifetime
|
|
|
|
const ArgInfo key_arg_info(key.c_str(), false);
|
|
|
|
|
|
|
|
// get value
|
|
|
|
if (isBool(value_obj))
|
|
|
|
{
|
|
|
|
npy_bool npy_value = NPY_FALSE;
|
|
|
|
if (PyArray_BoolConverter(value_obj, &npy_value) >= 0)
|
2019-03-21 15:45:02 +08:00
|
|
|
{
|
2023-07-21 17:44:56 +08:00
|
|
|
p.setBool(key, npy_value == NPY_TRUE);
|
|
|
|
continue;
|
2016-08-07 00:46:17 +08:00
|
|
|
}
|
2023-07-21 17:44:56 +08:00
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int int_value = 0;
|
|
|
|
if (pyopencv_to(value_obj, int_value, key_arg_info))
|
|
|
|
{
|
|
|
|
if (key == "algorithm")
|
2016-08-07 00:46:17 +08:00
|
|
|
{
|
2023-07-21 17:44:56 +08:00
|
|
|
p.setAlgorithm(int_value);
|
2019-03-21 15:45:02 +08:00
|
|
|
}
|
2023-07-21 17:44:56 +08:00
|
|
|
else
|
2016-08-07 00:46:17 +08:00
|
|
|
{
|
2023-07-21 17:44:56 +08:00
|
|
|
p.setInt(key, int_value);
|
2016-08-07 00:46:17 +08:00
|
|
|
}
|
2023-07-21 17:44:56 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
double flt_value = 0.0;
|
|
|
|
if (pyopencv_to(value_obj, flt_value, key_arg_info))
|
|
|
|
{
|
|
|
|
if (key == "eps")
|
2016-08-07 00:46:17 +08:00
|
|
|
{
|
2023-07-21 17:44:56 +08:00
|
|
|
p.setFloat(key, static_cast<float>(flt_value));
|
2016-08-07 00:46:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-21 17:44:56 +08:00
|
|
|
p.setDouble(key, flt_value);
|
2016-08-07 00:46:17 +08:00
|
|
|
}
|
2023-07-21 17:44:56 +08:00
|
|
|
continue;
|
2016-08-07 00:46:17 +08:00
|
|
|
}
|
2023-07-21 17:44:56 +08:00
|
|
|
PyErr_Clear();
|
2016-08-07 00:46:17 +08:00
|
|
|
|
2023-07-21 17:44:56 +08:00
|
|
|
std::string str_value;
|
|
|
|
if (getUnicodeString(value_obj, str_value))
|
|
|
|
{
|
|
|
|
p.setString(key, str_value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
// All conversions are failed
|
|
|
|
failmsg("Failed to parse IndexParam with key '%s'. "
|
|
|
|
"Supported types: [bool, int, float, str]", key.c_str());
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
2016-08-07 00:46:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-11-15 22:29:51 +08:00
|
|
|
bool pyopencv_to(PyObject* obj, cv::flann::SearchParams & value, const ArgInfo& info)
|
2016-08-07 00:46:17 +08:00
|
|
|
{
|
2019-11-15 22:29:51 +08:00
|
|
|
return pyopencv_to<cv::flann::IndexParams>(obj, value, info);
|
2016-08-07 00:46:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-11-15 22:29:51 +08:00
|
|
|
bool pyopencv_to(PyObject *o, cvflann::flann_distance_t& dist, const ArgInfo& info)
|
2016-08-07 00:46:17 +08:00
|
|
|
{
|
|
|
|
int d = (int)dist;
|
2019-11-15 22:29:51 +08:00
|
|
|
bool ok = pyopencv_to(o, d, info);
|
2016-08-07 00:46:17 +08:00
|
|
|
dist = (cvflann::flann_distance_t)d;
|
|
|
|
return ok;
|
|
|
|
}
|
2019-03-21 15:45:02 +08:00
|
|
|
#endif
|