Reputation: 75
I am using Numpy and OpenCV2.4.1, my IP Camera has an SDK that returns the picture buffer through a callback function. The simplified function is as follows:
def py_fDecodeCallBack(lPort, pBuffer, lSize, pFrameInfo, lReserved1, lReserved2):
frameInfo = pFrameInfo.contents
pBufY = np.asarray( pBuffer[:frameInfo.lHeight*frameInfo.lWidth],dtype=np.uint8).reshape(frameInfo.lHeight,frameInfo.lWidth, 1)
$
pBuffer is of POINTER(c_ubyte) type as I am using ctypes.
I try to acquire the Y channel of the pBuffer, which is in YV12 format, and put it into a Numpy Array for OpenCV to process.
However, there is a big bottleneck in np.asarray(), it takes too long to acquire the frame data and put into a 3D numpy array (Height, Width, Channel). I have tested that pointer access operation of pBuffer for slicing the Y data out is not the bottle neck. This callback can only run at 3 frames per second on a dual core computer with 4GB ram. Without the np.asarray() operation, the callback can run at 30 frames per second.
Please suggest a method in order to put the pBuffer Data into a 3D numpy array which is FAST enough to get 30frames per second .
Upvotes: 2
Views: 2663
Reputation: 157364
If you don't need to copy the data (i.e. your callback will process it and then discard it) you can construct the array using the buffer directly:
array = (ctypes.c_ubyte * frameInfo.lHeight * frameInfo.lWidth * 1
).from_address(ctypes.addressof(pBuffer.contents))
pBufY = np.ndarray(buffer=array, dtype=np.uint8,
shape=(frameInfo.lHeight, frameInfo.lWidth, 1))
Upvotes: 3