返回> 网站首页 

[转载]使用微软Kinect进行手势识别操作

yoours2011-07-29 11:24:00 阅读 1310

简介一边听听音乐,一边写写文章。

微软在2009年6月2日的E3大展上。正式公布的XBOX360体感周边外设。Natal彻底颠覆了游戏的单一操作。使人机互动的理念更加彻底的展现出来。 它是一种3D体感摄影机(开发代号“Project Natal”),同时它导入了即时动态捕捉、影像辨识、麦克风输入、语音辨识、社群互动等功能。微软的Natal不需要使用任何控制器;它是依靠相机捕捉三维空间中玩家的运动。Kinect 的出现不仅带来了一种新的游戏操控体验,由之引发的让机器“读懂人”的交互方式,也正在引领人机交互技术的新一轮变革。

 

微软在今年的6月16日正式发布了Kinect for Windows SDK beta, 让开发人员可以在Windows上开发出和XBOX上一样的互动程序。

 

Kinect for Windows SDK的下载地址:

http://research.microsoft.com/en-us/um/redmond/projects/kinectsdk/download.aspx

Kinect SDK目前只支持Windows 7,分为x86和x64两个版本。开发工具方面还需要.NET Framework 4.0和Visual Studio 2010 (最低Express版本)的支持。

Kinect SDK的视频开发教程:

http://channel9.msdn.com/Series/KinectSDKQuickstarts?sort=recent#tab_sortBy_recent

Kinect SDK的开发指南:

http://research.microsoft.com/en-us/um/redmond/projects/kinectsdk/guides.aspx

Kinect SDK的官方论坛:

http://social.msdn.microsoft.com/Forums/en-US/kinectsdk/threads

硬件设备的需求是:Kinect for Xbox 360 sensor和Xbox 360 Kinect AC Adapter/ Power Supply。

 

你可以使用C#(WPF)程序来判断操作者左右手的几个手势:

1.从左 到右挥动

2.从右到左挥动

3.从后到前(按下动作)

4.从前到后(按下后离开)

要正确运行本文的程序,你需要下载和安装:

1.Kinect for Windows SDK
http://research.microsoft.com/en-us/um/redmond/projects/kinectsdk/download.aspx

2.Microsoft XNA Game Studio 4.0
http://www.microsoft.com/download/en/details.aspx?id=23714

因为我们要利用XNA来计算双手的X,Y,Z坐标。

3.微软最新版的Kinect Toolkit :
http://kinecttoolkit.codeplex.com/

微软最新版的Kinect Toolkit 还有很多其他的动作,比如 SwipeToLeft(左下摆),SwipeToRight(右下摆),LeftHello(左手挥手),RightHello(右手挥手)动作,大家有兴趣可以自己好好研究,本篇文章不做解释。

唯一不足的是,没有LeftToRight(左到右挥手)和RightToLeft(右到左挥手),FrontToBack(按下动作),BackToFront(离开动作),我们知道这4个动作是非常有用的。前两个可以用在界面图片或文章的上下翻页,而后两个可以用来选择所需要的图片或者文章。

所以我在微软的Kinect Toolkit里添加了这两个动作的支持,代码如下:

大家可以在Gestures/SwipeGestureDetector.cs

内添加:

void LookForGesture()
        {
            // 左到右
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.20f, (p1, p2) => p2.X - p1.X > - 0.01f , (p1, p2) => Math.Abs(p2.X - p1.X) > 0.2f, 250, 2500))
            {
                RaiseGestureDetected(“LeftToRight”);
                return;
            }

            // 右到左
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.20f, (p1, p2) => p2.X - p1.X < 0.01f, (p1, p2) => Math.Abs(p2.X - p1.X) > 0.2f, 250, 2500))
            {
                RaiseGestureDetected(“RightToLeft”);
                return;
            }

            // 后到前
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.15f, (p1, p2) => p2.Z - p1.Z < 0.01f, (p1, p2) => Math.Abs(p2.Z - p1.Z) > 0.2f, 250, 2500))
            {
                RaiseGestureDetected(“BackToFront”);
                return;
            }

            // 前到后
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.15f, (p1, p2) => p2.Z - p1.Z > -0.04f, (p1, p2) => Math.Abs(p2.Z - p1.Z) > 0.4f, 250, 2500))
            {
                RaiseGestureDetected(“FrontToBack”);
                return;
            }
        }

我们稍微修改下上一篇文章的代码:

void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            SkeletonFrame skeletonFrame = e.SkeletonFrame;
            foreach (SkeletonData data in skeletonFrame.Skeletons)
            {
                if (SkeletonTrackingState.Tracked == data.TrackingState)
                {
                    foreach (Joint joint in data.Joints)
                    {
                        Point jointPos = getDisplayPosition(joint);
                        
                        //判断左手或右手的手势
                        switch (joint.ID)
                        {
                            case JointID.HandLeft://左手
                                if (joint.Position.W > 0.6f) //准确概率>0.6,1.0f 为100% 准确
                                    leftHandGestureRecognizer.Add(joint.Position.ToVector3());
                                break;
                            case JointID.HandRight://右手
                                if (joint.Position.W > 0.6f) //准确概率>0.6,1.0f 为100% 准确
                                    rightHandGestureRecognizer.Add(joint.Position.ToVector3());
                                break;                       
                        }

                    }
                }
            }
        }

下面就可以接收手势判断的事件了:

#region 手势判断
        void rightHandGestureRecognizer_OnGestureDetected(SupportedGesture gesture)//右手手势
        {

            switch (gesture)
            {
                case SupportedGesture.LeftToRight:
                    _receiveGestures("右手:从左到右");
                    break;
                case SupportedGesture.RightToLeft:
                    _receiveGestures("右手:从右到左");
                    break;
                case SupportedGesture.BackToFront:
                    _receiveGestures("右手:从后到前");
                    break;
                case SupportedGesture.FrontToBack:
                    _receiveGestures("右手:从前到后");
                    break;
            }
        }

        void leftHandGestureRecognizer_OnGestureDetected(SupportedGesture gesture)//左手手势
        {

            switch (gesture)
            {
                case SupportedGesture.LeftToRight:
                    _receiveGestures("左手:从左到右");
                    break;
                case SupportedGesture.RightToLeft:
                    _receiveGestures("左手:从右到左");
                    break;
                case SupportedGesture.BackToFront:
                    _receiveGestures("左手:从后到前");
                    break;
                case SupportedGesture.FrontToBack:
                    _receiveGestures("左手:从前到后");
                    break;
            }
        }
        #endregion


微信小程序扫码登陆

文章评论

1310人参与,0条评论