在Kinect for windows SDK 1.7版中,添加了一个抓握的新功能,即左右手的抓与放都能被识别出来,手形的识别需要深度流和骨骼流的数据,所以在使用时,首选得开启这两个流的接收。
如果抓握,需要引用Kinect 的ToolKit,在安装目录下,如C:\Program Files\Microsoft SDKs\Kinect\Developer Toolkit v1.7.0\Assemblies\Microsoft.Kinect.Toolkit.Interaction.dll。当然也得引用Microsoft.Kinect。抓握的识别是靠一个Interaction流来实现的,这些类型在Microsoft.Kinect.ToolKit.Interaction的命名空间下。
下面的例子是一个用winform实现的抓握实践。
首选新建一个Winform项目,后台代码如下:
- using Microsoft.Kinect;
- using Microsoft.Kinect.Toolkit.Interaction;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- using System.Windows.Forms;
- namespace Grip
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- KinectSensor ks = null;
- InteractionClient ic;
- InteractionStream its;
- private void Form1_Load(object sender, EventArgs e)
- {
- foreach (var kss in KinectSensor.KinectSensors)
- {
- if (kss.Status == KinectStatus.Connected)
- {
- ks = kss;
- }
- }
- if (ks != null)
- {
- //开启深度流数据
- ks.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
- //开启骨骼流数据
- ks.SkeletonStream.Enable();
- //设置深度数据读取委托实例
- ks.DepthFrameReady += kinectSensor_DepthFrameReady;
- //设置骨骼数据读取委托实例
- ks.SkeletonFrameReady += kinectSensor_SkeletonFrameReady;
- //实例化交互端
- ic = new InteractionClient();
- //实例化交互流
- its = new InteractionStream(ks, ic);
- //设置交互数据读取委托实例
- its.InteractionFrameReady += its_InteractionFrameReady;
- //开启Kinect,读取数据
- ks.Start();
- }
- }
- private UserInfo[] UserInfos = null;//定义用户信息
- void its_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
- {
- using (InteractionFrame frame = e.OpenInteractionFrame())
- {
- if (frame != null)
- {
- if (this.UserInfos == null)
- {
- //获得用户信息
- this.UserInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
- }
- //把用户数据从交互流中读取存放到UserInfo数组中
- frame.CopyInteractionDataTo(this.UserInfos);
- }
- else
- {
- return;
- }
- }
- //遍历用户数组
- foreach (UserInfo userInfo in this.UserInfos)
- {
- //遍历用户手
- foreach (InteractionHandPointer handPointer in userInfo.HandPointers)
- {
- string action = null;
- //区分动作
- switch (handPointer.HandEventType)
- {
- case InteractionHandEventType.Grip:
- action = "握住";
- break;
- case InteractionHandEventType.GripRelease:
- action = "放开";
- break;
- }
- if (action != null)
- {
- string handSide = "unknown";
- //区分左右手
- switch (handPointer.HandType)
- {
- case InteractionHandType.Left:
- handSide = "左";
- break;
- case InteractionHandType.Right:
- handSide = "右";
- break;
- }
- //设置显示和语音
- if (this.Text != "你" + action + "了" + handSide + "手.")
- {
- this.Text = ("你" + action + "了" + handSide + "手.");
- string content = "CreateObject(\"SAPI.SpVoice\").Speak\"" + Text + "\"";
- try
- {
- File.WriteAllText(@"F:/test/a.vbs", content, Encoding.Default);
- Process.Start(@"F:/test/a.vbs");
- }
- catch
- { }
- }
- }
- }
- }
- }
- private void kinectSensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
- {
- using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())
- {
- if (depthImageFrame != null)
- {
- //把深度数据传给交互对象
- its.ProcessDepth(depthImageFrame.GetRawPixelData(), depthImageFrame.Timestamp);
- }
- }
- }
- private void kinectSensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
- {
- using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
- {
- if (skeletonFrame != null)
- {
- Skeleton[] skeletonData = new Skeleton[ks.SkeletonStream.FrameSkeletonArrayLength];
- skeletonFrame.CopySkeletonDataTo(skeletonData);
- //把骨骼数据传给交互对象
- its.ProcessSkeleton(skeletonData, ks.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
- }
- }
- }
- //关闭窗体时停止Kiect
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (ks != null)
- {
- ks.Stop();
- }
- }
- }
- //实现交互端接口的类
- public class InteractionClient : IInteractionClient
- {
- //实现得到交互对象
- public InteractionInfo GetInteractionInfoAtLocation(int skeletonTrackingId, InteractionHandType handType, double x, double y)
- {
- return new InteractionInfo
- {
- IsPressTarget = true,
- IsGripTarget = true,
- };
- }
- }
- }
当抓握和释放左手或右手时,不但能看到窗体的标题栏上的提示,还能听到语音提示。
同时,需要把KinectInteraction170_32.dll,和KinectInteraction170_64.dll这两个文件放到exe的同目录下,32为32位系统使用,64为64位系统使用。
KinectInteraction170_32.dll下载地址:
http://down.51cto.com/data/771643
KinectInteraction 170_64.dll下载地址:
http://down.51cto.com/data/771644
本文转自桂素伟51CTO博客,原文链接:http://blog.51cto.com/axzxs/1189973 ,如需转载请自行联系原作者