Fixed superglue deadlock on standalone (#896). Fixed some elemSize opencv asserts in debug build (876)

This commit is contained in:
matlabbe
2023-09-17 01:19:59 -07:00
parent fb466a6a96
commit f1cd819673
11 changed files with 68 additions and 129 deletions

View File

@@ -10,6 +10,8 @@
#include <rtabmap/utilite/UConversion.h>
#include <rtabmap/utilite/UTimer.h>
#include <pybind11/embed.h>
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include <numpy/arrayobject.h>
@@ -32,7 +34,7 @@ PyDetector::PyDetector(const ParametersMap & parameters) :
return;
}
lock();
pybind11::gil_scoped_acquire acquire;
std::string matcherPythonDir = UDirectory::getDir(path_);
if(!matcherPythonDir.empty())
@@ -54,15 +56,13 @@ PyDetector::PyDetector(const ParametersMap & parameters) :
if(!pModule_)
{
UERROR("Module \"%s\" could not be imported! (File=\"%s\")", scriptName.c_str(), path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}
unlock();
}
PyDetector::~PyDetector()
{
lock();
pybind11::gil_scoped_acquire acquire;
if(pFunc_)
{
@@ -72,8 +72,6 @@ PyDetector::~PyDetector()
{
Py_DECREF(pModule_);
}
unlock();
}
void PyDetector::parseParameters(const ParametersMap & parameters)
@@ -102,7 +100,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
return keypoints;
}
lock();
pybind11::gil_scoped_acquire acquire;
if(!pFunc_)
{
@@ -116,7 +114,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
if(result == NULL)
{
UERROR("Call to \"init(...)\" in \"%s\" failed!", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return keypoints;
}
Py_DECREF(result);
@@ -129,7 +127,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
else
{
UERROR("Cannot find method \"detect(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
if(pFunc_)
{
Py_DECREF(pFunc_);
@@ -141,7 +139,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
else
{
UERROR("Cannot call method \"init(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return keypoints;
}
Py_DECREF(pFunc);
@@ -149,7 +147,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
else
{
UERROR("Cannot find method \"init(...)\"");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return keypoints;
}
UDEBUG("init time = %fs", timer.ticks());
@@ -167,7 +165,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
if(pReturn == NULL)
{
UERROR("Failed to call match() function!");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}
else
{
@@ -220,8 +218,6 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
Py_DECREF(pImageBuffer);
}
unlock();
return keypoints;
}

View File

@@ -18,7 +18,7 @@
namespace rtabmap
{
class PyDetector : public Feature2D, public PythonInterface
class PyDetector : public Feature2D
{
public:
PyDetector(const ParametersMap & parameters = ParametersMap());

View File

@@ -10,6 +10,8 @@
#include <rtabmap/utilite/UConversion.h>
#include <rtabmap/utilite/UTimer.h>
#include <pybind11/embed.h>
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include <numpy/arrayobject.h>
@@ -39,7 +41,7 @@ PyMatcher::PyMatcher(
return;
}
lock();
pybind11::gil_scoped_acquire acquire;
std::string matcherPythonDir = UDirectory::getDir(path_);
if(!matcherPythonDir.empty())
@@ -59,15 +61,13 @@ PyMatcher::PyMatcher(
if(!pModule_)
{
UERROR("Module \"%s\" could not be imported! (File=\"%s\")", scriptName.c_str(), path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}
unlock();
}
PyMatcher::~PyMatcher()
{
lock();
pybind11::gil_scoped_acquire acquire;
if(pFunc_)
{
Py_DECREF(pFunc_);
@@ -76,7 +76,6 @@ PyMatcher::~PyMatcher()
{
Py_DECREF(pModule_);
}
unlock();
}
std::vector<cv::DMatch> PyMatcher::match(
@@ -104,7 +103,7 @@ std::vector<cv::DMatch> PyMatcher::match(
imageSize.width>0 && imageSize.height>0)
{
lock();
pybind11::gil_scoped_acquire acquire;
UDEBUG("matchThreshold=%f, iterations=%d, cuda=%d", matchThreshold_, iterations_, cuda_?1:0);
@@ -120,7 +119,7 @@ std::vector<cv::DMatch> PyMatcher::match(
if(result == NULL)
{
UERROR("Call to \"init(...)\" in \"%s\" failed!", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return matches;
}
Py_DECREF(result);
@@ -133,7 +132,7 @@ std::vector<cv::DMatch> PyMatcher::match(
else
{
UERROR("Cannot find method \"match(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
if(pFunc_)
{
Py_DECREF(pFunc_);
@@ -145,7 +144,7 @@ std::vector<cv::DMatch> PyMatcher::match(
else
{
UERROR("Cannot call method \"init(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return matches;
}
Py_DECREF(pFunc);
@@ -153,7 +152,7 @@ std::vector<cv::DMatch> PyMatcher::match(
else
{
UERROR("Cannot find method \"init(...)\"");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return matches;
}
UDEBUG("init time = %fs", timer.ticks());
@@ -216,7 +215,7 @@ std::vector<cv::DMatch> PyMatcher::match(
if(pReturn == NULL)
{
UERROR("Failed to call match() function!");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}
else
{
@@ -260,7 +259,6 @@ std::vector<cv::DMatch> PyMatcher::match(
UDEBUG("Fill matches (%d/%d) and cleanup time = %fs", matches.size(), std::min(descriptorsQuery.rows, descriptorsTrain.rows), timer.ticks());
}
unlock();
}
else
{

View File

@@ -16,7 +16,7 @@
namespace rtabmap
{
class PyMatcher : public PythonInterface
class PyMatcher
{
public:
PyMatcher(const std::string & pythonMatcherPath,

View File

@@ -8,83 +8,26 @@
#include <rtabmap/core/PythonInterface.h>
#include <rtabmap/utilite/ULogger.h>
#include <rtabmap/utilite/UThread.h>
#include <pybind11/embed.h>
namespace rtabmap {
UMutex PythonInterface::mutex_;
int PythonInterface::refCount_ = 0;
PyThreadState * PythonInterface::mainThreadState_ = 0;
unsigned long PythonInterface::mainThreadID_ = 0;
PythonInterface::PythonInterface() :
threadState_(0)
PythonInterface::PythonInterface()
{
UScopeMutex lockM(mutex_);
if(refCount_ == 0)
{
UINFO("Py_Initialize() with thread = %d", UThread::currentThreadId());
// initialize Python
Py_Initialize();
// initialize thread support
PyEval_InitThreads();
Py_DECREF(PyImport_ImportModule("threading"));
//release the GIL, store thread state, set the current thread state to NULL
mainThreadState_ = PyEval_SaveThread();
UASSERT(mainThreadState_);
mainThreadID_ = UThread::currentThreadId();
}
++refCount_;
UINFO("Initialize python interpreter");
guard_ = new pybind11::scoped_interpreter();
pybind11::module::import("threading");
release_ = new pybind11::gil_scoped_release();
}
PythonInterface::~PythonInterface()
{
UScopeMutex lock(mutex_);
if(refCount_>0 && --refCount_==0)
{
// shut down the interpreter
UINFO("Py_Finalize() with thread = %d", UThread::currentThreadId());
PyEval_RestoreThread(mainThreadState_);
Py_Finalize();
}
UINFO("Finalize python interpreter");
delete release_;
delete guard_;
}
void PythonInterface::lock()
{
mutex_.lock();
UDEBUG("Lock: Current thread=%d (main=%d)", UThread::currentThreadId(), mainThreadID_);
if(UThread::currentThreadId() == mainThreadID_)
{
PyEval_RestoreThread(mainThreadState_);
}
else
{
// create a thread state object for this thread
threadState_ = PyThreadState_New(mainThreadState_->interp);
UASSERT(threadState_);
PyEval_RestoreThread(threadState_);
}
}
void PythonInterface::unlock()
{
if(UThread::currentThreadId() == mainThreadID_)
{
mainThreadState_ = PyEval_SaveThread();
}
else
{
PyThreadState_Clear(threadState_);
PyThreadState_DeleteCurrent();
}
UDEBUG("Unlock: Current thread=%d (main=%d)", UThread::currentThreadId(), mainThreadID_);
mutex_.unlock();
}
std::string PythonInterface::getTraceback()
std::string getPythonTraceback()
{
// Author: https://stackoverflow.com/questions/41268061/c-c-python-exception-traceback-not-being-generated