1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| from ctypes import *
import cv2 import numpy as np
lib = CDLL("./libcapi.so", RTLD_GLOBAL)
""" // struct struct Point{ float x, y; };
// pure C API SHARED_EXPORT int add(int a, int b); SHARED_EXPORT float addf(float a, float b); SHARED_EXPORT void print_point(Point* p);
// warpper for c++ class SHARED_EXPORT void wrapper_init_class(int id); SHARED_EXPORT void wrapper_free_class();
SHARED_EXPORT int wrapper_add(int a, int b); SHARED_EXPORT float wrapper_addf(float a, float b); SHARED_EXPORT void wrapper_print_point(Point* p); """
class POINT(Structure): _fields_ = [("x", c_float), ("y", c_float)]
lib.add.argtypes = [c_int, c_int] lib.add.restype = c_int
lib.addf.argtypes = [c_float, c_float] lib.addf.restype = c_float
lib.print_point.argtypes = [POINTER(POINT)] lib.print_point.restype = None
lib.wrapper_init_class.argtypes = [c_int] lib.wrapper_init_class.restype = None
lib.wrapper_free_class.argtypes = None lib.wrapper_free_class.restype = None
lib.wrapper_add.argtypes = [c_int, c_int] lib.wrapper_add.restype = c_int
lib.wrapper_addf.argtypes = [c_float, c_float] lib.wrapper_addf.restype = c_float
lib.wrapper_print_point.argtypes = [POINTER(POINT)] lib.wrapper_print_point.restype = None
def test_capi(): print("test_capi") print(lib.add(3,5)) print(lib.addf(3.3,5.5))
p = POINT(9.1,9.2) print(p) print(p.x, p.y) lib.print_point(byref(p)) lib.print_point(pointer(p))
def test_wrapper_capi(): print("test_wrapper_capi")
lib.wrapper_init_class(100)
print(lib.wrapper_add(3,5)) print(lib.wrapper_addf(3.3,5.5))
p = POINT(9.1,9.2) print(p) print(p.x, p.y) lib.wrapper_print_point(byref(p)) lib.wrapper_print_point(pointer(p))
lib.wrapper_free_class()
class IMAGE_FLOAT(Structure): _fields_ = [("h", c_int), ("w", c_int), ("c", c_int), ("data", POINTER(c_float))]
class IMAGE_CHAR(Structure): _fields_ = [("h", c_int), ("w", c_int), ("c", c_int), ("data", POINTER(c_ubyte))]
lib.process_image_float.argtypes = [IMAGE_FLOAT] lib.process_image_float.restype = None
lib.process_image_char.argtypes = [IMAGE_CHAR] lib.process_image_char.restype = None
def mat_to_darknet_image_float(bgr): rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB) rgb = rgb.transpose(2, 0, 1) c = rgb.shape[0] h = rgb.shape[1] w = rgb.shape[2] arr = np.ascontiguousarray(rgb.flat, dtype=np.float32) / 255.0
data = arr.ctypes.data_as(POINTER(c_float)) im = IMAGE_FLOAT(w, h, c, data) return im, arr
def mat_to_image_float(bgr): print(bgr.shape)
(h,w,c) = bgr.shape arr = np.ascontiguousarray(bgr.flat, dtype=np.float32)
data = arr.ctypes.data_as(POINTER(c_float)) im = IMAGE_FLOAT(h, w, c, data) return im, arr
def mat_to_image_char(bgr): print(bgr.shape)
(h,w,c) = bgr.shape arr = np.ascontiguousarray(bgr.flat, dtype=np.uint8)
data = arr.ctypes.data_as(POINTER(c_ubyte)) im = IMAGE_CHAR(h, w, c, data) return im, arr
def test_process_image_float(): image_filepath = "dog.jpg" bgr = cv2.imread(image_filepath) im, arr = mat_to_image_float(bgr) lib.process_image_float(im)
def test_process_image_char(): image_filepath = "dog.jpg" bgr = cv2.imread(image_filepath) im, arr = mat_to_image_char(bgr) lib.process_image_char(im)
def main(): test_process_image_float() test_process_image_char()
if __name__ == "__main__": main()
|