Skip to content

Instantly share code, notes, and snippets.

@rafaftahsin
Created October 7, 2015 08:06
Show Gist options
  • Save rafaftahsin/ec988963bb3dacf621ce to your computer and use it in GitHub Desktop.
Save rafaftahsin/ec988963bb3dacf621ce to your computer and use it in GitHub Desktop.
Kinect WPF C# Program to count number of joints tracked in a body.
<Window x:Class="_32905277.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Name="textBox" HorizontalAlignment="Left" Height="23" Margin="200,100,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="125"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
namespace _32905277
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
KinectSensor kinectSensor;
MultiSourceFrameReader msfr;
Body[] bodies;
public MainWindow()
{
InitializeComponent();
kinectSensor = KinectSensor.GetDefault();
kinectSensor.Open();
bodies = new Body[6];
msfr = kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Body);
msfr.MultiSourceFrameArrived += msfr_MultiSourceFrameArrived;
}
void msfr_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
MultiSourceFrame msf = e.FrameReference.AcquireFrame();
if (msf == null) return;
using (BodyFrame bf = msf.BodyFrameReference.AcquireFrame())
{
bf.GetAndRefreshBodyData(bodies);
int numTrackedJoints;
for (int i = 0; i < bodies.Length; i++)
{
if (bodies[i].IsTracked)
{
numTrackedJoints = countTrackedJoints(bodies[i].Joints);
textBox.Text = numTrackedJoints.ToString();
}
}
}
}
public static int countTrackedJoints(IReadOnlyDictionary<JointType, Joint> joints)
{
int count = 0;
foreach (var joint in joints)
{
if (joint.Value.TrackingState == TrackingState.Tracked) count++;
}
return count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment