Skip to content

Instantly share code, notes, and snippets.

Created July 11, 2012 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3094052 to your computer and use it in GitHub Desktop.
Save anonymous/3094052 to your computer and use it in GitHub Desktop.
This is in Beta 2 code, I have converted it though
/////////////////////////////////////////////////////////////////////////
//
// This module contains code to do Kinect NUI initialization and
// processing and also to display NUI streams on screen.
//
// Copyright © Microsoft Corporation. All rights reserved.
// This code is licensed under the terms of the
// Microsoft Kinect for Windows SDK (Beta) from Microsoft Research
// License Agreement: http://research.microsoft.com/KinectSDK-ToU
//
/////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Research.Kinect.Nui;
using Coding4Fun.Kinect.WinForm;
using Coding4Fun.Kinect.Wpf;
using System.IO;
using System.Threading;
using Microsoft.Research.Kinect.Audio;
//using Microsoft.Speech.AudioFormat;
//using Microsoft.Speech.Recognition;
namespace SkeletalViewer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Runtime nui;
int totalFrames1 = 0;
int timecounter = 10;
DateTime lastTime = DateTime.MaxValue;
double[, ,] skeleton_frames = new double[2000, 21, 3];
double[, ,] skeleton_frames_doctor = new double[2000, 21, 3];
double[, ,] skeleton_frames_doctor_new = new double[2000, 21, 3];
int[, ,] Time_frames_doctor = new int[2000, 1, 3];
int[, ,] Time_frames = new int[2000, 1, 3];
int[, ,] Trans_Vec = new int[1, 1, 3];
string [] delim = new string[3];
// We want to control how depth data gets converted into false-color data
// for more intuitive visualization, so we keep 32-bit color frame buffer versions of
// these, to be updated whenever we receive and process a 16-bit frame.
const int RED_IDX = 2;
const int GREEN_IDX = 1;
const int BLUE_IDX = 0;
byte[] depthFrame32 = new byte[320 * 240 * 4];
int count = 0;
int total_frames = 0;
int[,] JointNo = new int[19, 2];
Dictionary<JointID,Brush> jointColors = new Dictionary<JointID,Brush>() {
{JointID.HipCenter, new SolidColorBrush(Color.FromRgb(169, 176, 155))},
{JointID.Spine, new SolidColorBrush(Color.FromRgb(169, 176, 155))},
{JointID.ShoulderCenter, new SolidColorBrush(Color.FromRgb(168, 230, 29))},
{JointID.Head, new SolidColorBrush(Color.FromRgb(200, 0, 0))},
{JointID.ShoulderLeft, new SolidColorBrush(Color.FromRgb(79, 84, 33))},
{JointID.ElbowLeft, new SolidColorBrush(Color.FromRgb(84, 33, 42))},
{JointID.WristLeft, new SolidColorBrush(Color.FromRgb(255, 126, 0))},
{JointID.HandLeft, new SolidColorBrush(Color.FromRgb(215, 86, 0))},
{JointID.ShoulderRight, new SolidColorBrush(Color.FromRgb(33, 79, 84))},
{JointID.ElbowRight, new SolidColorBrush(Color.FromRgb(33, 33, 84))},
{JointID.WristRight, new SolidColorBrush(Color.FromRgb(77, 109, 243))},
{JointID.HandRight, new SolidColorBrush(Color.FromRgb(37, 69, 243))},
{JointID.HipLeft, new SolidColorBrush(Color.FromRgb(77, 109, 243))},
{JointID.KneeLeft, new SolidColorBrush(Color.FromRgb(69, 33, 84))},
{JointID.AnkleLeft, new SolidColorBrush(Color.FromRgb(229, 170, 122))},
{JointID.FootLeft, new SolidColorBrush(Color.FromRgb(255, 126, 0))},
{JointID.HipRight, new SolidColorBrush(Color.FromRgb(181, 165, 213))},
{JointID.KneeRight, new SolidColorBrush(Color.FromRgb(71, 222, 76))},
{JointID.AnkleRight, new SolidColorBrush(Color.FromRgb(245, 228, 156))},
{JointID.FootRight, new SolidColorBrush(Color.FromRgb(77, 109, 243))}
};
private void Window_Loaded(object sender, EventArgs e)
{
//initializing Natural User Interface (nui)
nui = new Runtime();
// Creating an array of JointNo which is used for Scaling and Transformation
JointNo[0, 0] = 1;
JointNo[0, 1] = 2;
JointNo[1, 0] = 2;
JointNo[1, 1] = 3;
JointNo[2, 0] = 3;
JointNo[2, 1] = 4;
JointNo[3, 0] = 3;
JointNo[3, 1] = 9;
JointNo[4, 0] = 9;
JointNo[4, 1] = 10;
JointNo[5, 0] = 10;
JointNo[5, 1] = 11;
JointNo[6, 0] = 11;
JointNo[6, 1] = 12;
JointNo[7, 0] = 3;
JointNo[7, 1] = 5;
JointNo[8, 0] = 5;
JointNo[8, 1] = 6;
JointNo[9, 0] = 6;
JointNo[9, 1] = 7;
JointNo[10, 0] = 7;
JointNo[10, 1] = 8;
JointNo[11, 0] = 1;
JointNo[11,1] = 13;
JointNo[12, 0] = 13;
JointNo[12, 1] = 14;
JointNo[13,0] = 14;
JointNo[13, 1] = 15;
JointNo[14, 0] = 15;
JointNo[14, 1] = 16;
JointNo[15, 0] = 1;
JointNo[15, 1] = 17;
JointNo[16, 0] = 17;
JointNo[16, 1] = 18;
JointNo[17, 0] = 18;
JointNo[17, 1] = 19;
JointNo[18, 0] = 19;
JointNo[18, 1] = 20;
//Reading the Doctors Data from a file
char[] delimiters = new char[] { '|', ' ' };
//Make sure this file exists.
string file_name_2 = "C:\\preRes\\kinectPT\\gouthamApril12b\\Kinect_for_Physiotherapy\\doctor.txt";
string[] lines = System.IO.File.ReadAllLines(@file_name_2);
foreach (string line in lines)
{
// for the skeleton frame
string[] seperated = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
seperated.CopyTo(delim, 0);
skeleton_frames_doctor[total_frames, count, 0] = int.Parse(delim[0]);
skeleton_frames_doctor[total_frames, count, 1] = int.Parse(delim[1]);
skeleton_frames_doctor[total_frames, count, 2] = int.Parse(delim[2]);
count++;
if (count == 21)
{
count = 0;
total_frames++;
}
}
//nui initialize
try
{
nui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor);
}
catch (InvalidOperationException)
{
System.Windows.MessageBox.Show("Runtime initialization failed. Please make sure Kinect device is plugged in.");
return;
}
try
{
nui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
nui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);
}
catch (InvalidOperationException)
{
System.Windows.MessageBox.Show("Failed to open stream. Please make sure to specify a supported image type and resolution.");
return;
}
lastTime = DateTime.Now;
//Just the skeleton frame is being called
nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);
}
//function to get the coordinates from kinect
private Point getDisplayPosition(Joint joint)
{
float depthX, depthY;
nui.SkeletonEngine.SkeletonToDepthImage(joint.Position, out depthX, out depthY);
depthX = Math.Max(0, Math.Min(depthX * 320, 320)); //convert to 320, 240 space
depthY = Math.Max(0, Math.Min(depthY * 240, 240)); //convert to 320, 240 space
int colorX, colorY;
ImageViewArea iv = new ImageViewArea();
// only ImageResolution.Resolution640x480 is supported at this point
nui.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(ImageResolution.Resolution640x480, iv, (int)depthX, (int)depthY, (short)0, out colorX, out colorY);
// map back to skeleton.Width & skeleton.Height
return new Point((int)(skeleton.Width * colorX / 640.0), (int)(skeleton.Height * colorY / 480));
}
//function to draw the body segment with lines
Polyline getBodySegment(Microsoft.Research.Kinect.Nui.JointsCollection joints, Brush brush, params JointID[] ids)
{
PointCollection points = new PointCollection(ids.Length);
for (int i = 0; i < ids.Length; ++i )
{
points.Add(getDisplayPosition(joints[ids[i]]));
}
Polyline polyline = new Polyline();
polyline.Points = points;
polyline.Stroke = brush;
polyline.StrokeThickness = 1;
return polyline;
}
//function to draw doctors body segment
Polyline getDoctorBody(Brush brush,int frame,int id1,int id2,int id3, int id4,int id5)
{
PointCollection points = new PointCollection(20);
points.Add(new Point(skeleton_frames_doctor_new[frame, id1 - 1, 0], skeleton_frames_doctor[frame, id1 - 1, 1]));
points.Add(new Point(skeleton_frames_doctor_new[frame, id2 - 1, 0], skeleton_frames_doctor[frame, id2 - 1, 1]));
points.Add(new Point(skeleton_frames_doctor_new[frame, id3 - 1, 0], skeleton_frames_doctor[frame, id3 - 1, 1]));
points.Add(new Point(skeleton_frames_doctor_new[frame, id4 - 1, 0], skeleton_frames_doctor[frame, id4 - 1, 1]));
points.Add(new Point(skeleton_frames_doctor_new[frame, id5 - 1, 0], skeleton_frames_doctor[frame, id5 - 1, 1]));
Polyline polyline = new Polyline();
polyline.Points = points;
polyline.Stroke = brush;
polyline.StrokeThickness = 1;
return polyline;
}
//skeleton_frame_ready function
void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
if (timecounter > 0)
{
Thread.Sleep(1000);
frameRate.Text = timecounter.ToString() + " fps";
timecounter--;
}
SkeletonFrame skeletonFrame = e.SkeletonFrame;
int iSkeleton = 0;
int joint_count = 0;
double XPos;
double YPos;
++totalFrames1;
DateTime startTime = DateTime.Now;
string bb1 = Convert.ToString(totalFrames1);
//string file_name_2 = "C:\\Research\\Kinect\\Junk3\\coord" + bb1 + ".txt";
//System.IO.StreamWriter file_dist = new System.IO.StreamWriter(@file_name_2);
Brush[] brushes = new Brush[6];
brushes[0] = new SolidColorBrush(Color.FromRgb(255, 0, 0));
brushes[1] = new SolidColorBrush(Color.FromRgb(0, 255, 0));
brushes[2] = new SolidColorBrush(Color.FromRgb(64, 255, 255));
brushes[3] = new SolidColorBrush(Color.FromRgb(255, 255, 64));
brushes[4] = new SolidColorBrush(Color.FromRgb(255, 64, 255));
brushes[5] = new SolidColorBrush(Color.FromRgb(128, 128, 255));
skeleton.Children.Clear();
//byte[] skeletonFrame32 = new byte[(int)(skeleton.Width) * (int)(skeleton.Height) * 4];
//file_dist.WriteLine("{0},{1}", startTime.Second, startTime.Millisecond);
joint_count = 0;
Time_frames[totalFrames1, 0, 0] = startTime.Millisecond;
Time_frames[totalFrames1, 0, 1] = startTime.Second;
Time_frames[totalFrames1, 0, 2] = startTime.Minute;
foreach (SkeletonData data in skeletonFrame.Skeletons)
{
if (SkeletonTrackingState.Tracked == data.TrackingState)
{
// Draw joints
joint_count = 0;
foreach (Joint joint in data.Joints)
{
Point jointPos = getDisplayPosition(joint);
//storing the patients x y coordinates
skeleton_frames[totalFrames1, joint_count, 0] = (double)jointPos.X;
skeleton_frames[totalFrames1, joint_count, 1] = (double)jointPos.Y;
//getting the doctors x y coordinates
XPos = (skeleton_frames[totalFrames1, 0, 0]) - (skeleton_frames_doctor[totalFrames1, 0, 0]);
YPos = (skeleton_frames[totalFrames1, 0, 1]) - (skeleton_frames_doctor[totalFrames1, 0, 1]);
//Scaling and transformation of doctors frame to patients frame
skeleton_frames_doctor_new[totalFrames1, joint_count, 0] = skeleton_frames_doctor[totalFrames1, joint_count, 0] + XPos;
skeleton_frames_doctor_new[totalFrames1, joint_count, 1] = skeleton_frames_doctor[totalFrames1, joint_count, 1] + YPos;
for (int j = 0; j < 19; j++)
{
double DistP = Math.Sqrt(Math.Pow((skeleton_frames[totalFrames1, JointNo[j, 0] - 1, 0]) - skeleton_frames[totalFrames1, JointNo[j, 1] - 1, 0], 2) + Math.Pow((skeleton_frames[totalFrames1, JointNo[j, 0] - 1, 1]) - skeleton_frames[totalFrames1, JointNo[j, 1] - 1, 1], 2));
double DistD = Math.Sqrt(Math.Pow((skeleton_frames_doctor_new[totalFrames1, JointNo[j, 0] - 1, 0]) - skeleton_frames_doctor_new[totalFrames1, JointNo[j, 1] - 1, 0], 2) + Math.Pow((skeleton_frames_doctor_new[totalFrames1, JointNo[j, 0] - 1, 1]) - skeleton_frames_doctor_new[totalFrames1, JointNo[j, 1] - 1, 1], 2));
double alpha = DistP / DistD;
skeleton_frames_doctor_new[totalFrames1, JointNo[j, 1]-1, 0] = skeleton_frames_doctor_new[totalFrames1, JointNo[j, 0]-1, 0] + (skeleton_frames_doctor_new[totalFrames1, JointNo[j, 1]-1, 0] - skeleton_frames_doctor_new[totalFrames1, JointNo[j, 0]-1, 0]) * (alpha);
skeleton_frames_doctor_new[totalFrames1, JointNo[j, 1]-1, 1] = skeleton_frames_doctor_new[totalFrames1, JointNo[j, 0]-1, 1] + (skeleton_frames_doctor_new[totalFrames1, JointNo[j, 1]-1, 1] - skeleton_frames_doctor_new[totalFrames1, JointNo[j, 0]-1, 1]) * (alpha);
}
//Line jointLine = new Line();
//jointLine.X1 = jointPos.X - 3;
//jointLine.X2 = jointLine.X1 + 6;
//jointLine.Y1 = jointLine.Y2 = jointPos.Y;
//jointLine.Stroke = jointColors[joint.ID];
//jointLine.StrokeThickness = 6;
//skeleton.Children.Add(jointLine);
//file_dist.WriteLine("{0}, {1},{2}", jointPos.X.GetType(),jointPos.X, jointPos.Y);
//joint_count++;
joint_count++;
}
// Draw bones
Brush brush = brushes[iSkeleton % brushes.Length];
skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.HipCenter, JointID.Spine, JointID.ShoulderCenter, JointID.Head));
skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.ShoulderCenter, JointID.ShoulderLeft, JointID.ElbowLeft, JointID.WristLeft, JointID.HandLeft));
skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.ShoulderCenter, JointID.ShoulderRight, JointID.ElbowRight, JointID.WristRight, JointID.HandRight));
skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.HipCenter, JointID.HipLeft, JointID.KneeLeft, JointID.AnkleLeft, JointID.FootLeft));
skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.HipCenter, JointID.HipRight, JointID.KneeRight, JointID.AnkleRight, JointID.FootRight));
skeleton.Children.Add(getDoctorBody(brush, totalFrames1-1, 1, 2, 3, 4, 4));
skeleton.Children.Add(getDoctorBody(brush, totalFrames1-1, 3, 5, 6, 7, 8));
skeleton.Children.Add(getDoctorBody(brush, totalFrames1-1, 3, 9, 10, 11, 12));
skeleton.Children.Add(getDoctorBody(brush, totalFrames1-1, 1, 13, 14, 15, 16));
skeleton.Children.Add(getDoctorBody(brush, totalFrames1-1, 1, 17, 18, 19, 20));
// ExportToPng(uri1, skeleton);
}
iSkeleton++;
} // for each skeleton
}
private void Window_Closed(object sender, EventArgs e)
{
string file_name_2 = "C:\\preRes\\kinectPT\\gouthamApril12b\\Kinect_for_Physiotherapy\\patient.txt";
System.IO.StreamWriter file_dist = new System.IO.StreamWriter(@file_name_2);
for (int i = 0; i < totalFrames1; i++)
{
for (int j = 0; j < 20; j++)
{
file_dist.WriteLine("{0} {1} {2}", skeleton_frames[i, j, 0], skeleton_frames[i, j, 1], skeleton_frames[i, j, 2]);
}
file_dist.WriteLine("{0} {1} {2}", Time_frames[i, 0, 2]*60000+Time_frames[i, 0, 1]*1000+Time_frames[i, 0, 0],0,0);
}
file_dist.Close();
nui.Uninitialize();
Environment.Exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment