我在 Cython 中有一个 cdef
ed 类,它看起来与此非常相似:
cdef class AprilTagDetector:
cdef capriltag.apriltag_detector_t* _apriltag_detector
def __cinit__(self):
self._apriltag_detector = capriltag.apriltag_detector_create();
# standard null checks
# standard __dealloc__(self) here
property quad_decimate:
def __get__(self):
return self._apriltag_detector.quad_decimate
相应的.pxd
文件如下所示:
cdef extern from "apriltag.h":
# The detector itself
ctypedef struct apriltag_detector_t:
pass
# Detector constructor and destructor
apriltag_detector_t* apriltag_detector_create()
void apriltag_detector_destroy(apriltag_detector_t* td);
问题是,当我去编译这段代码时,它吐出了这个错误:
property quad_decimate:
def __get__(self):
return self._apriltag_detector.quad_decimate ^
------------------------------------------------------------
apriltags.pyx:47:14: Cannot convert 'apriltag_detector_t *' to Python object
这是怎么回事?我无法从 Cython 文档中弄清楚。
最佳答案
谢天谢地,我在与一个黑客空间的 friend 一起做这个项目时解决了这个问题。
问题出在 ctypedef struct apriltag_detector_t
block 中。
当我在 block 中编写 pass
时,我认为 Cython 会自动计算出结构的内部内容,并让我访问我需要的元素 - 这里是 quad_decimate
.
不是这样的。 要让 Cython 理解结构的内容,您必须告诉它结构中的内容:
ctypedef struct apriltag_detector_t:
float quad_decimate
关于python - Cython - 尝试访问结构指针的内容时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28999518/