我在Cython上了一节教育课,看起来很像:

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

相应的cdef文件如下所示:
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

怎么回事我还没从赛顿医生那里弄明白。

最佳答案

谢天谢地,当我和一个黑客空间的朋友一起做这个项目时,我发现了这个问题。
问题出在ctypedef struct apriltag_detector_t块中。
当我在块中编写pass时,我认为Cython将自动计算出结构的内部内容,并允许我访问所需的元素-这里,quad_decimate
不是这样。
为了让Cython理解结构的内容,您必须这样告诉它结构中的内容:

ctypedef struct apriltag_detector_t:
    float quad_decimate

关于python - Cython-尝试访问指向struct的指针的内容时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28999518/

10-11 03:25
查看更多