cameraMoving.cs
5.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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;
}
}