将boost::shared_ptrs的向量的std::vector从C++传递到Python的问题



我在传递一个<std::vector<std::vector<boost::shared_ptr检测>gt>我希望有人能帮我。我可以传递vector的vector,但当我尝试访问Detection时,我得到一个TypeError,即模块定义中没有为C++类boost::shared_ptr注册的python类。

我的C++代码如下:

class Detection {
public: 
Detection () {};
~Detection() {};

double getR () {return r_;};
double getA () {return a_;};
double r_;
double a_;
}
class Detector {
public
std::vector <std::vector <boost::shared_ptr <Detection> > > getDetections();
}
std::vector <std::vector <boost::shared_ptr <Detection> > > 
Detector::getDetections () {
std::vector <std::vector <boost::shared_ptr <Detection> > > allVecDetections;
// Build 4 vectors containing 2 detections each
for (unsigned int i=0; i < 4; i++) {
std::vector<boost::shared_ptr<Detection> > vecDetections;
for (unsigned int j = 0; j < 2; j++ ) {
boost::shared_ptr<Detection> dt (new Detection (j+i, ts));
dt->r_ = (j+i) + (j*i);
dt->a_ = (j+i) * (j*i);
vecDetections.push_back (dt);
}
allVecDetections.push_back (vecDetections);
}
return allVecDetections;
}
// Module.cpp
BOOST_PYTHON_MODULE (myModule) {

boost::python::class_<Detector>("Detector")
.def("getDetections",  &getDetections)
;
boost::python::class_<Detection>("Detection")
.def("getA",  &getA)
.def("getR",   &getR)
;

// Map list of Detections 
boost::python::class_<std::vector <boost::shared_ptr <Detection> > > ("DetectionVector")
.def(vector_indexing_suite<std::vector <boost::shared_ptr <Detection> > > ());
// Map list of lists of Detections 
boost::python::class_<std::vector <std::vector <boost::shared_ptr <Detection> > > > ("DetectionVectorVector")
.def(vector_indexing_suite<std::vector<std::vector <boost::shared_ptr <Detection> > > > ());
// Register our shared pointers with Python
register_ptr_to_python<boost::shared_ptr<Detection> >(); 

}

我能够从Python中调用getDetections,并返回4个向量,每个向量包含2个Detections。以下是Python代码:

import myModule
...
plots = det.getDetections()
numvectors = len(plots)
print (f'Received {numvectors} vectors ')
jdx = 0
pdx = 0
while jdx < numvectors:
detections = plots[jdx]
pdx = 0
numPlots = len(detections)
print (f'Received {numPlots} extractions')
while pdx < numPlots:
print ("Extraction " + str (pdx) + ":")
detect = detections[jdx]

这运行,但我得到以下输出:

Received 4 vectors  
Received 2 extractions  
Extraction 0:  
Traceback (most recent call last):  
File "scripts/test.py", line 87, in <module>
foo = detections[jdx].getA()  
TypeError: No Python class registered for C++ class boost::shared_ptr<Detection>  

那么,为什么我会抱怨Python没有注册boost::shared_ptr类呢?

谢谢你能给我的任何帮助。

根据Valeca的建议,我重新完成了将向量<矢量>(消除了boost::shared_ptr(。现在,由于在Pyhton脚本中导入模块时出错,模块出现了问题。

以下是新的变化:

class Detection {
public: 
Detection () {};
~Detection() {};
Detection (const Detection &D) {
: r_ (D.r_),
a_ (D.a_)
{};
void operator = (const Detection &D) {
r_ = D.r_;
a_ = D.a_;
};
double getR () {return r_;};
double getA () {return a_;};
double r_;
double a_;
friend bool operator== (const Detection &d1, const Detection &d2);
friend bool operator!= (const Detection &d1, const Detection &d2);
};
bool operator== (const Detection &d1, const Detection &d2) {
return (d1.r_ == d2.r_ && d1.a_ == d2.a_);
}
bool operator!= (const Detection &d1, const Detection &d2) {
return !(d1.r_ == d2.r_ && d1.a_ == d2.a_ );
}
class Detector {
public
std::vector <std::vector <Detection> > getDetections();
}
std::vector <std::vector <Detection> > 
Detector::getDetections () {
std::vector <std::vector <Detection> > allVecDetections;
// Build 4 vectors containing 2 detections each
for (unsigned int i=0; i < 4; i++) {
std::vector<Detection> vecDetections;
for (unsigned int j = 0; j < 2; j++ ) {
Detection dt (j+i, ts);
dt.r_ = (j+i) + (j*i);
dt.a_ = (j+i) * (j*i);
vecDetections.push_back (dt);
}
allVecDetections.push_back (vecDetections);
}
return allVecDetections;
}
// Module.cpp
BOOST_PYTHON_MODULE (myModule) {
boost::python::class_<Detector>("Detector")
.def("getDetections",  &getDetections)
;
boost::python::class_<Detection>("Detection")
.def("getA",  &getA)
.def("getR",   &getR)
;
// Map list of Detections 
boost::python::class_<std::vector <Detection> > ("DetectionVector")
.def(vector_indexing_suite<std::vector <Detection> > ());
// Map list of lists of Detections 
boost::python::class_<std::vector <std::vector<Detection> > > ("DetectionVectorVector")
.def(vector_indexing_suite<std::vector<std::vector <Detection> > > ());
// Register our shared pointers with Python
// register_ptr_to_python<boost::shared_ptr<Detection> >(); 
}

现在Python端的seg在启动时出错。我可能添加了比需要更多的过载操作员,但如果没有他们,我会看到:

*/usr/include/c++/7/bits/predefined_ops.h:241:17:错误:"operator=="不匹配(操作数类型为"Detection"one_answers"const Detection"({return__it=_M_value;}

编译模块时。

谢谢你给我的线索。

我用向量<向量<boost::shared_ptr>gt事实证明,当我注册矢量时,我错过了NoProxy覆盖。请参阅Module.cpp.中的固定代码

boost::python::class_<std::vector <boost::shared_ptr <Detection> > > ("DetectionVector")
.def(vector_indexing_suite<std::vector <boost::shared_ptr <Detection> >,true > ());
// Map list of lists of Detections 
boost::python::class_<std::vector <std::vector <boost::shared_ptr <Detection> > > > ("DetectionVectorVector")
.def(vector_indexing_suite<std::vector<std::vector <boost::shared_ptr <Detection> > >, true > ());

我记得看到过一条关于这件事的Stack Overflow消息,作者在其中说,真相真的很重要。确实如此。感谢所有提出建议的人。

最新更新