Kinect2.0UnitySDK在unity中使用-手势识别

Kinect2.0UnitySDK在unity中使用之手势识别
1:新建项目,导入KinectUnitySDK, 新建空物体并挂载KinectManager脚本
在这里插入图片描述
2:修改脚本CubeGestureListener 中的UserDetected方法,将要识别的手势添加进去。
在这里插入图片描述
3:修改脚本CubeGestureListerner中GestureCompleted方法,添加要识别的逻辑;
在这里插入图片描述
在此Demo中我只做了挥左手以及挥右手的逻辑,具体看自己需求来拓展以上方法;以下是该脚本的全部代码:

/*
http://www.cgsoso.com/forum-211-1.html

CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!

CGSOSO 主打游戏开发,影视设计等CG资源素材。

插件如若商用,请务必官网购买!

daily assets update for try.

U should buy the asset from home store if u use it in your project!
*/

using UnityEngine;
using System.Collections;
using System;
//using Windows.Kinect;

public class CubeGestureListener : MonoBehaviour, KinectGestures.GestureListenerInterface
{
	[Tooltip("GUI-Text to display gesture-listener messages and gesture information.")]
	public GUIText gestureInfo;

	// singleton instance of the class
	private static CubeGestureListener 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 CubeGestureListener 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;
		//多人手势识别将此判断userId != manager.GetPrimaryUserID())去掉就行。
		if (!manager || (userId != manager.GetPrimaryUserID()))
			return;

		// detect these user specific gestures
		manager.DetectGesture(userId, KinectGestures.Gestures.SwipeLeft);
		manager.DetectGesture(userId, KinectGestures.Gestures.SwipeRight);
		manager.DetectGesture(userId, KinectGestures.Gestures.SwipeUp);
		manager.DetectGesture(userId, KinectGestures.Gestures.RaiseLeftHand);//举起左手
		manager.DetectGesture(userId, KinectGestures.Gestures.RaiseRightHand);//举起右手

		if (gestureInfo != null)
		{
			gestureInfo.GetComponent<GUIText>().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
		KinectManager manager = KinectManager.Instance;
		//多人手势识别将此判断userId != manager.GetPrimaryUserID())去掉就行。
		if (!manager || (userId != manager.GetPrimaryUserID()))
			return;

		if (gestureInfo != null)
		{
			gestureInfo.GetComponent<GUIText>().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
		KinectManager manager = KinectManager.Instance;
		//多人手势识别将此判断userId != manager.GetPrimaryUserID())去掉就行。
		if (!manager || (userId != manager.GetPrimaryUserID()))
			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.GetComponent<GUIText>().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.GetComponent<GUIText>().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 * 100);
				gestureInfo.GetComponent<GUIText>().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
		KinectManager manager = KinectManager.Instance;
	    //多人手势识别将此判断userId != manager.GetPrimaryUserID())去掉就行。
		if (!manager || (userId != manager.GetPrimaryUserID()))
			return false;

		if (gestureInfo != null)
		{
			string sGestureText = gesture + " detected";
			gestureInfo.GetComponent<GUIText>().text = sGestureText;
		}

		if (gesture == KinectGestures.Gestures.SwipeLeft)
		{ swipeLeft = true; print("挥左手"); }
		else if (gesture == KinectGestures.Gestures.SwipeRight)
		{ swipeRight = true; print("挥右手"); }
		else if (gesture == KinectGestures.Gestures.SwipeUp)
			swipeUp = true;
		else if (gesture == KinectGestures.Gestures.RaiseRightHand)
		{
			print("举起右手");
		}
		else if (gesture == KinectGestures.Gestures.RaiseLeftHand)
		{
			print("举起左手");
		}

		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
		KinectManager manager = KinectManager.Instance;
		//多人手势识别将此判断userId != manager.GetPrimaryUserID())去掉就行。
		if (!manager || (userId != manager.GetPrimaryUserID()))
			return false;

		if (progressDisplayed)
		{
			progressDisplayed = false;

			if (gestureInfo != null)
			{
				gestureInfo.GetComponent<GUIText>().text = String.Empty;
			}
		}

		return true;
	}


	void Awake()
	{
		instance = this;
	}

	void Update()
	{
		if (progressDisplayed && ((Time.realtimeSinceStartup - progressGestureTime) > 2f))
		{
			progressDisplayed = false;
			gestureInfo.GetComponent<GUIText>().text = String.Empty;

			Debug.Log("Forced progress to end.");
		}
	}
}

SDK提供全部手势识别的枚举全在以下代码中,可根据需要自定义识别逻辑;

public enum Gestures
{
	None = 0,
	RaiseRightHand,//右手举起过肩并保持至少一秒
	RaiseLeftHand,//左手举起过肩并保持至少一秒
	Psi,//双手举起过肩并保持至少一秒
	Tpose,//触摸
	Stop,//双手下垂
	Wave,//左手或右手举起来回摆动
	Click,//左手或右手在适当的位置停留至少2.5秒
	SwipeLeft,//右手向左挥
	SwipeRight,//左手向右挥
	SwipeUp,//左手或者右手向上挥
	SwipeDown,//左手或者右手向下挥
	RightHandCursor,//假手势,用来使光标随着手移动
	LeftHandCursor,//假手势,用来使光标随着手移动
	ZoomIn,//手肘向下,两手掌相聚至少0.7米,然后慢慢合在一起
	ZoomOut,//手肘向下,左右手掌合在一起(求佛的手势),然后慢慢分开
	Wheel,//想象一下你双手握着方向盘,然后左右转动
	Jump,//在1.5秒内髋关节中心至少上升10厘米 (跳)
	Squat,//在1.5秒内髋关节中心至少下降10厘米 (下蹲)
	Push,//在1.5秒内将左手或右手向外推
	Pull,//在1.5秒内将左手或右手向里拉
	ShoulderLeftFront,//左肩前倾
	ShoulderRightFront,//右肩前倾
	LeanLeft, //身体向左倾斜
	LeanRight, //身体向右倾斜
	LeanForward,//身体向前倾斜
	LeanBack,//身体向后倾斜
	KickLeft,//踢左脚
	KickRight,//踢右脚
	Run,//跑
	RaisedRightHorizontalLeftHand,//左手平举
	RaisedLeftHorizontalRightHand,//右手平举
	
	//自定义手势
	UserGesture1 = 101,
	UserGesture2 = 102,
	UserGesture3 = 103,
	UserGesture4 = 104,
	UserGesture5 = 105,
	UserGesture6 = 106,
	UserGesture7 = 107,
	UserGesture8 = 108,
	UserGesture9 = 109,
	UserGesture10 = 110,
}

到此手势识别功能已实现,记录下来避免后续忘记。。

  • 8
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Kinect 2.0是一种基于体感技术的设备,可以实现用户与电脑之间的交互。它通过感应用户的动作和语音,来控制计算机上的应用程序。Kinect 2.0具有更高的分辨率、更精确的检测能力和更快的响应速度,是Kinect 1.0的升级版本。Unity是一种跨平台的游戏开发引擎,可以帮助开发者快速制作丰富多样的游戏和应用程序。CSDN是一个IT技术社区,提供丰富的编程资源和技术文章。 在使用Kinect 2.0Unity进行开发时,一般可以使用官方提供的开发工具包来实现与Kinect设备的交互。通过Unity的编程接口,可以调用Kinect 2.0的传感器数据,如深度图像、彩色图像和骨骼追踪信息,从而实现实时的人机交互。开发者可以根据自己的需求,自定义Kinect 2.0的交互方式和应用场景,例如通过手势识别来控制角色的移动,通过语音识别来控制应用的操作等。 Kinect 2.0Unity的结合,可以为开发者提供更丰富、更直观的交互体验。通过Kinect 2.0的体感技术,用户可以以更自然的方式与计算机进行交互,提升了交互的乐趣和便捷性。而Unity作为一种强大的游戏开发引擎,可以帮助开发者快速实现各种游戏和应用程序的开发,并且支持多种平台的部署和发布。 总之,Kinect 2.0Unity的结合,可以带来更加丰富、有趣的用户体验,并且为开发者提供了强大的开发工具和平台,使他们能够更轻松地创造出精彩的交互应用。而CSDN作为一个IT技术社区,提供了丰富的资源和技术支持,使开发者能够更好地学习和掌握Kinect 2.0Unity的开发技术

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有点朦

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值