Copy Data From Numpy To Ctypes Quickly
I have a ctypes object shared between a c++ and python process. The python process takes the input values from that object, runs them through Tensorflow, and I'm left with a numpy
Solution 1:
This shows how to copy a whole data block from numpy array to ctypes array:
import numpy as np
import ctypes
# Preparing example
class TransferData(ctypes.Structure):
_fields_ = [
('statusReady', ctypes.c_bool),
('velocity', ctypes.c_double * 4),
('pressure', ctypes.c_double * 4)
]
data = TransferData()
output = np.array([12., 13., 11., 10.])
# Actual code
# Both values should be equal but there could be problems with alignment settings
assert ctypes.sizeof(data.pressure) == output.nbytes
ctypes.memmove(ctypes.byref(data.pressure), output.ctypes.data, output.nbytes)
print(list(data.pressure))
Post a Comment for "Copy Data From Numpy To Ctypes Quickly"