日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/3/24 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Kinect2.0UnitySDK在unity中使用-手势识别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Kinect2.0UnitySDK在unity中使用之手勢識別
1:新建項目,導入KinectUnitySDK, 新建空物體并掛載KinectManager腳本

2:修改腳本CubeGestureListener 中的UserDetected方法,將要識別的手勢添加進去。

3:修改腳本CubeGestureListerner中GestureCompleted方法,添加要識別的邏輯;

在此Demo中我只做了揮左手以及揮右手的邏輯,具體看自己需求來拓展以上方法;以下是該腳本的全部代碼:

/* http://www.cgsoso.com/forum-211-1.htmlCG搜搜 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 classprivate static CubeGestureListener instance = null;// internal variables to track if progress message has been displayedprivate bool progressDisplayed;private float progressGestureTime;// whether the needed gesture has been detected or notprivate 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 onlyKinectManager manager = KinectManager.Instance;//多人手勢識別將此判斷userId != manager.GetPrimaryUserID())去掉就行。if (!manager || (userId != manager.GetPrimaryUserID()))return;// detect these user specific gesturesmanager.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 onlyKinectManager 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 onlyKinectManager 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 onlyKinectManager 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 onlyKinectManager 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, }

到此手勢識別功能已實現,記錄下來避免后續忘記。。

總結

以上是生活随笔為你收集整理的Kinect2.0UnitySDK在unity中使用-手势识别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。