cameraMoving.cs 5.03 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Windows.Kinect;
using System;
using System.IO;

public class cameraMoving : MonoBehaviour
{
    private KinectSensor _Sensor;
    private BodyFrameReader _Reader;
    private Body[] _Data = null;

    public Text stepText;

    public Body[] GetData()
    {
        return _Data;
    }

    int step = 0;
    float speed = 2f;

    KinectManager manager;

    /*
    Windows.Kinect.KinectSensor sensor;
    KinectManager manager;
    Windows.Kinect.Body body;
    Windows.Kinect.BodyIndexFrameArrivedEventArgs e;
    Windows.Kinect.BodyFrameReader bodyFrameReader;

    Vector3 moveToPosition;
    float speed = 2f;
    bool started = false;


    BodyIndexFrameReader bodyIndexFrameReader = null;
    FrameDescription bodyIndexFrameDescription = null;
    //byte[] bodyIndexBuffer = null;
    */
    // Start is called before the first frame update
    void Start()
    {
        _Sensor = KinectSensor.GetDefault();

        if (_Sensor != null)
        {
            _Reader = _Sensor.BodyFrameSource.OpenReader();

            if (!_Sensor.IsOpen)
            {
                _Sensor.Open();
                Debug.Log(_Sensor.IsOpen);

            }
        }


        //Debug.Log(_Sensor.IsAvailable);

        /*
        sensor = Windows.Kinect.KinectSensor.GetDefault();
        sensor.Open();
        //bodyIndexFrameDescription = sensor.BodyIndexFrameSource.FrameDescription;
        bodyIndexFrameReader = sensor.BodyIndexFrameSource.OpenReader();
        bodyFrameReader = sensor.BodyFrameSource.OpenReader();

        Debug.Log(sensor.IsOpen);
        Debug.Log(sensor.IsAvailable);
        Debug.Log(e);
        Debug.Log(bodyIndexFrameReader);
        var bodyIndexFrame = e.FrameReference.AcquireFrame();
        bodyIndexFrame.CopyFrameDataToArray(bodyIndexBuffer);
        for (int i = 0; i < bodyIndexBuffer.Length; i++)
        {
            var index = bodyIndexBuffer[i];
            if (index != 255) {}
        }
        //bodyIndexFrameReader.FrameArrived += bodyIndexBuffer;
        //bodyIndexFrameReader.FrameArrived += KinectManager.BodyIndexFrameReader_FrameArrived(sensor, e);
        //var bodyIndexFrame = 
        //bodyFrameReader = sensor.BodyFrameSource.OpenReader();
        //sensor.Open();
        //bodyFrameReader.FrameArrived += Reader_FrameArrived;
        //body = Windows.Kinect.BodyFrameSource;
        moveToPosition = new Vector3(0, 2, 0);
        Debug.Log("started");
        //Debug.Log(body);
        started = true;
        */
    }

    // Update is called once per frame
    void Update()
    {
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                //Debug.Log("frame");

                if (_Data == null)
                {
                    //Debug.Log("data");

                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                    Debug.Log("2");

                    transform.Translate(Vector3.forward * Time.deltaTime * speed);
                    //Debug.Log("3");

                    //Camera.main.transform.position = Vector3.Lerp(transform.position, moveToPosition, speed);
                    //transform.position = Vector3.Lerp(transform.position, moveToPosition, speed);
                    step = manager.UpdateKinect(_Data);
                    Debug.Log(step);

                    stepText.text = "Step: " + step;

                }

                frame.GetAndRefreshBodyData(_Data);

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

public class KinectManager
{
    int step_num = 0;
    
    //var bodyIndexFrame = 

    public int UpdateKinect(Windows.Kinect.Body[] bodies)
    {
        //Debug.Log("1");
        var body = bodies[0];
        var joints = body.Joints;
        //Debug.Log("2");

        //if (body == null || !body.IsTracked) return 0;

        JointType left_knee = (Windows.Kinect.JointType)13;
        JointType right_knee = (Windows.Kinect.JointType)17;
        //Debug.Log("3");

        var left_knee_position = joints[left_knee].Position;
        var right_knee_position = joints[right_knee].Position;
        //Debug.Log("4");
        //Walking
        if (right_knee_position.X < left_knee_position.X && right_knee_position.Y < left_knee_position.Y)
        {
            Debug.Log(string.Format("{0} STEP", step_num.ToString()));
            //Console.WriteLine(string.Format("{0} STEP", step_num.ToString()));
            step_num++;
        }
        else if (right_knee_position.X > left_knee_position.X && right_knee_position.Y > left_knee_position.Y)
        {
            Debug.Log(string.Format("{0} STEP", step_num.ToString()));
            //Console.WriteLine(string.Format("{0} STEP", step_num.ToString()));
            step_num++;
        }
        else
        {
            Debug.Log("not walking");
            //Console.WriteLine("not walking");
        }
        return step_num;
    }

}