在unity中使用kinect识别手势等,我在这里用的是卡耐基梅隆的插件

网址  https://pan.baidu.com/s/19XbE7CLA64c1K6jrK9wf9g  密码 e4uw  或  链接:https://pan.baidu.com/s/10hC2mubXOrlpm6FD0ZjF4A 密码:odei

借鉴  https://www.cnblogs.com/Zackzhang/p/6203379.html

在场景中创建一个空物体,命名为KinectManager,给这个物体绑定上 KinectManager 和 KinectGestures脚本

再新建一个空物体,命名为MyGestureListener,新建一个脚本MyGestureListener.cs。这个脚本需要继承MonoBehaviour类并且实现KinectGestures.GestureListenerInterface接口。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MyGestureListener : MonoBehaviour,KinectGestures.GestureListenerInterface {

    [Tooltip("由该组件跟踪的播放器索引。0代表第一人,1人,第二人,2人,第三人,等等。")]
    public int playerIndex = 0;

    [Tooltip("Text 用于显示监听到的手势动作的信息")]
    public Text gestureInfo;

    //跟踪进程消息是否已显示的内部变量
    private bool progressDisplayed;
    private float progressGestureTime;

    //是否检测到需要的手势
    private bool swipeLeft;
    private bool swipeRight;
    private bool swipeUp;

    /// <summary>
    /// UDP客户端
    /// </summary>
    public UDPClient udpClient;

    /// <summary>
    /// 当检测到新用户时调用。在这里,可以通过调用KinectManager.DetectGesture()来开始手势跟踪。
    /// </summary>
    /// <param name="userId">User ID</param>
    /// <param name="userIndex">User index</param>
    public void UserDetected(long userId, int userIndex)
    {
        // 只允许主要用户使用手势
        KinectManager manager = KinectManager.Instance;
        if (!manager || (userIndex != playerIndex))
            return;

        // 检测这些用户特定的手势
        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);*/
        /*manager.DetectGesture(userId, KinectGestures.Gestures.Pull);
        manager.DetectGesture(userId, KinectGestures.Gestures.Push);*/
        //manager.DetectGesture(userId, KinectGestures.Gestures.SwipeDown);


        // manager.DetectGesture(userId, KinectGestures.Gestures.Wave);//极少触发

        //ing中
        //manager.DetectGesture(userId, KinectGestures.Gestures.Wheel);
        manager.DetectGesture(userId, KinectGestures.Gestures.ZoomIn);
        manager.DetectGesture(userId, KinectGestures.Gestures.Tpose);
        //manager.DetectGesture(userId, KinectGestures.Gestures.ZoomOut);

        //未测出
        //manager.DetectGesture(userId, KinectGestures.Gestures.RaisedLeftHorizontalRightHand);
        //manager.DetectGesture(userId, KinectGestures.Gestures.RaisedRightHorizontalLeftHand);

        if (gestureInfo != null)
        {
            //gestureInfo.text = "Swipe left, right or up to change the slides.";
            gestureInfo.text = "向左、向右或向上滑动,以更改信息。";
            
        }
    }
    
    /// <summary>
    /// 当用户丢失时调用。此用户的所有跟踪手势都会自动清除
    /// </summary>
    /// <param name="userId">User ID</param>
    /// <param name="userIndex">User index</param>
    public void UserLost(long userId, int userIndex)
    {
        // 只允许主要用户使用手势
        if (userIndex != playerIndex)
            return;

        if (gestureInfo != null)
        {
            gestureInfo.text = string.Empty;
        }
    }
    /// <summary>
    /// 当手势正在进行时调用
    /// </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)
    {
        // 只允许主要用户使用手势
        if (userIndex != playerIndex)
            return;
        /*Debug.Log("ing");
        Debug.Log(gesture);
        Debug.Log("----ing----");*/
        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;
            }
            /*if(gesture == KinectGestures.Gestures.ZoomOut)
            {
                udpClient.SocketSend("fangDa");
            }
            //Debug.Log(gesture);
            if (gesture == KinectGestures.Gestures.ZoomIn)
            {
                udpClient.SocketSend("suoXiao");
            }*/
            if (gesture == KinectGestures.Gestures.ZoomIn && progress > 0.6f)
            {
                Debug.Log("缩小");
                //udpClient.SocketSend("suoXiao");
            }


        }
        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 * 100);
                gestureInfo.text = sGestureText;

                progressDisplayed = true;
                progressGestureTime = Time.realtimeSinceStartup;
            }
        }
        if (gesture == KinectGestures.Gestures.Tpose)
        {
            Debug.Log("放大");
            //udpClient.SocketSend("fangDa");
        }
    }
    /// <summary>
    /// 如果手势完成,则调用。
    /// </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)
    {
        // 只允许主要用户使用手势
        if (userIndex != playerIndex)
            return false;

        Debug.Log("complete");
        Debug.Log(gesture);
        Debug.Log("----complete----");
        if (gestureInfo != null)
        {
            string sGestureText = gesture + " detected";
            gestureInfo.text = sGestureText;
        }

        if (gesture == KinectGestures.Gestures.SwipeLeft)
        {
            swipeLeft = true;
            //udpClient.SocketSend(gesture.ToString());
            //udpClient.SocketSend("xiangZuo");
        }
        else if (gesture == KinectGestures.Gestures.SwipeRight)
        {
            swipeRight = true;
            //udpClient.SocketSend(gesture.ToString());
            //udpClient.SocketSend("xiangYou");
        }
        else if (gesture == KinectGestures.Gestures.SwipeUp)
        {
            swipeUp = true;
            //udpClient.SocketSend(gesture.ToString());
            //udpClient.SocketSend("xiangShang");
        }
        else if(gesture==KinectGestures.Gestures.RaiseLeftHand|| gesture == KinectGestures.Gestures.RaiseRightHand)
        {
            //udpClient.SocketSend("fangDa");
        }

        return true;
    }
    /// <summary>
    /// 如果手势被取消,则调用
    /// </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)
    {
        // 只允许主要用户使用手势
        if (userIndex != playerIndex)
            return false;

        if (progressDisplayed)
        {
            progressDisplayed = false;

            if (gestureInfo != null)
            {
                gestureInfo.text = String.Empty;
            }
        }

        return true;
    }
    // Update is called once per frame
    void Update () {
        
    }
}

 

posted on 2018-09-18 10:44  凌落成迷  阅读(5615)  评论(0编辑  收藏  举报