1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 07:13:02 +04:00

Merge pull request #24028 from VadimLevin:dev/vlevin/fix-flann-python-bindings

Fix FLANN python bindings #24028

As a side-effect this patch improves reporting errors by FLANN `get_param`.

resolves #21642

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Vadim Levin
2023-07-21 12:44:56 +03:00
committed by GitHub
parent 09d2f4ea46
commit e3cb5f80e7
7 changed files with 148 additions and 45 deletions
+70 -38
View File
@@ -17,57 +17,89 @@ PyObject* pyopencv_from(const cvflann_flann_distance_t& value)
template<>
bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const ArgInfo& info)
{
CV_UNUSED(info);
bool ok = true;
PyObject* key = NULL;
PyObject* item = NULL;
Py_ssize_t pos = 0;
if (!o || o == Py_None)
{
return true;
}
if(PyDict_Check(o)) {
while(PyDict_Next(o, &pos, &key, &item))
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;
while(PyDict_Next(o, &key_pos, &key_obj, &value_obj))
{
// get key
std::string key;
if (!getUnicodeString(key_obj, key))
{
// get key
std::string k;
if (!getUnicodeString(key, k))
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)
{
ok = false;
break;
p.setBool(key, npy_value == NPY_TRUE);
continue;
}
// get value
if( !!PyBool_Check(item) )
PyErr_Clear();
}
int int_value = 0;
if (pyopencv_to(value_obj, int_value, key_arg_info))
{
if (key == "algorithm")
{
p.setBool(k, item == Py_True);
}
else if( PyInt_Check(item) )
{
int value = (int)PyInt_AsLong(item);
if( strcmp(k.c_str(), "algorithm") == 0 )
p.setAlgorithm(value);
else
p.setInt(k, value);
}
else if( PyFloat_Check(item) )
{
double value = PyFloat_AsDouble(item);
p.setDouble(k, value);
p.setAlgorithm(int_value);
}
else
{
std::string val_str;
if (!getUnicodeString(item, val_str))
{
ok = false;
break;
}
p.setString(k, val_str);
p.setInt(key, int_value);
}
continue;
}
}
PyErr_Clear();
return ok && !PyErr_Occurred();
double flt_value = 0.0;
if (pyopencv_to(value_obj, flt_value, key_arg_info))
{
if (key == "eps")
{
p.setFloat(key, static_cast<float>(flt_value));
}
else
{
p.setDouble(key, flt_value);
}
continue;
}
PyErr_Clear();
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;
}
template<>
@@ -0,0 +1,30 @@
#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import cv2
import numpy as np
from tests_common import NewOpenCVTests
class FlannBasedMatcher(NewOpenCVTests):
def test_all_parameters_can_be_passed(self):
img1 = self.get_sample("samples/data/right01.jpg")
img2 = self.get_sample("samples/data/right02.jpg")
orb = cv2.ORB.create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
FLANN_INDEX_KDTREE = 1
index_param = dict(algorithm=FLANN_INDEX_KDTREE, trees=4)
search_param = dict(checks=32, sorted=True, eps=0.5,
explore_all_trees=False)
matcher = cv2.FlannBasedMatcher(index_param, search_param)
matches = matcher.knnMatch(np.float32(des1), np.float32(des2), k=2)
self.assertGreater(len(matches), 0)
if __name__ == '__main__':
NewOpenCVTests.bootstrap()