2012-12-13 20 views
3

这是我第一次试图做一个使用Kinect的程序,我不知道为什么我一直得到一个null错误。也许有人更好地了解KinectSDK可以提供帮助?Kinect错误启用流

public ProjKinect() 
{ 
    InitializeComponent(); 
    updateSensor(0);//set current sensor as 0 since we just started 
} 

public void updateSensor(int sensorI) 
{ 
    refreshSensors();//see if any new ones connected 
    if (sensorI >= sensors.Length)//if it goes to end, then repeat 
    { 
     sensorI = 0; 
    } 
    currentSensorInt = sensorI; 
    if (activeSensor != null && activeSensor.IsRunning) 
    { 
     activeSensor.Stop();//stop so we can cahnge 
    } 
    MessageBox.Show(sensors.Length + " Kinects Found"); 
    activeSensor = KinectSensor.KinectSensors[currentSensorInt]; 
    activeSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); //ERROR IS RIGHT HERE 
    activeSensor.DepthStream.Enable(); 
    activeSensor.SkeletonStream.Enable(); 
    activeSensor.SkeletonFrameReady += runtime_SkeletonFrameReady; 
    activeSensor.DepthFrameReady += runtime_DepthFrameReady; 
    activeSensor.ColorFrameReady += runtime_ImageFrameReady; 
    activeSensor.Start();//start the newly enabled one 
} 
public void refreshSensors() 
{ 
    sensors = KinectSensor.KinectSensors.ToArray(); 
} 

错误:

Object reference not set to an instance of an object. 

顺便说一句,它说我有1 Kinect的连接,所以我知道它至少承认,我有事情来连接。如果我只是说0而不是currentSensorInt,它也不起作用。如果我将ColorStream.Enable评论为DepthStream.Enable,也是一个错误。所以我猜测我创建传感器时只是做错了什么?

希望它有点小。 在此先感谢:)

回答

3

我没有看到任何明显的错误,但我也没有看到传感器获取和设置完全按照这种方式。你有看过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类,这是在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,拔出一个Kinect,然后程序切换到另一个Kinect。

+0

感谢您的回答。但不知何故,它在很多时候拔出并插入kinect后才开始工作。猜测这只是一个奇怪的故障,因为它现在起作用。感谢所有的示例代码。绝对帮助我掌握SDK。 – Brandon

+1

是的,发生了。我还没弄明白为什么。您有时会在'Kinect.dll'中得到一个“InvalidOperation”异常 - 当您拔下Kinect并重新插入时通常会解决这个异常。在调试时保持输出窗口打开,以确保您可以观察此异常以了解当你需要拔掉东西。 :) –