Thanks Vitorio for starting the ball rolling.
The python code below works great except for the depth data.
I picked up a Kinect this weekend and was frustrated that on Windows I couldn’t use the Python interface from OpenKinect, then I found CLNUI, but with no Python interface had to resort to ctypes (thankfully Vitorio got the ball rolling with it).
Here are some updates that I found helpful (I’ll post more later if folks find it useful - not sure if this is the best place for it).
If you only want to save the RGB depth image add the following to Vitorio’s code :
#Get 32 bit depth image
kinect.GetNUICameraDepthFrameRGB32(camera, nuiimg32)
#Save the 32bit depth map image
outimg32 = Image.fromstring(‘RGBA’, [640, 480], nuiimg32, ‘raw’,‘RGBA’, 0, 1)
outimg32.save(“depth_img.jpg”)
Now the raw data is done differently, this seems to be (mostly) working for me:
import numpy
import struct
#get raw depth data
kinect.GetNUICameraDepthFrameRAW(camera, nuiimg16)
#unpack and save the depth data
s=struct.Struct(‘H’)
depth_image=numpy.empty((640, 480), dtype=numpy.uint16)
for i in range(640):
for j in range(480):
x=s.unpack_from(nuiimg16,(j*640+i)*2)[0]
depth_image[i, j]=x
depth_image*=255.0/depth_image.max()
i = Image.fromarray(numpy.uint8(depth_image))
i.save(“depth_data.jpg”)
Rob
Vitorio - 21 November 2010 03:27 PM
Here is an example using the CL NUI Platform DLL with Python to pull data from the Kinect. It saves out a PNG of the RGB camera, and what I believe is a valid 16-bit greyscale TIFF of the depth camera.
# Requires at least Python 2.5 and PIL
import ctypes
import time
import Image
# Kinect image buffers
nuiimg24 = ctypes.create_string_buffer(640*480*(24/8))
nuiimg16 = ctypes.create_string_buffer(640*480*(16/8))
# Init the Kinect
kinect = ctypes.cdll.CLNUIDevice
camera = kinect.CreateNUICamera()
kinect.StartNUICamera(camera)
# Let the RGB camera warm up
time.sleep(2)
# Get the Kinect data
kinect.GetNUICameraColorFrameRGB24(camera, nuiimg24)
kinect.GetNUICameraDepthFrameRAW(camera, nuiimg16)
# Shut down the Kinect
kinect.StopNUICamera(camera)
kinect.DestroyNUICamera(camera)
# Output with PIL
outimg24 = Image.fromstring('RGB', [640, 480], nuiimg24, 'raw', 'BGR', 0, 1)
outimg24.save("outimg24.png")
outimg16 = Image.fromstring('I;16', [640, 480], nuiimg16)
outimg16.save('outimg16.tiff')
http://python.pastebin.com/ZQw6ttzZ