Monday, August 13, 2012

Player Tracking with Kinect

Recently I saw this question on StackOverflow and reminded me of Skeleton IDs, Player Indexs, etc; so I thought I would show you guys how to do this. It is pretty simple, and easy to keep track of.
So first we want to cover IDs since they are the easy ones.
 void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)  
 {  
   SkeletonFrame sf = e.SkeletonFrame;  
   if (sf.TrackingState == SkeletalTrackingState.Tracked)  
    {  
      int ID1 = sf.TrackingID;  
    }  
 }  

Really simple right? Index's are also easy but are a bit more complicated.
 void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)   
 {  
   SkeletonFrame sf = e.SkeletonFrame;  
   //check which skeletons in array are active and  
   // use that array indexes for player index  
   SkeletonData player1 = sf.Skeletons[playerIndex1];  
   SkeletonData player2 = sf.Skeletons[playerIndex2];  
 }  

Still extremely simple. Now the code for detecting humans is a bit more complicated than that, but still extremely simple.
 void DepthFrameReady(object sender, DepthFrameReadyEventArgs e)  
 {   
      using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())  
      {  
           short[] rawDepthData = new short[depthFrame.PixelDataLength];  
           depthFrame.CopyPixelDataTo(rawDepthData);   
           Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4];     
           int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;  
           if (player > 0)  
           {  
               //do something  
           }   
      }  
 }  

See? this is all pretty easy and very useful. Hope this helps anyone wanting to know about it.

No comments:

Post a Comment