我有一个看起来像这样的c ++代码,我想用ctypes从python中调用它:
c ++:
extern "C" Array<DetectionWindow>* getdets(const uint8_t *indatav, int rows, int cols){
/** Detection window struct */
typedef struct DetectionWindow
{
ushort x; /**< Top-left x coordinate */
ushort y; /**< Top-left y coordinate */
ushort width; /**< Width of the detection window */
ushort height; /**< Height of the detection window */
ushort idx_class; /**< Index of the class */
float score; /**< Confidence value for the detection window */
} DetectionWindow;
...
...
using DetectionWindowArray = Array<DetectionWindow>;
...
DetectionWindowArray win(100000);
Array<DetectionWindow>* dets = &win;
return dets ;
}
我不认为实际上我在c ++中做的事情真的很重要。重要的是我返回一个指向结构数组的指针。编译我设法创建我想在python中使用的共享库的源代码。
Python:
import numpy.ctypeslib as ctl
import ctypes
import numpy as np
import cv2
from numpy.ctypeslib import ndpointer
class detection(ctypes.Structure):
_fields_ = [
('x', ctypes.c_ushort),
('y', ctypes.c_ushort),
('width', ctypes.c_ushort),
('height', ctypes.c_ushort),
('idx_class', ctypes.c_ushort),
('score', ctypes.c_float)
]
lib = ctypes.cdll.LoadLibrary("./lib/libmain.so")
getdets = lib.getdets
getdets.argtypes = [ctl.ndpointer(ctypes.c_uint8, flags='aligned, c_contiguous'), ctypes.c_int, ctypes.c_int]
img = cv2.imread('./data/image_0.jpg',0)
(???) = getdets(img , img.shape[0], img.shape[1])
如何通过我在c ++中返回的指针返回并打印数组中每个元素的所有struct参数?