传回并使用C中的结构指针数组?

2021-09-27 06:03:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的C代码

int num_detections = zarray_size(detections);

apriltag_detection_t tag_detections[num_detections];
for (int i = 0; i < zarray_size(detections); i++) {
    apriltag_detection_t *det;
    zarray_get(detections, i, &det);
    tag_detections[i] = det;
}

在Python通过ctypes调用的函数中定义。我希望能够将tag_detections(一个struct指针数组)传回我的Python函数,并且能够将struct作为Python对象访问。这是我在Python方面所做的。你知道吗

libtag36h11_detector = ctypes.CDLL('tag36h11_detector/libtag36h11_detector.so')
_scan_frame = libtag36h11_detector.scan_frame
_scan_frame.argtypes = (ctypes.c_int, ctypes.c_int, ndpointer(ctypes.c_uint8, flags="C_CONTIGUOUS"))
...
def scan_frame(frame):
    height, width, channels = frame.shape
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame = frame.flatten()
    _scan_frame(width, height, frame)

搜索结果找到了另一种方法(将Python对象数组传递给Cstruct数组)