0

I have the following C code

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;
}

defined in a function that is being called by Python via ctypes. I want to be able to pass back tag_detections (an array of struct pointers) back to my Python function, and be able to access the structs as Python objects. Here's what I have on the Python side.

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)

Search results have turned up ways to do the other way around (passing an array of Python objects to an array of C structs)

8
  • 1
    Perhaps the reason you haven't found anything is because doing so isn't a good (or feasible) approach to the problem—for example since Python doesn't have pointers. Commented Sep 30, 2017 at 17:43
  • Okay, thanks. How do I accomplish something similar? Commented Sep 30, 2017 at 17:43
  • I'm no expert on the subject, but you may have to pass back an array of the structs themselves, which can be handled with Python's built-in ctypes or struct modules. Commented Sep 30, 2017 at 17:47
  • Do you have any resources I could use to learn how to do that? Commented Sep 30, 2017 at 17:57
  • You now know what to look for, so go for it. Here's something I just found in about 20 seconds: Python C API How to pass array of structs from C to Python. The section in the ctypes documentation about pointers may also be useful. Commented Sep 30, 2017 at 19:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.