明らかに悪いことは何も見られませんが、センサーがこのように取得されてセットアップされたこともありません。Kinect for Windows Developer Toolkitの例を見たことがありますか?Kinectに接続する方法には複数の例があり、単純なブルートフォース接続であるものもあれば、かなり堅牢なものもあります。
たとえば、これはSlideshowGestures-WPFの例の接続コードのトリミングされたバージョンです。
public partial class MainWindow : Window
{
/// <summary>
/// Active Kinect sensor
/// </summary>
private KinectSensor sensor;
/// <summary>
/// Execute startup tasks
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void WindowLoaded(object sender, RoutedEventArgs e)
{
// Look through all sensors and start the first connected one.
// This requires that a Kinect is connected at the time of app startup.
// To make your app robust against plug/unplug,
// it is recommended to use KinectSensorChooser provided in Microsoft.Kinect.Toolkit
foreach (var potentialSensor in KinectSensor.KinectSensors)
{
if (potentialSensor.Status == KinectStatus.Connected)
{
this.sensor = potentialSensor;
break;
}
}
if (null != this.sensor)
{
// Turn on the color stream to receive color frames
this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
// Add an event handler to be called whenever there is new color frame data
this.sensor.ColorFrameReady += this.SensorColorFrameReady;
// Start the sensor!
try
{
this.sensor.Start();
}
catch (IOException)
{
this.sensor = null;
}
}
}
/// <summary>
/// Execute shutdown tasks
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (null != this.sensor)
{
this.sensor.Stop();
}
}
}
ただし、センサーを取得する最も簡単な方法は、名前空間KinectSensorChooser
の一部であるクラスを使用することです。Microsoft.Kinect.Toolkit
それはあなたのためにすべての仕事をします。たとえば、これが私のセットアップのトリミングされたバージョンです:
public class MainViewModel : ViewModelBase
{
private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel(IDataService dataService)
{
if (IsInDesignMode)
{
// do something special, only for design mode
}
else
{
_sensorChooser.Start();
if (_sensorChooser.Kinect == null)
{
MessageBox.Show("Unable to detect an available Kinect Sensor");
Application.Current.Shutdown();
}
}
}
それでおしまい。私はセンサーを持っていて、それを使い始めることができます。Kinectを接続して制御する方法のより大きな例では、名前空間KinectSensorManager
にあるToolkitのクラスを使用しています。KinectWpfViewers
public class MainViewModel : ViewModelBase
{
private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel(IDataService dataService)
{
if (IsInDesignMode)
{
// do something special, only for design mode
}
else
{
KinectSensorManager = new KinectSensorManager();
KinectSensorManager.KinectSensorChanged += OnKinectSensorChanged;
_sensorChooser.Start();
if (_sensorChooser.Kinect == null)
{
MessageBox.Show("Unable to detect an available Kinect Sensor");
Application.Current.Shutdown();
}
// Bind the KinectSensor from the sensorChooser to the KinectSensor on the KinectSensorManager
var kinectSensorBinding = new Binding("Kinect") { Source = _sensorChooser };
BindingOperations.SetBinding(this.KinectSensorManager, KinectSensorManager.KinectSensorProperty, kinectSensorBinding);
}
}
#region Kinect Discovery & Setup
private void OnKinectSensorChanged(object sender, KinectSensorManagerEventArgs<KinectSensor> args)
{
if (null != args.OldValue)
UninitializeKinectServices(args.OldValue);
if (null != args.NewValue)
InitializeKinectServices(KinectSensorManager, args.NewValue);
}
/// <summary>
/// Initialize Kinect based services.
/// </summary>
/// <param name="kinectSensorManager"></param>
/// <param name="sensor"></param>
private void InitializeKinectServices(KinectSensorManager kinectSensorManager, KinectSensor sensor)
{
// configure the color stream
kinectSensorManager.ColorFormat = ColorImageFormat.RgbResolution640x480Fps30;
kinectSensorManager.ColorStreamEnabled = true;
// configure the depth stream
kinectSensorManager.DepthStreamEnabled = true;
kinectSensorManager.TransformSmoothParameters =
new TransformSmoothParameters
{
// as the smoothing value is increased responsiveness to the raw data
// decreases; therefore, increased smoothing leads to increased latency.
Smoothing = 0.5f,
// higher value corrects toward the raw data more quickly,
// a lower value corrects more slowly and appears smoother.
Correction = 0.5f,
// number of frames to predict into the future.
Prediction = 0.5f,
// determines how aggressively to remove jitter from the raw data.
JitterRadius = 0.05f,
// maximum radius (in meters) that filtered positions can deviate from raw data.
MaxDeviationRadius = 0.04f
};
// configure the skeleton stream
sensor.SkeletonFrameReady += OnSkeletonFrameReady;
kinectSensorManager.SkeletonStreamEnabled = true;
// initialize the gesture recognizer
_gestureController = new GestureController();
_gestureController.GestureRecognized += OnGestureRecognized;
kinectSensorManager.KinectSensorEnabled = true;
if (!kinectSensorManager.KinectSensorAppConflict)
{
// set up addition Kinect based services here
// (e.g., SpeechRecognizer)
}
kinectSensorManager.ElevationAngle = Settings.Default.KinectAngle;
}
/// <summary>
/// Uninitialize all Kinect services that were initialized in InitializeKinectServices.
/// </summary>
/// <param name="sensor"></param>
private void UninitializeKinectServices(KinectSensor sensor)
{
sensor.SkeletonFrameReady -= this.OnSkeletonFrameReady;
}
#endregion Kinect Discovery & Setup
#region Properties
public KinectSensorManager KinectSensorManager { get; private set; }
#endregion Properties
}
この余分なコードすべての利点はKinectExplorer
、ツールキットの例で見ることができます。つまり、このコードで複数のKinectを管理でき、1つを抜くと、プログラムが別のKinectに切り替わります。