Search code examples
c#kinectkinect-sdk

Kinect V2- Saving Body Data


I am writing a program in C# with the Kinect V2 SDK and am endeavoring to record the Body data. I attempted to just save the BodyFrame to a file, but it is not serializable and neither is the Body class. What would be the best way to accomplish this? My current thinking is to save the positional data for each joint in each body in each frame, but I have a feeling that will get complicated very fast. Suggestions? I've attached a portion of Microsoft's sample BodyBasics code below where it breaks the body data into joints. Thanks!

if (body.IsTracked)
{
     //this.DrawClippedEdges(body, dc);
     IReadOnlyDictionary<JointType, Joint> joints = body.Joints;

     // convert the joint points to depth (display) space
     Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();

     foreach (JointType jointType in joints.Keys)
     {
         // sometimes the depth(Z) of an inferred joint may show as negative
         // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
         CameraSpacePoint position = joints[jointType].Position;
         if (position.Z < 0)
         {
             position.Z = InferredZPositionClamp;
         }

         DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
         jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
      } 

Solution

  • Use JSON.net.

    static List<Body> trackedBodies = new List<Body>();
    trackedBodies = bodies.Where(b => b.IsTracked == true).ToList();
    if (trackedBodies.Count() < 1)
        return null;
    string kinectBodyDataString = JsonConvert.SerializeObject(trackedBodies);