Reputation: 25
I want to build a simple WCF service that returns the depth data from the Kinect sensor. Here's my code:
[DataContract]
public class DepthFrame
{
[DataMember]
public short[] depthData { get; set; }
}
[ServiceContract]
public interface IKinectTools
{
[OperationContract]
DepthFrame getDepthData();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class KinectTools : IKinectTools
{
public DepthFrame getDepthData()
{
DepthFrame df = new DepthFrame();
KinectSensor kinect = KinectSensor.KinectSensors[0];
kinect.DepthStream.Enable();
kinect.Start();
DepthImageFrame depthFrame = kinect.DepthStream.OpenNextFrame(5);
df.depthData = new short[depthFrame.PixelDataLength];
depthFrame.CopyPixelDataTo(df.depthData);
return df;
}
}
but the code is not working. When I run the service, I get an error at the following line
df.depthData = new short[depthFrame.PixelDataLength];
because the depthFrame is null. How come the depthFrame variable is null while I'm calling the OpenNextFrame method?
Thanks in advance!
Upvotes: 2
Views: 418
Reputation: 4036
http://msdn.microsoft.com/en-us/library/microsoft.kinect.depthimagestream.opennextframe.aspx says that the function will return null if the wait time elapses before a frame is available. Since you're specifying a wait time of 5ms this is quite likely to happen. Especially as you only just started the sensor so it may not have finished starting up yet (some of its startup is asynchronous). You need to either specify a longer timeout for the next frame, or handle the null case.
In my game, I poll for a new frame on every game frame specifying a timeout of zero. If the result is null I use the last frame. During game startup I wait for the first frame to become available so that I don't have to deal with the edge case where we haven't got the first frame yet. This is neat because it allows the game frame rate to be decoupled from the Kinect frame rate (which is ideally much lower).
Upvotes: 1