Unity中手波手势C#脚本的问题

时间:2015-03-31 03:08:28

标签: c# unity3d kinect gesture

我让这段代码在昨晚工作,在Unity中注册挥手。不幸的是,我今天开始研究面部识别,我对此代码所做的重大改变是创建一个新的脚本来处理BodySourceManagement。这是在Handwaving停止在Unity的Debug Log中注册的时候。所以我改变了bodymanagement在脚本中处理。所以我有三个不同的脚本基本上跟踪身体。手动脚本,这个手绘脚本和面部识别。

请注意,我是一名设计师,我这样做是为了我班级的任务,我必须使用各种网站自学所有代码。我作为业余爱好已经做了两年了。

using UnityEngine;
using System.Collections;

using Microsoft.Kinect;
using Windows.Kinect;
//using System;

public class HandWaveGesture : MonoBehaviour {
    //public MyBodySourceManager bodysource;
    private KinectSensor Sensor;
    private BodyFrameReader Reader;
    public Body[] bodies;
    private int bodyCount;
    public bool waveSegment1;
    public bool waveComplete;


    // Use this for initialization
    void Start () 
    {
        Sensor = KinectSensor.GetDefault();

        if (Sensor != null)
        {
            Reader = Sensor.BodyFrameSource.OpenReader();

            if (!Sensor.IsOpen)
            {
                Sensor.Open();
            }
        } 

    }

    void OnApplicationQuit()
    {
        if (Reader != null)
        {
            Reader.Dispose();
            Reader = null;
        }

        if (Sensor != null)
        {
            if (Sensor.IsOpen)
            {
                Sensor.Close();
            }

            Sensor = null;
        }
    }
    // Update is called once per frame
    void Update () 
    {
        if (Reader != null)
        {
            var frame = Reader.AcquireLatestFrame();
            if (frame != null)
            {
                if (bodies == null)
                {
                    bodies = new Body[Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(bodies);

                frame.Dispose();
                frame = null;
            }
        }    

            WaveSegments ();
    }

    void WaveSegments()

    {
        if (Reader != null) 
            {
            var frame = Reader.AcquireLatestFrame ();

            if (frame != null)
                {
                if (bodies == null) {
                    bodies = new Body[Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData (bodies);

                frame.Dispose ();
                frame = null;

                int idx = -1;
                for (int i = 0; i < Sensor.BodyFrameSource.BodyCount; i++) {
                    if (bodies [i].IsTracked) 
                    {
                        idx = i;
                    }
                }
                if (idx > -1) 
                    {
                        // Hand above elbow
                            if (bodies [idx].Joints[JointType.HandRight].Position.Y > 
                                bodies [idx].Joints[JointType.ElbowRight].Position.Y)
                            {
                                // Hand right of elbow
                                if (bodies [idx].Joints[JointType.HandRight].Position.X > 
                                    bodies [idx].Joints[JointType.ElbowRight].Position.X)
                                    {
                                        Debug.Log ("Part1Wave Occured");
                                        waveSegment1=true;
                                        waveComplete=false;
                                    }
                                else
                                    {
                                        waveSegment1=false;
                                    }
                            }
                            if(bodies [idx].Joints[JointType.HandRight].Position.Y > 
                               bodies [idx].Joints[JointType.ElbowRight].Position.Y && waveSegment1)
                            {
                                if (bodies [idx].Joints[JointType.HandRight].Position.X > 
                                    bodies [idx].Joints[JointType.ElbowRight].Position.X)
                                    {
                                        waveSegment1=false;
                                        Debug.Log ("You have Completed a Wave");
                                        waveComplete=true;
                                    }
                                else
                                    {

                                    }
                            }

                    }
                }
            }
        }
    }

0 个答案:

没有答案