Kinect2.0UnitySDK used in unity - gesture recognition

Gesture recognition used by Kinect2.0UnitySDK in unity
1: Create a new project, import KinectUnitySDK, create a new empty object and mount the KinectManager script
insert image description here
2: Modify the UserDetected method in the script CubeGestureListener, and add the gesture to be recognized.
insert image description here
3: Modify the GestureCompleted method in the script CubeGestureListerner, and add the logic to be recognized;
insert image description here
in this demo, I only made the logic of waving the left hand and waving the right hand, and expand the above method according to your own needs; the following is the entire code of the script:

/*
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.");
		}
	}
}

The enumeration of all gesture recognition provided by the SDK is in the following code, and the recognition logic can be customized as needed;

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,
}

At this point, the gesture recognition function has been realized, and it is recorded to avoid subsequent forgetting. .

Guess you like

Origin blog.csdn.net/qq_41088607/article/details/124505640