2012年12月29日 星期六

Kinect SDK - (2) 設定開發環境-Setting Up Your Development Environment

  1. Download Sample Requirements
  2. Setting up a new Visual Studio 2010 Project
    • 於VS2010 新增 以Windows Presentation Foundation為版本的new project
    • Add references
      • For C#
      • For Visual Basic
    • 在Add Reference對話框,切換至.NET分頁,選取Microsoft.Research.Kinect (版本:1.0.0.45)
    • 新增 using statement
      • C#
        • using Microsoft.Research.Kinect.Nui;
          using Microsoft.Research.Kinect.Audio;
      • Visual Basic
        • Imports Microsoft.Research.Kinect.Nui
          Imports Microsoft.Research.Kinect.Audio
    • Optional: Using the Coding4Fun Kinect Toolkit
      • 下載Coding4Fun - http://c4fkinect.codeplex.com
      • 載入DLL檔: (WPF 或 Windows Form application應載入不同檔案)
        • WPF Applications: use the Coding4Fun.Kinect.Wpf.dll
          • // if you're using WPF
            using Coding4Fun.Kinect.Wpf;
            // if you're using WinForms
            using Coding4Fun.Kinect.WinForm;
        • Windows Form Applications: use the Coding4Fun.Kinect.WinForm.dll
          • ' if you're using WPF
            Imports Coding4Fun.Kinect.Wpf
            ' if you're using WinForms
            Imports Coding4Fun.Kinect.WinForm
  3. 初始化及解除 - Initializing and Uninitializing the runtime
    • For NUI (Nature User Interface),需要初始化物件,以下的範例利用"Window_Loaded event"來初始化及"Windows_Closed event"解除:
      • Create the Window_Loaded event
        • 到屬性視窗(properties window - F4),選擇"MainWindow"→"Events tab"→雙擊"Loaded"物件來新增"Window_Loaded event"。
        • Initializing the runtime
          • Create a new Runtime variable named “nui” outside of the Window_Loaded event. When the Window_Loaded event fires, we will call the SetupKinect method that checks to see if there is at least one Kinect plugged in, and if there is, it uses the first Kinect (0 representing the first Kinect) and initializes the Runtime (more details on that later). 
          • C#
            //Kinect Runtime
            Runtime nui;private void Window_Loaded(object sender, RoutedEventArgs e)
            {
            SetupKinect();
            }private void SetupKinect()
            {
            if (Runtime.Kinects.Count == 0)
            {
            this.Title = "No Kinect connected";
            }
            else
            {
            //use first Kinect
            nui = Runtime.Kinects[0];
            nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking);
            }
            }
          • Visual Basic
            'Kinect Runtime
            Private nui As Runtime
            Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            SetupKinect() AddHandler Runtime.Kinects.StatusChanged, AddressOf Kinects_StatusChanged
            End Sub Private Sub SetupKinect()
            If Runtime.Kinects.Count = 0 Then
            Me.Title = "No Kinect connected"
            Else
            'use first Kinect
            nui = Runtime.Kinects(0)
            nui.Initialize(RuntimeOptions.UseColor Or RuntimeOptions.UseDepthAndPlayerIndex Or RuntimeOptions.UseSkeletalTracking)
            End If
            End Sub
        • Handling multiple Kinects
          • You can see how many Kinects are connected by getting the count of Kinects as shown in the code below. Note that for multiple Kinects, you can only have one Kinect do skeletal tracking at a time (color and depth camera work for all) and each Kinect needs to be in its own USB hub, otherwise it won’t have enough USB bandwidth.
          • C#
            [code lang=”csharp”]
            int KinectCount = Runtime.Kinects.Count;
            [/code]
          • Visual Basic
          • [code lang=”vb”]
            Dim KinectCount as Integer = Runtime.Kinects.Count
            [/code]
        • Handling Kinect Status Change events
          • You can also handle events when the status changes for Kinects that are plugged in. To do this, first register for the StatusChanged event as shown below. When this event fires, it returns a KinectStatus enum with the following possible values: Connected, Error, Disconnected, NotReady, or NotPowered.
          • C#
            [code lang=”csharp”]
            Runtime.Kinects.StatusChanged += new EventHandler<StatusChangedEventArgs>(Kinects_StatusChanged);
            void Kinects_StatusChanged(object sender, StatusChangedEventArgs e)
            {
            string message = "Status Changed: ";
            switch (e.Status)
            {
            case KinectStatus.Connected:
            message += "Connected";
            break;
            case KinectStatus.Disconnected:
            message += "Disconnected";
            break;
            case KinectStatus.Error:
            message += "Error";
            break;
            case KinectStatus.NotPowered:
            message += "Not Powered";
            break;
            case KinectStatus.NotReady:
            message += "Not Ready";
            break;
            default:
            if (e.Status.HasFlag(KinectStatus.Error))
            {
            message += "Kinect error";
            }
            break;
            }
            this.Title = message;
            }
            [/code]
          • Visual Basic
            [code lang=”vb”]
            AddHandler Runtime.Kinects.StatusChanged, AddressOf Kinects_StatusChanged

            Private Sub Kinects_StatusChanged(sender As Object, e As StatusChangedEventArgs)
            Dim message As String = "Status Changed: "
            Select Case e.Status
            Case KinectStatus.Connected
            message += "Connected"
            Exit Select
            Case KinectStatus.Disconnected
            message += "Disconnected"
            Exit Select
            Case KinectStatus.[Error]
            message += "Error"
            Exit Select
            Case KinectStatus.NotPowered
            message += "Not Powered"
            Exit Select
            Case KinectStatus.NotReady
            message += "Not Ready"
            Exit Select
            Case Else
            If e.Status.HasFlag(KinectStatus.[Error]) Then
            message += "Kinect error"
            End If
            Exit Select
            End Select
            Me.Title = message

            End Sub

            [/code]
        • Setting Runtime Options
          In the Window_Loaded event, initialize the runtime with the options you want to use. For this example, set RuntimeOptions.UseColor to use the RGB camera:
          C#
          nui.Initialize(RuntimeOptions.UseColor);
          Visual Basic
          nui.Initialize(RuntimeOptions.UseColor)
          RuntimeOptions is a Flag enumeration, this means you can set multiple options as parameters in the Initialize method by separating them with the pipe "|" character (the "or" operator) in c# or the "Or" operator in Visual Basic. The example below sets the runtime to use the color camera, a depth camera, and skeletal tracking:
          C#
          nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking
          Visual Basic
          nui.Initialize(RuntimeOptions.UseColor Or RuntimeOptions.UseDepthAndPlayerIndex Or RuntimeOptions.UseSkeletalTracking
      • Uninitializing the Runtime
        • Remember that when you are done using the Kinect Runtime, you should call the Uninitialize method. For a WPF application, you would typically do this in the Windows_Closed event:
          C#
          nui.Uninitialize();
          Visual Basic
          nui.Uninitialize()
    Reference to: http://channel9.msdn.com/Series/KinectSDKQuickstarts/Getting-Started

    沒有留言: