需要帮助更新旧的Kinect SDK代码
本文关键字:Kinect SDK 代码 帮助 更新 | 更新日期: 2023-09-27 17:49:45
我们一直在寻找最新的Kinect SDK 1.8教程。我们在网上找到的所有教程都是针对旧版本的SDK。如果有人能帮忙,请告诉我们这段代码是用什么版本的Kinect SDK编写的,以及如何将代码更新到最新的SDK代码(1.8版本)。
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;
namespace KinectSkeletonApplication3
{
public partial class MainWindow : Window
{
//Instantiate the Kinect runtime. Required to initialize the device.
//IMPORTANT NOTE: You can pass the device ID here, in case more than one Kinect device is connected.
Runtime runtime = Runtime.Kinects[0];
public MainWindow()
{
InitializeComponent();
//Runtime initialization is handled when the window is opened. When the window
//is closed, the runtime MUST be unitialized.
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded);
runtime.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(runtime_SkeletonFrameReady);
}
void runtime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
SkeletonFrame skeletonSet = e.SkeletonFrame;
SkeletonData data = (from s in skeletonSet.Skeletons
where s.TrackingState == SkeletonTrackingState.Tracked
select s).FirstOrDefault();
if (data != null)
{
SetEllipsePosition(Head, data.Joints[JointID.Head]);
SetEllipsePosition(leftHand, data.Joints[JointID.HandLeft]);
SetEllipsePosition(rightHand, data.Joints[JointID.HandRight]);
ProcessGesture(data.Joints[JointID.Head], data.Joints[JointID.HandLeft], data.Joints[JointID.HandRight]);
}
}
private void ProcessGesture(Joint head, Joint handLeft, Joint handRight)
{
if ((handRight.position.Y > head.position.Y) || (handLeft.position.Y > head.position.Y))
{
MessageBox.Show("Your hand is above your head, bitch.");
}
else if ((handRight.position.Y < 0) || (handLeft.position.Y < 0))
{
MessageBox.Show("Your hand is below your waist, bitch.");
}
else if (handRight.position.X < handLeft.position.X)
{
MessageBox.Show("Your hands are crossed, bitch.");
}
}
private void SetEllipsePosition(Ellipse ellipse, Joint joint)
{
Microsoft.Research.Kinect.Nui.Vector vector = new Microsoft.Research.Kinect.Nui.Vector();
vector.X = ScaleVector(640, joint.Position.X);
vector.Y = ScaleVector(480, -joint.Position.Y);
vector.Z = joint.Position.Z;
Joint updatedJoint = new Joint();
updatedJoint.ID = joint.ID;
updatedJoint.TrackingState = JointTrackingState.Tracked;
updatedJoint.Position = vector;
Canvas.SetLeft(ellipse, updatedJoint.Position.X);
Canvas.SetTop(ellipse, updatedJoint.Position.Y);
}
private float ScaleVector(int length, float position)
{
float value = (((((float)length) / 1f) / 2f) * position) + (length / 2);
if (value > length)
{
return (float)length;
}
if (value < 0f)
{
return 0f;
}
return value;
}
void MainWindow_Unloaded(object sender, RoutedEventArgs e)
{
runtime.Uninitialize();
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//Since only a color video stream is needed, RuntimeOptions.UseColor is used.
runtime.Initialize(Microsoft.Research.Kinect.Nui.RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking);
}
}
}
之前
我会看这篇博客和这篇。它们都描述了从beta版到v1.0版的主要变化。v1.0具有与1.8相同的结构,有关转换代码的示例,请参见将Kinect方法从Beta 2转换为版本1。虽然转换到v1.8可能看起来令人生畏,而且不值得工作,但请继续使用它,它更加高效和有效。