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)

martineau
  • 119,623
  • 25
  • 170
  • 301
Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
  • 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. – martineau Sep 30 '17 at 17:43
  • Okay, thanks. How do I accomplish something similar? – Carpetfizz Sep 30 '17 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. – martineau Sep 30 '17 at 17:47
  • Do you have any resources I could use to learn how to do that? – Carpetfizz Sep 30 '17 at 17:57
  • You now know what to look for, so [go for it](https://www.google.com/search?lr=&newwindow=1&hl=en&as_qdr=all&q=python+c+interface+array+of+structs&spell=1&sa=X&ved=0ahUKEwj514OSzs3WAhXjsFQKHQigAjgQBQglKAA&biw=1151&bih=940). Here's something I just found in about 20 seconds: [**_Python C API How to pass array of structs from C to Python_**](https://stackoverflow.com/questions/15786525/python-c-api-how-to-pass-array-of-structs-from-c-to-python). The section in the [`ctypes` documentation](https://docs.python.org/3/library/ctypes.html#pointers) about pointers may also be useful. – martineau Sep 30 '17 at 19:18
  • Since you're probably going to need to create your own extension module to put the C code from your question into, also see [**Extending Python with C or C++**](https://docs.python.org/3.4/extending/extending.html) in the documentation. There's probably a number of examples of doing that on the web (as well as in Python's own source code). – martineau Sep 30 '17 at 19:26
  • So I can’t accomplish this with just a DLL/ctypes? – Carpetfizz Sep 30 '17 at 19:27
  • Hard to say from the small snippet of C code in your question. I suspect you'll need to write some new C code that builds a Python list of C `struct`s and passes a pointer to that list back to Python (a little like what is shown in the answer to the linked question in an earlier comment of mine). – martineau Sep 30 '17 at 19:37

0 Answers0