繼上一篇的骨架追蹤成功繪製出顏色區分,

本篇將在介紹一支好用的DLL,可以讓我們輕鬆就寫出左右手揮動的控制。

當我們安裝完Kinect SDK V1.5 後,

可以在範例找到一個名為SlideshowGestures-WPF的範例,

並將此範例抓下來。

3  

 

開啟新專案,將使用到的參考都加入

Microsoft.Samples.Kinect.SwipeGestureRecognizer.dll 

可以在SlideshowGestures-WPF範例尋找。

4  


接下來程式撰寫為下面三個步驟:

1.加入使用到的命名空間。

2.開啟Kinect的骨架串流資料。

3.當Kinect偵測到骨架時欲做的事情。


本篇將直接使用SwipeGestureRecognizer的方法來判斷使用者的手勢,

目前此方法只有兩種手勢:

1.左手向右揮

2.右手向左揮


這邊我們將手勢拿來控制鍵盤動作,

在System.Windows.Forms參考裡面,

有 SenKeys 函示能直接控制鍵盤。


完整 xaml.cs code

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.Kinect;
using Microsoft.Samples.Kinect.SwipeGestureRecognizer;

namespace handSwipeDemo
{
    /// 
    /// MainWindow.xaml 的互動邏輯
    /// 
    public partial class MainWindow : Window
    {
        /// 
        /// 使用的識別
        /// 
        private readonly Recognizer activeRecognizer;

        /// 
        ///Kinect接收的骨架陣列
        /// 
        private Skeleton[] skeletons = new Skeleton[0];

        KinectSensor k;

        Boolean skeletonFlag = false;

        public MainWindow()
        {
            InitializeComponent();
            //取得第一台Kinect
            k = KinectSensor.KinectSensors[0];
            // 建立一個手勢的辨識
            this.activeRecognizer = this.CreateRecognizer();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (k != null)
            {
                //開啟骨架串流
                k.SkeletonStream.Enable();
                k.SkeletonFrameReady += new EventHandler(k_SkeletonFrameReady);
                k.Start();
            }
            else
                MessageBox.Show("尚未連接Kinect設備!");
        }

        void k_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            skeletonFlag = isSkeletonTracking(skeletonFlag);

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                }

                if (skeletons.Length != 0)
                {
                    foreach (Skeleton skel in skeletons)
                    {
                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            skeletonFlag = true;    

                            activeRecognizer.Recognize(sender, skeletonFrame, this.skeletons);
                        }
                    }
                }
            }
        }

        //判斷燈號顏色,用於是否追蹤到骨架,黑色代表未追蹤,綠色代表追蹤到
        private Boolean isSkeletonTracking(Boolean flag)
        {
            if (flag)
            {
                //顯示綠色燈號
                ellipse1.Fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 255, 0));
                flag = false;
            }
            else
                //顯示黑色燈號
                ellipse1.Fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 0, 0));

            return flag;
        }

        private Recognizer CreateRecognizer()
        {
            var recognizer = new Recognizer();
            // 右手往左揮
            recognizer.SwipeRightDetected += (s, e) =>
            {
                System.Windows.Forms.SendKeys.SendWait("{Right}");
            };
            // 左手往右揮
            recognizer.SwipeLeftDetected += (s, e) =>
            {
                System.Windows.Forms.SendKeys.SendWait("{Left}");
            };
            return recognizer;
        }


    }
}


完整xaml code

 

<Window x:Class="handSwipeDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<StackPanel>
<TextBlock Text="顯示黑色圈圈,代表骨架未追蹤到!" />
<TextBlock Text="顯示綠色圈圈,代表骨架追蹤到!"/>
</StackPanel>
<Canvas x:Name="CanvasSkeleton" Height="200" Width="200" />
<Ellipse x:Name="ellipse1" Height="50" Width="50" HorizontalAlignment="Center" />
</Grid>
</Window>


參考資料:

無花無酒鋤作田


範例下載:handSwipeDemo


arrow
arrow

    東勢厝滴yang 發表在 痞客邦 留言(3) 人氣()