kinect笔记 二、识别手势(全身/姿势)

在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;

    /// 
    /// UDP客户端
    /// 
    public UDPClient udpClient;

    /// 
    /// 当检测到新用户时调用。在这里,可以通过调用KinectManager.DetectGesture()来开始手势跟踪。
    /// 
    /// User ID
    /// User index
    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 = "向左、向右或向上滑动,以更改信息。";
            
        }
    }
    
    /// 
    /// 当用户丢失时调用。此用户的所有跟踪手势都会自动清除
    /// 
    /// User ID
    /// User index
    public void UserLost(long userId, int userIndex)
    {
        // 只允许主要用户使用手势
        if (userIndex != playerIndex)
            return;

        if (gestureInfo != null)
        {
            gestureInfo.text = string.Empty;
        }
    }
    /// 
    /// 当手势正在进行时调用
    /// 
    /// User ID
    /// User index
    /// Gesture type
    /// Gesture progress [0..1]
    /// Joint type
    /// Normalized viewport position
    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");
        }
    }
    /// 
    /// 如果手势完成,则调用。
    /// 
    /// true
    /// false
    /// User ID
    /// User index
    /// Gesture type
    /// Joint type
    /// Normalized viewport position    标准视口位置
    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;
    }
    /// 
    /// 如果手势被取消,则调用
    /// 
    /// true
    /// false
    /// User ID
    /// User index
    /// Gesture type
    /// Joint type
    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 () {
        
    }
}

 

转载于:https://www.cnblogs.com/lingLuoChengMi/p/9647177.html

你可能感兴趣的:(kinect笔记 二、识别手势(全身/姿势))