2012-12-13 2 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. 

BTW, 그것이 내가 1 넥트 연결 한 말한다, 그래서 그것을 적어도 나는에 연결하는 일이 있음을 인식 것을 알고있다. currentSensorInt 대신 0이라고 말하면 작동하지 않습니다. 또한 ColorStream.Enable을 주석 처리하면 DepthStream.Enable에서 오류가 발생합니다. 그래서 저는 센서를 만들 때 뭔가 잘못하고 있다고 생각합니까?

잘하면 그것은 작은 것입니다. 미리 감사드립니다 :)

답변

3

나는 명백하게 잘못 본 것이 없지만 이전에 정확히 획득 한 센서를 보지 못했습니다. Kinect for Windows Developer Toolkit 예제를 통해 살펴 보셨습니까? Kinect에 연결하는 방법에 대한 여러 가지 예가 있습니다. 일부는 무차별 적 연결이며 다른 일부는 꽤 강력합니다. 의 부분

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 클래스를 사용하는 것이다 :

, 이것은 SlideshowGestures-WPF 예에서 상기 접속 코드의 트리밍 버전 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(); 
      } 
     } 
    } 

그래, 맞아. 센서가있어 작업을 시작할 수 있습니다. 이 모든 추가 코드의 장점은 툴킷의 KinectExplorer 예에서 볼 수

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 
} 

: 나는 키 넥트를 연결하고 제어하는 ​​방법의 큰 예는 KinectWpfViewers 네임 스페이스에있는 툴킷에서 KinectSensorManager 클래스를 사용 . 요컨대 -이 코드로 여러 Kinect를 관리 할 수 ​​있으며 하나만 뽑으면 프로그램이 다른 코드로 전환됩니다.

+0

답변 해 주셔서 감사합니다. 그러나 여하튼 그것은 많은 시간에 kinect를 뽑고 꽂은 후에 작동하기 시작했습니다. 그것이 지금 일하기 때문에 그것을 추측하는 것은 단지 별난 결함이었다. 모든 샘플 코드를 보내 주셔서 감사합니다. 확실히 SDK를 이해하도록 도와줍니다. – Brandon

+1

예. 그렇습니다. 나는 이유를 알아 내지 못했습니다. Kinect.dll에서 "InvalidOperation"예외가 발생하는 경우가 있는데, Kinect를 뽑았다가 다시 연결하면 예외가 발생합니다. 디버깅 할 때 출력 윈도우를 열어두면이 예외를 알 수 있음을 확인할 수 있습니다 물건을 뽑아야 할 때. :) –