-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathSkeletalTrackingProvider.cs
More file actions
134 lines (116 loc) · 5.93 KB
/
SkeletalTrackingProvider.cs
File metadata and controls
134 lines (116 loc) · 5.93 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
using Microsoft.Azure.Kinect.BodyTracking;
using Microsoft.Azure.Kinect.Sensor;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;
public class SkeletalTrackingProvider : BackgroundDataProvider
{
bool readFirstFrame = false;
TimeSpan initialTimestamp;
public SkeletalTrackingProvider(int id) : base(id)
{
Debug.Log("in the skeleton provider constructor");
}
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter { get; set; } = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
public Stream RawDataLoggingFile = null;
protected override void RunBackgroundThreadAsync(int id, CancellationToken token)
{
try
{
UnityEngine.Debug.Log("Starting body tracker background thread.");
// Buffer allocations.
BackgroundData currentFrameData = new BackgroundData();
// Open device.
using (Device device = Device.Open(id))
{
device.StartCameras(new DeviceConfiguration()
{
CameraFPS = FPS.FPS30,
ColorResolution = ColorResolution.Off,
DepthMode = DepthMode.NFOV_Unbinned,
WiredSyncMode = WiredSyncMode.Standalone,
});
UnityEngine.Debug.Log("Open K4A device successful. id " + id + "sn:" + device.SerialNum);
var deviceCalibration = device.GetCalibration();
using (Tracker tracker = Tracker.Create(deviceCalibration, new TrackerConfiguration() { ProcessingMode = TrackerProcessingMode.Gpu, SensorOrientation = SensorOrientation.Default }))
{
UnityEngine.Debug.Log("Body tracker created.");
while (!token.IsCancellationRequested)
{
using (Capture sensorCapture = device.GetCapture())
{
// Queue latest frame from the sensor.
tracker.EnqueueCapture(sensorCapture);
}
// Try getting latest tracker frame.
using (Frame frame = tracker.PopResult(TimeSpan.Zero, throwOnTimeout: false))
{
if (frame == null)
{
UnityEngine.Debug.Log("Pop result from tracker timeout!");
}
else
{
IsRunning = true;
// Get number of bodies in the current frame.
currentFrameData.NumOfBodies = frame.NumberOfBodies;
// Copy bodies.
for (uint i = 0; i < currentFrameData.NumOfBodies; i++)
{
currentFrameData.Bodies[i].CopyFromBodyTrackingSdk(frame.GetBody(i), deviceCalibration);
}
// Store depth image.
Capture bodyFrameCapture = frame.Capture;
Image depthImage = bodyFrameCapture.Depth;
if (!readFirstFrame)
{
readFirstFrame = true;
initialTimestamp = depthImage.DeviceTimestamp;
}
currentFrameData.TimestampInMs = (float)(depthImage.DeviceTimestamp - initialTimestamp).TotalMilliseconds;
currentFrameData.DepthImageWidth = depthImage.WidthPixels;
currentFrameData.DepthImageHeight = depthImage.HeightPixels;
// Read image data from the SDK.
var depthFrame = MemoryMarshal.Cast<byte, ushort>(depthImage.Memory.Span);
// Repack data and store image data.
int byteCounter = 0;
currentFrameData.DepthImageSize = currentFrameData.DepthImageWidth * currentFrameData.DepthImageHeight * 3;
for (int it = currentFrameData.DepthImageWidth * currentFrameData.DepthImageHeight - 1; it > 0; it--)
{
byte b = (byte)(depthFrame[it] / (ConfigLoader.Instance.Configs.SkeletalTracking.MaximumDisplayedDepthInMillimeters) * 255);
currentFrameData.DepthImage[byteCounter++] = b;
currentFrameData.DepthImage[byteCounter++] = b;
currentFrameData.DepthImage[byteCounter++] = b;
}
if (RawDataLoggingFile != null && RawDataLoggingFile.CanWrite)
{
binaryFormatter.Serialize(RawDataLoggingFile, currentFrameData);
}
// Update data variable that is being read in the UI thread.
SetCurrentFrameData(ref currentFrameData);
}
}
}
Debug.Log("dispose of tracker now!!!!!");
tracker.Dispose();
}
device.Dispose();
}
}
catch (Exception e)
{
Debug.Log($"catching exception for background thread {e.Message}");
token.ThrowIfCancellationRequested();
}
finally
{
IsRunning = false;
if (RawDataLoggingFile != null)
{
RawDataLoggingFile.Close();
}
}
}
}