【Unity/Kinect】手势识别Gesture
在Unity的AssetStore官方商店下载Kinect v2 Examples案例包,参考KinectDemos/GestureDemo这个文件夹下的例子。
自定义一个类,实现KinectGestures.GestureListenerInterface接口。参考案例中的CubeGestureListener中的用法。下面演示监听SwipeLeft向左划,SwipeRight向右划,SwipeUp向上划的手势Gesture。其中包含了一些修改界面UI显示当前手势状态等功能,如不需要可以去掉(主要是用来调试)。
using UnityEngine;
using System.Collections;
using System;
//using Windows.Kinect; public class PeopleGestureListener : MonoBehaviour, KinectGestures.GestureListenerInterface
{
[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
public int playerIndex = ; [Tooltip("GUI-Text to display gesture-listener messages and gesture information.")]
public GUIText gestureInfo; // singleton instance of the class
private static PeopleGestureListener instance = null; // internal variables to track if progress message has been displayed
private bool progressDisplayed;
private float progressGestureTime; // whether the needed gesture has been detected or not
private bool swipeLeft;
private bool swipeRight;
private bool swipeUp; /// <summary>
/// Gets the singleton CubeGestureListener instance.
/// </summary>
/// <value>The CubeGestureListener instance.</value>
public static PeopleGestureListener Instance
{
get
{
return instance;
}
} /// <summary>
/// Determines whether swipe left is detected.
/// </summary>
/// <returns><c>true</c> if swipe left is detected; otherwise, <c>false</c>.</returns>
public bool IsSwipeLeft()
{
if(swipeLeft)
{
swipeLeft = false;
return true;
} return false;
} /// <summary>
/// Determines whether swipe right is detected.
/// </summary>
/// <returns><c>true</c> if swipe right is detected; otherwise, <c>false</c>.</returns>
public bool IsSwipeRight()
{
if(swipeRight)
{
swipeRight = false;
return true;
} return false;
} /// <summary>
/// Determines whether swipe up is detected.
/// </summary>
/// <returns><c>true</c> if swipe up is detected; otherwise, <c>false</c>.</returns>
public bool IsSwipeUp()
{
if(swipeUp)
{
swipeUp = false;
return true;
} return false;
} /// <summary>
/// Invoked when a new user is detected. Here you can start gesture tracking by invoking KinectManager.DetectGesture()-function.
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="userIndex">User index</param>
public void UserDetected(long userId, int userIndex)
{
// the gestures are allowed for the primary user only
KinectManager manager = KinectManager.Instance;
if(!manager || (userIndex != playerIndex))
return; // detect these user specific gestures
manager.DetectGesture(userId, KinectGestures.Gestures.SwipeLeft);
manager.DetectGesture(userId, KinectGestures.Gestures.SwipeRight);
manager.DetectGesture(userId, KinectGestures.Gestures.SwipeUp); if(gestureInfo != null)
{
gestureInfo.text = "Swipe left, right or up to change the slides.";
}
} /// <summary>
/// Invoked when a user gets lost. All tracked gestures for this user are cleared automatically.
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="userIndex">User index</param>
public void UserLost(long userId, int userIndex)
{
// the gestures are allowed for the primary user only
if(userIndex != playerIndex)
return; if(gestureInfo != null)
{
gestureInfo.text = string.Empty;
}
} /// <summary>
/// Invoked when a gesture is in progress.
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="userIndex">User index</param>
/// <param name="gesture">Gesture type</param>
/// <param name="progress">Gesture progress [0..1]</param>
/// <param name="joint">Joint type</param>
/// <param name="screenPos">Normalized viewport position</param>
public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture,
float progress, KinectInterop.JointType joint, Vector3 screenPos)
{
// the gestures are allowed for the primary user only
if(userIndex != playerIndex)
return; if((gesture == KinectGestures.Gestures.ZoomOut || gesture == KinectGestures.Gestures.ZoomIn) && progress > 0.5f)
{
if(gestureInfo != null)
{
string sGestureText = string.Format ("{0} - {1:F0}%", gesture, screenPos.z * 100f);
gestureInfo.text = sGestureText; progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
}
else if((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft ||
gesture == KinectGestures.Gestures.LeanRight) && progress > 0.5f)
{
if(gestureInfo != null)
{
string sGestureText = string.Format ("{0} - {1:F0} degrees", gesture, screenPos.z);
gestureInfo.text = sGestureText; progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
}
else if(gesture == KinectGestures.Gestures.Run && progress > 0.5f)
{
if(gestureInfo != null)
{
string sGestureText = string.Format ("{0} - progress: {1:F0}%", gesture, progress * );
gestureInfo.text = sGestureText; progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
}
} /// <summary>
/// Invoked if a gesture is completed.
/// </summary>
/// <returns>true</returns>
/// <c>false</c>
/// <param name="userId">User ID</param>
/// <param name="userIndex">User index</param>
/// <param name="gesture">Gesture type</param>
/// <param name="joint">Joint type</param>
/// <param name="screenPos">Normalized viewport position</param>
public bool GestureCompleted (long userId, int userIndex, KinectGestures.Gestures gesture,
KinectInterop.JointType joint, Vector3 screenPos)
{
// the gestures are allowed for the primary user only
if(userIndex != playerIndex)
return false; if(gestureInfo != null)
{
string sGestureText = gesture + " detected";
gestureInfo.text = sGestureText;
} if(gesture == KinectGestures.Gestures.SwipeLeft)
swipeLeft = true;
else if(gesture == KinectGestures.Gestures.SwipeRight)
swipeRight = true;
else if(gesture == KinectGestures.Gestures.SwipeUp)
swipeUp = true; return true;
} /// <summary>
/// Invoked if a gesture is cancelled.
/// </summary>
/// <returns>true</returns>
/// <c>false</c>
/// <param name="userId">User ID</param>
/// <param name="userIndex">User index</param>
/// <param name="gesture">Gesture type</param>
/// <param name="joint">Joint type</param>
public bool GestureCancelled (long userId, int userIndex, KinectGestures.Gestures gesture,
KinectInterop.JointType joint)
{
// the gestures are allowed for the primary user only
if(userIndex != playerIndex)
return false; if(progressDisplayed)
{
progressDisplayed = false; if(gestureInfo != null)
{
gestureInfo.text = String.Empty;
}
} return true;
} void Awake()
{
instance = this;
} void Update()
{
if(progressDisplayed && ((Time.realtimeSinceStartup - progressGestureTime) > 2f))
{
progressDisplayed = false;
gestureInfo.text = String.Empty; Debug.Log("Forced progress to end.");
}
} }
调用方法:其他脚本获取该脚本的实例,然后通过IsSwipeLeft()等方法获取手势识别结果即可。
PeopleGestureListener gestureListener = PeopleGestureListener.Instance;
if (gestureListener.IsSwipeLeft())
{
// do something
}
坑点:该脚本需要被挂在Kienct Manager脚本所在的游戏物体身上!
新手建议:如果自己写的GestureListener类 (实现KinectGestures.GestureListenerInterface接口)无论怎么测都不能识别出手势的话,可以复制KinectDemos/GestureDemo下面的例子场景(如KinectGesturesDemo1.unity),先实现能识别出手势了再根据自己的需求删改一下代码!
参考:
【Unity/Kinect】手势识别Gesture的更多相关文章
- kinect for windows - 手势识别之一,kinect手势识别框架
何为手势识别,就是电脑认识人手的自然动作.自然动作包括摆手,握拳,双手合十等等.如果电脑能认识我们这些手势,将来的人机交互将会变得简单而有趣.这里手势不等同于某些规定的动作,比如按鼠标左键,则不属于手 ...
- 【Unity/Kinect】获取预制的手势信息KinectInterop.HandState
Kinect使用了枚举KinectInterop.HandState来描述手势. 该手势指的是手掌的状态(张开/握拳),而不是说整个手臂的肢体动作(Gesture). 同样是需要嵌套在Kinect获取 ...
- 【Unity/Kinect】Kinect入门——项目搭建
本文是Unity Store里的官方Demo包中的ReadMe翻译(别人翻的),介绍了用Unity如何入门搭建起一个Kinect项目工程. 非常感谢下面这位大大的无私奉献! http://www.ma ...
- 【Unity/Kinect】显示Kinect摄像头内容,屏幕显示环境背景及人体投影
最近学习用Unity做些体感小游戏,使用Kinect的Unity插件,结合一些官方Demo学习(网上资源用Unity做的较少,蛋疼).插件及其Demo就在Unity商店里搜Kinect即可找到,其中下 ...
- 【Unity/Kinect】Kinect实现UI控件的点击
用体感来实现UI控件的点击,如点击按钮. 做法:用一个图片表示左手手掌,图片位置追踪左手手掌移动,当手掌位于UI控件的矩形内时,握拳表示点击该控件. using UnityEngine; using ...
- 【Unity/Kinect】Kinect一些常用的API
先开好这个坑,之后用到就补充,方便回顾. 获取用户相对Kinect传感器设备的位置坐标.(在Kinect坐标系中的位置) public Vector3 GetUserPosition(Int64 us ...
- 【Unity/Kinect】使用KinectManager的一般流程
想要从Kinect读取到数据,然后使用数据,通常是以下流程: using UnityEngine; using System.Collections; /// <summary> /// ...
- 手势识别(一)--手势基本概念和ChaLearn Gesture Challenge
以下转自: http://blog.csdn.net/qq1175421841/article/details/50312565 像点击(clicks)是GUI平台的核心,轻点(taps)是触摸平台的 ...
- IOS-Gesture(手势识别)
手势识别——Gesture Recognizer •iOS3.2版本之后,苹果推出了手势识别(Gesture Recognizer),其目的是: –简化开发者的开发难度 –统一用户体验 • •iOS目 ...
随机推荐
- 彻底理解this 的值到底是什么?
作者:方应杭 来源:知乎 你可能遇到过这样的 JS 面试题: var obj = { foo: function(){ console.log(this) } } var bar = obj.foo ...
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
问题:** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau ...
- 【Intellij IDEA】eclipse项目导入
intellij idea中文资料网上比较少,对于eclipse的项目如何导入intellij idea也没有完整的说明,本人在这里整理下,方便更多人加入到intellij idea的阵容里. 直接上 ...
- SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)
题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...
- Nodejs连接mysql的增、删、改、查操作
一,创建数据库 Source Database : my_news_test SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- ...
- 树形动态规划(树形DP)入门问题—初探 & 训练
树形DP入门 poj 2342 Anniversary party 先来个题入门一下~ 题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上 ...
- 在win10中解决 你要以何方式打开此 .xlsx
鼠标右击开始按钮,点击控制面板. 查看方式选择大图标或者小图标. 然后点击“默认程序”. 点击,设置默认程序. 在左侧程序蓝,选择你需要设定的程序.然后点击“将此程序设为默认值”.确定 ...
- C++学习笔记45:多态性
运算符重载 运算符是针对新类型数据的实际需要,对原有运算符进行适当的改造 1.比如使复数类的对象可以使用+运算符实现加法: 2.比如使时钟类的对象可以用++运算符实现时间增加1秒: 注意:可以重载为类 ...
- Java RMI的轻量级实现 - LipeRMI
Java RMI的轻量级实现 - LipeRMI 之前博主有记录关于Java标准库的RMI,后来发现问题比较多,尤其是在安卓端直接被禁止使用,于是转向了第三方的LipeRMI 注意到LipeRMI的中 ...
- Map集合架构总结
说明;这里先学习Map集合,然后再学习Set集合,是因为Set集合中的HashSet依赖于hashMap,就是通过hashMap集合来实现的,TreeSet集合依赖于TreeMap集合,TreeSet ...