/
KinectSource.cs
365 lines (309 loc) · 18.3 KB
/
KinectSource.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using UnityEngine;
using Windows.Kinect;
using System.Collections.Generic;
using System;
/// <summary>
/// Connects to the Kinect sensor, retrieves relevant data streams,
/// processes those streams into the forms required for MeshData and SkeletonData objects,
/// and sends those objects to the DataManager object for storage.
/// </summary>
public class KinectSource : MonoBehaviour
{
public DataManager dataManager;
private const int bytesPerPixel = 4;
private KinectSensor kinectSensor = null;
private CoordinateMapper coordinateMapper = null;
private MultiSourceFrameReader multiSourceReader = null;
private ushort[] depthFrameData = null;
private byte[] colorFrameData = null;
private byte[] bodyIndexFrameData = null;
private Body[] bodies = null;
public TimeSpan bodyTimeStamp;
Vector3[,] vertices;
Vector2[,] uvs;
int[,] dbIndex;
int[,] cbIndex;
private ColorSpacePoint[] colorPoints = null;
private CameraSpacePoint[] cameraPoints = null;
private byte[] colorData;
public int depthWidth = 0;
public int depthHeight = 0;
private int colorWidth = 0;
private int colorHeight = 0;
private DepthSpacePoint[] depthPoints;
Color[] cIndex;
private int bodyIndexWidth = 0;
private int bodyIndexHeight = 0;
public bool meshReady;
public bool colorTextureReady;
public bool bodyReady;
public bool bodyIndexReady;
public GameObject depthMap;
public Texture2D colorTexture;
public Texture2D colorBodyIndex;
public int[] bodyIndexFrameArray;
/// <summary>
/// Called when KinectSource GameObject is initialised by the Unity engine.
///
/// Sets up the sensor to read multiple sources, obtains coordinate maps that are necessary to use different types of Kinect data together,
/// and initialises the arrays that store the retrieved data.
/// </summary>
void Start()
{
kinectSensor = KinectSensor.GetDefault();
if (kinectSensor != null)
{
//Enable reading of multiple sources from kinect data.
multiSourceReader = kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.BodyIndex | FrameSourceTypes.Body);
//Obtain coordinate mappings and depth data dimensions.
this.coordinateMapper = this.kinectSensor.CoordinateMapper;
FrameDescription depthFrameDescription = kinectSensor.DepthFrameSource.FrameDescription;
int depthWidth = depthFrameDescription.Width;
int depthHeight = depthFrameDescription.Height;
//Create arrays of the required size to hold scanned data.
this.depthFrameData = new ushort[depthWidth * depthHeight];
this.bodyIndexFrameData = new byte[depthWidth * depthHeight];
this.colorPoints = new ColorSpacePoint[depthWidth * depthHeight];
this.cameraPoints = new CameraSpacePoint[depthWidth * depthHeight];
//Create Unity Texture2D in which to store the ColorFrame data.
FrameDescription colorFrameDescription = kinectSensor.ColorFrameSource.FrameDescription;
int colorWidth = colorFrameDescription.Width;
int colorHeight = colorFrameDescription.Height;
colorData = new byte[colorWidth * colorHeight * bytesPerPixel];
this.depthPoints = new DepthSpacePoint[colorWidth * colorHeight];
//Create array for body pixel indicators.
this.bodyIndexFrameArray = new int[depthWidth * depthHeight];
//Accesses Kinect Sensor
if (!kinectSensor.IsOpen)
{
kinectSensor.Open();
}
}
}
/// <summary>
/// Called every frame (display) by the Unity engine.
///
/// Checks if there is Kinect data available, stores it in the initialised arrays, and tores results by TimeSpan in the DataManager object.
/// </summary>
void Update()
{
//Clears flags for previous data.
bool colorFrameProcessed = false;
bool depthFrameProcessed = false;
bool bodyIndexFrameProcessed = false;
bool bodyFrameProcessed = false;
bool meshDataProcessed = false;
//Conditional access for Kinect data streams, nested to match interdependencies of streams.
if (multiSourceReader != null)
{
var multiSourceFrame = multiSourceReader.AcquireLatestFrame();
if (multiSourceFrame != null)
{
using (DepthFrame depthFrame = multiSourceFrame.DepthFrameReference.AcquireFrame())
{
using (ColorFrame colorFrame = multiSourceFrame.ColorFrameReference.AcquireFrame())
{
using (BodyIndexFrame bodyIndexFrame = multiSourceFrame.BodyIndexFrameReference.AcquireFrame())
{
using (BodyFrame bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame())
{
//Obtains the depth data stream from the Kinect, if avaiable.
//Depth is a set of values showing the relative distance of each point (when mapped on to other sources) in the image from the camera.
if (depthFrame != null)
{
FrameDescription depthFrameDescription = depthFrame.FrameDescription;
depthWidth = depthFrameDescription.Width;
depthHeight = depthFrameDescription.Height;
if ((depthWidth * depthHeight) == this.depthFrameData.Length)
{
depthFrame.CopyFrameDataToArray(this.depthFrameData);
depthFrameProcessed = true;
}
}
//Obtains the color data stream from the Kinect, if available.
//Color is an array of the RGB values for an HD (1920x1080p) image.
if (colorFrame != null)
{
FrameDescription colorFrameDescription = colorFrame.FrameDescription;
colorWidth = colorFrameDescription.Width;
colorHeight = colorFrameDescription.Height;
if ((colorWidth * colorHeight * bytesPerPixel) == this.colorData.Length)
{
colorTexture = new Texture2D(colorFrameDescription.Width, colorFrameDescription.Height, TextureFormat.RGBA32, false);
colorFrame.CopyConvertedFrameDataToArray(colorData, ColorImageFormat.Rgba);
colorTexture.LoadRawTextureData(colorData);
colorTexture.Apply();
colorFrameProcessed = true;
}
}
//Obtains the body index data stream from the Kinect, if available.
//Body index shows which points (when mapped onto other sources) contain a person as recognised by the Kinect.
if (bodyIndexFrame != null)
{
FrameDescription bodyIndexFrameDescription = bodyIndexFrame.FrameDescription;
bodyIndexWidth = bodyIndexFrameDescription.Width;
bodyIndexHeight = bodyIndexFrameDescription.Height;
if ((bodyIndexWidth * bodyIndexHeight) == this.bodyIndexFrameData.Length)
{
bodyIndexFrame.CopyFrameDataToArray(this.bodyIndexFrameData);
for (int i = 0; i < bodyIndexFrameData.Length; i++)
{
bodyIndexFrameArray[i] = (bodyIndexFrameData[i] != 0xff) ? 1 : 0;
}
bodyIndexFrameProcessed = true;
}
}
//Obtains the body data stream from the Kinect, if available.
//Body data stream is the locations and rotations of a set of joints in a representation of a human body, as recognised by the kinect,
//as well as other representational data (hand gestures, confidence, etc) that is not used here.
if (bodyFrame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyFrame.BodyCount];
}
bodyTimeStamp = bodyFrame.RelativeTime;
bodyFrame.GetAndRefreshBodyData(this.bodies);
bodyFrameProcessed = true;
}
//When the three data streams necessary to create a mesh are present, the data is processed to be stored as MeshData.
if (depthFrameProcessed && colorFrameProcessed && bodyIndexFrameProcessed)
{
vertices = new Vector3[depthWidth, depthHeight];
uvs = new Vector2[depthWidth, depthHeight];
dbIndex = new int[depthWidth, depthHeight];
cbIndex = new int[colorWidth, colorHeight];
cIndex = new Color[colorWidth * colorHeight];
//Populate coordinate mapping arrays between depth/color and depth/camera spaces.
//(Camera space being coordinate system with camera at the origin.)
this.coordinateMapper.MapDepthFrameToColorSpace(this.depthFrameData, this.colorPoints);
this.coordinateMapper.MapDepthFrameToCameraSpace(this.depthFrameData, this.cameraPoints);
//Mapping is done between data streams, and non-body depth data is discarded.
//Depth points are stored as vertices.
//Color mappings to depth points are stored as uvs.
//BodyIndex is kept for use in further processing.
for (int x = 0; x < depthWidth; x++)
{
for (int y = 0; y < depthHeight; y++)
{
int depthIndex = x + y * depthWidth;
byte bodyPixel = this.bodyIndexFrameData[depthIndex];
CameraSpacePoint cameraPoint = this.cameraPoints[depthIndex];
ColorSpacePoint colorPoint = this.colorPoints[depthIndex];
Vector3 vertex = new Vector3(cameraPoint.X, cameraPoint.Y, cameraPoint.Z);
Vector2 uv = new Vector2(colorPoint.X / colorWidth, colorPoint.Y / colorHeight);
int bodyPixelVal = 0;
if (bodyPixel != 0xff)
{
bodyPixelVal = 1;
}
else
{
vertex = Vector3.zero;
}
vertices[x, y] = vertex;
uvs[x, y] = uv;
dbIndex[x, y] = bodyPixelVal;
}
}
//Populate coordinate mapping arrays between depth and color spaces.
this.coordinateMapper.MapColorFrameToDepthSpace(this.depthFrameData, depthPoints);
//Maps the body index stream into a masking map for the color stream.
for (int x = 0; x < colorWidth; x++)
{
for (int y = 0; y < colorHeight; y++)
{
int colorIndex = x + y * colorWidth;
int depthX = (int)depthPoints[colorIndex].X;
int depthY = (int)depthPoints[colorIndex].Y;
int depthIndex = depthX + depthY * depthWidth;
int colorIndexVal = 0;
if ((depthIndex > 0) && (depthIndex < depthWidth * depthHeight))
{
byte bodyPixel = this.bodyIndexFrameData[depthIndex];
if (bodyPixel != 0xff)
{
colorIndexVal = 1;
}
}
cbIndex[x, y] = colorIndexVal;
}
}
meshDataProcessed = true;
}
// If mesh has been processed and the body frame (required to create skeleton model) are both available,
// MeshData and SkeletonData objects are created, and passed to the data manager to be stored under the corresponding TimeSpan.
// flags are reset.
if (meshDataProcessed && bodyFrameProcessed)
{
MeshData meshData = new MeshData(bodyTimeStamp, vertices, uvs, dbIndex, cbIndex, colorTexture);
SkeletonData skeletonData = new SkeletonData(bodyTimeStamp, bodies);
dataManager.AddToList(bodyTimeStamp, meshData, skeletonData);
meshDataProcessed = false;
depthFrameProcessed = false;
colorFrameProcessed = false;
bodyIndexFrameProcessed = false;
bodyFrameProcessed = false;
}
else
{
string s = "Proccessing Error Occurred. The following failed : ";
if (!depthFrameProcessed) s = s + "Depth, ";
if (!colorFrameProcessed) s = s + "Color, ";
if (!bodyIndexFrameProcessed) s = s + "Body Index, ";
if (!bodyFrameProcessed) s = s + "Body (Skeleton), ";
if (!meshDataProcessed) s = s + "Mesh Data";
//Debug.Log(s);
}
}
}
}
}
multiSourceFrame = null;
}
}
}
/// <summary>
/// Called when Unity application or build program is closed.
///
/// Closes the data streams from the Kinects and stops the application accessing the sensor.
/// </summary>
void OnApplicationQuit()
{
if (multiSourceReader != null)
{
multiSourceReader.Dispose();
multiSourceReader = null;
}
if (kinectSensor != null)
{
if (kinectSensor.IsOpen)
{
kinectSensor.Close();
}
kinectSensor = null;
}
}
//DEBUGGING
void DrawDepthMap()
{
Texture2D texture = new Texture2D(depthWidth, depthHeight, TextureFormat.BGRA32, false);
byte[] RawData = new byte[depthWidth * depthHeight * 4];
int index = 0;
for (int x = 0; x < depthWidth; x++)
{
for (int y = 0; y < depthHeight; y++)
{
int depthIndex = x + y * depthWidth;
byte intensity = (byte)(depthFrameData[depthIndex] >> 8);
RawData[index++] = intensity;
RawData[index++] = intensity;
RawData[index++] = intensity;
RawData[index++] = 255;
texture.LoadRawTextureData(RawData);
texture.Apply();
depthMap.GetComponent<Renderer>().material.SetTexture("_MainTex", texture);
}
}
}
}