CameraMoving.cs
3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
void Start()
{
_Sensor = KinectSensor.GetDefault();
if (_Sensor != null)
{
_Reader = _Sensor.BodyFrameSource.OpenReader();
if (!_Sensor.IsOpen)
{
_Sensor.Open();
Debug.Log(_Sensor.IsOpen);
}
}
}
// 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");
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;
}
}