Kinect for windows中的抓握(Grip)和释放(GripRelease)

简介:

在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项目,后台代码如下:
 
 
  1. using Microsoft.Kinect;  
  2. using Microsoft.Kinect.Toolkit.Interaction;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.ComponentModel;  
  6. using System.Diagnostics;  
  7. using System.IO;  
  8. using System.Text;  
  9. using System.Windows.Forms;  
  10.  
  11. namespace Grip  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         KinectSensor ks = null;  
  20.         InteractionClient ic;  
  21.         InteractionStream its;  
  22.         private void Form1_Load(object sender, EventArgs e)  
  23.         {  
  24.             foreach (var kss in KinectSensor.KinectSensors)  
  25.             {  
  26.                 if (kss.Status == KinectStatus.Connected)  
  27.                 {  
  28.                     ks = kss;  
  29.                 }  
  30.             }  
  31.             if (ks != null)  
  32.             {  
  33.                 //开启深度流数据  
  34.                 ks.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);  
  35.                 //开启骨骼流数据  
  36.                 ks.SkeletonStream.Enable();  
  37.                 //设置深度数据读取委托实例  
  38.                 ks.DepthFrameReady += kinectSensor_DepthFrameReady;  
  39.                 //设置骨骼数据读取委托实例  
  40.                 ks.SkeletonFrameReady += kinectSensor_SkeletonFrameReady;  
  41.                 //实例化交互端  
  42.                 ic = new InteractionClient();  
  43.                 //实例化交互流  
  44.                 its = new InteractionStream(ks, ic);  
  45.                 //设置交互数据读取委托实例  
  46.                 its.InteractionFrameReady += its_InteractionFrameReady;  
  47.                 //开启Kinect,读取数据  
  48.                 ks.Start();  
  49.             }  
  50.         }  
  51.  
  52.         private UserInfo[] UserInfos = null;//定义用户信息  
  53.         void its_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)  
  54.         {  
  55.             using (InteractionFrame frame = e.OpenInteractionFrame())  
  56.             {  
  57.                 if (frame != null)  
  58.                 {  
  59.                     if (this.UserInfos == null)  
  60.                     {                        
  61.                         //获得用户信息  
  62.                         this.UserInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];  
  63.                     }  
  64.                     //把用户数据从交互流中读取存放到UserInfo数组中  
  65.                     frame.CopyInteractionDataTo(this.UserInfos);  
  66.                 }  
  67.                 else 
  68.                 {  
  69.                     return;  
  70.                 }  
  71.             }  
  72.             //遍历用户数组  
  73.             foreach (UserInfo userInfo in this.UserInfos)  
  74.             {  
  75.                 //遍历用户手  
  76.                 foreach (InteractionHandPointer handPointer in userInfo.HandPointers)  
  77.                 {  
  78.                     string action = null;  
  79.                     //区分动作  
  80.                     switch (handPointer.HandEventType)  
  81.                     {  
  82.                         case InteractionHandEventType.Grip:  
  83.                             action = "握住";  
  84.                             break;  
  85.                         case InteractionHandEventType.GripRelease:  
  86.                             action = "放开";  
  87.                             break;  
  88.                     }  
  89.                     if (action != null)  
  90.                     {  
  91.                         string handSide = "unknown";  
  92.                         //区分左右手  
  93.                         switch (handPointer.HandType)  
  94.                         {  
  95.                             case InteractionHandType.Left:  
  96.                                 handSide = "左";  
  97.                                 break;  
  98.                             case InteractionHandType.Right:  
  99.                                 handSide = "右";  
  100.                                 break;  
  101.                         }  
  102.                         //设置显示和语音  
  103.                         if (this.Text != "你" + action + "了" + handSide + "手.")  
  104.                         {  
  105.                             this.Text = ("你" + action + "了" + handSide + "手.");  
  106.                             string content = "CreateObject(\"SAPI.SpVoice\").Speak\"" + Text + "\"";  
  107.                             try 
  108.                             {  
  109.                                 File.WriteAllText(@"F:/test/a.vbs", content, Encoding.Default);  
  110.                                 Process.Start(@"F:/test/a.vbs");  
  111.                             }  
  112.                             catch 
  113.                             { }  
  114.                         }  
  115.                     }  
  116.  
  117.                 }  
  118.             }  
  119.  
  120.         }  
  121.  
  122.         private void kinectSensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)  
  123.         {  
  124.             using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())  
  125.             {  
  126.                 if (depthImageFrame != null)  
  127.                 {  
  128.                     //把深度数据传给交互对象  
  129.                     its.ProcessDepth(depthImageFrame.GetRawPixelData(), depthImageFrame.Timestamp);  
  130.                 }  
  131.             }  
  132.         }  
  133.  
  134.         private void kinectSensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)  
  135.         {  
  136.             using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())  
  137.             {  
  138.                 if (skeletonFrame != null)  
  139.                 {  
  140.                     Skeleton[] skeletonData = new Skeleton[ks.SkeletonStream.FrameSkeletonArrayLength];  
  141.                     skeletonFrame.CopySkeletonDataTo(skeletonData);  
  142.                     //把骨骼数据传给交互对象  
  143.                     its.ProcessSkeleton(skeletonData, ks.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);  
  144.                 }  
  145.             }  
  146.         }  
  147.         //关闭窗体时停止Kiect  
  148.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  149.         {  
  150.             if (ks != null)  
  151.             {  
  152.                 ks.Stop();  
  153.             }  
  154.         }  
  155.     }  
  156.  
  157.  
  158.     //实现交互端接口的类  
  159.     public class InteractionClient : IInteractionClient  
  160.     {  
  161.         //实现得到交互对象  
  162.         public InteractionInfo GetInteractionInfoAtLocation(int skeletonTrackingId, InteractionHandType handType, double x, double y)  
  163.         {          
  164.             return new InteractionInfo  
  165.             {  
  166.                 IsPressTarget = true,  
  167.                 IsGripTarget = true,  
  168.             };  
  169.         }  
  170.     }  
  171. }  

当抓握和释放左手或右手时,不但能看到窗体的标题栏上的提示,还能听到语音提示。

同时,需要把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 ,如需转载请自行联系原作者



相关文章
|
传感器 UED Windows
|
语音技术 Windows 数据格式
|
26天前
|
边缘计算 安全 网络安全