mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +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:
@@ -19,16 +19,39 @@
|
||||
#include <ostream>
|
||||
#include <typeinfo>
|
||||
|
||||
#include "opencv2/core/cvdef.h"
|
||||
#include "opencv2/core/utility.hpp"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
namespace anyimpl
|
||||
{
|
||||
|
||||
struct bad_any_cast
|
||||
struct bad_any_cast : public std::exception
|
||||
{
|
||||
bad_any_cast() = default;
|
||||
|
||||
bad_any_cast(const char* src, const char* dst)
|
||||
: message_(cv::format("cvflann::bad_any_cast(from %s to %s)", src, dst)) {}
|
||||
|
||||
|
||||
const char* what() const noexcept override
|
||||
{
|
||||
return message_.c_str();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string message_{"cvflann::bad_any_cast"};
|
||||
};
|
||||
|
||||
#ifndef CV_THROW_IF_TYPE_MISMATCH
|
||||
#define CV_THROW_IF_TYPE_MISMATCH(src_type_info, dst_type_info) \
|
||||
if ((src_type_info) != (dst_type_info)) \
|
||||
throw cvflann::anyimpl::bad_any_cast((src_type_info).name(), \
|
||||
(dst_type_info).name())
|
||||
#endif
|
||||
|
||||
struct empty_any
|
||||
{
|
||||
};
|
||||
@@ -271,7 +294,7 @@ public:
|
||||
template<typename T>
|
||||
T& cast()
|
||||
{
|
||||
if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
|
||||
CV_THROW_IF_TYPE_MISMATCH(policy->type(), typeid(T));
|
||||
T* r = reinterpret_cast<T*>(policy->get_value(&object));
|
||||
return *r;
|
||||
}
|
||||
@@ -280,7 +303,7 @@ public:
|
||||
template<typename T>
|
||||
const T& cast() const
|
||||
{
|
||||
if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
|
||||
CV_THROW_IF_TYPE_MISMATCH(policy->type(), typeid(T));
|
||||
const T* r = reinterpret_cast<const T*>(policy->get_value(&object));
|
||||
return *r;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
#ifndef OPENCV_FLANN_GENERAL_H_
|
||||
#define OPENCV_FLANN_GENERAL_H_
|
||||
|
||||
#include "opencv2/core/version.hpp"
|
||||
|
||||
#if CV_VERSION_MAJOR <= 4
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
@@ -35,6 +35,9 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "opencv2/core/cvdef.h"
|
||||
#include "opencv2/flann/defines.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
|
||||
@@ -72,11 +72,16 @@ struct SearchParams : public IndexParams
|
||||
|
||||
|
||||
template<typename T>
|
||||
T get_param(const IndexParams& params, cv::String name, const T& default_value)
|
||||
T get_param(const IndexParams& params, const cv::String& name, const T& default_value)
|
||||
{
|
||||
IndexParams::const_iterator it = params.find(name);
|
||||
if (it != params.end()) {
|
||||
return it->second.cast<T>();
|
||||
try {
|
||||
return it->second.cast<T>();
|
||||
} catch (const std::exception& e) {
|
||||
CV_Error_(cv::Error::StsBadArg,
|
||||
("FLANN '%s' param type mismatch: %s", name.c_str(), e.what()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
return default_value;
|
||||
@@ -84,11 +89,16 @@ T get_param(const IndexParams& params, cv::String name, const T& default_value)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get_param(const IndexParams& params, cv::String name)
|
||||
T get_param(const IndexParams& params, const cv::String& name)
|
||||
{
|
||||
IndexParams::const_iterator it = params.find(name);
|
||||
if (it != params.end()) {
|
||||
return it->second.cast<T>();
|
||||
try {
|
||||
return it->second.cast<T>();
|
||||
} catch (const std::exception& e) {
|
||||
CV_Error_(cv::Error::StsBadArg,
|
||||
("FLANN '%s' param type mismatch: %s", name.c_str(), e.what()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
FLANN_THROW(cv::Error::StsBadArg, cv::String("Missing parameter '")+name+cv::String("' in the parameters given"));
|
||||
|
||||
@@ -40,6 +40,9 @@
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include "opencv2/core/base.hpp"
|
||||
#include "opencv2/core/cvdef.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user