-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOSCeleton.cs
328 lines (291 loc) · 12.4 KB
/
OSCeleton.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
using Microsoft.Kinect;
using Microsoft.Kinect.Face;
using Microsoft.Samples.Kinect.FaceBasics;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Ventuz.OSC;
using OSCeleton;
namespace OSCeleton
{
class OSCeleton
{
// Settings
private bool allUsers = true;
private bool handsOnly = false;
private bool faceTracking = false;
private bool writeOSC = true;
private bool writeCSV = false;
private bool useUnixEpochTime = true;
private String oscHost = "127.0.0.1";
private int oscPort = 7110;
private const int skeletonCount = 6;
private const int pointScale = 1000;
// Outputs
private bool capturing = true;
private BlockingCollection<TrackingInformation> trackingInformationQueue = new BlockingCollection<TrackingInformation>();
Thread sendTracking;
private UdpWriter osc;
private StreamWriter fileWriter;
private Stopwatch stopwatch;
public void Initialise()
{
// Install Shortcut
CheckForShortcut();
// Parse commandline arguments
string[] args = Environment.GetCommandLineArgs();
for (int index = 1; index < args.Length; index += 2)
{
args[index] = args[index].ToLower();
if ("allUsers".ToLower().Equals(args[index])) allUsers = StringToBool(args[index + 1]);
if ("handsOnly".ToLower().Equals(args[index])) handsOnly = StringToBool(args[index + 1]);
if ("faceTracking".ToLower().Equals(args[index])) faceTracking = StringToBool(args[index + 1]);
if ("writeOSC".ToLower().Equals(args[index])) writeOSC = StringToBool(args[index + 1]);
if ("writeCSV".ToLower().Equals(args[index])) writeCSV = StringToBool(args[index + 1]);
if ("useUnixEpochTime".ToLower().Equals(args[index])) useUnixEpochTime = StringToBool(args[index + 1]);
if ("oscHost".ToLower().Equals(args[index])) oscHost = args[index + 1];
if ("oscPort".ToLower().Equals(args[index]))
{
if (!int.TryParse(args[index + 1], out oscPort))
{
System.Windows.MessageBox.Show("Failed to parse the oscPort argument: " + args[index + 1]);
}
}
}
// Initialisation
stopwatch = new Stopwatch();
stopwatch.Reset();
stopwatch.Start();
if (writeOSC)
{
osc = new UdpWriter(oscHost, oscPort);
}
if (writeCSV)
{
OpenNewCSVFile();
}
if (sendTracking == null)
{
sendTracking = new Thread(SendTrackingInformation);
sendTracking.Start();
}
}
public void Stop()
{
capturing = false;
if (sendTracking != null)
{
sendTracking.Abort();
sendTracking = null;
}
}
public bool GetHandsOnly()
{
return handsOnly;
}
public void SwitchHandsOnly(bool handsOnly)
{
this.handsOnly = handsOnly;
}
public bool GetFaceTracking()
{
return faceTracking;
}
public void OpenNewCSVFile()
{
if (!writeCSV) return;
CloseCSVFile();
StreamWriter fileWriter = InitCSVFile();
if (this.fileWriter != null)
{
lock (fileWriter)
{
this.fileWriter = fileWriter;
}
}
else
{
this.fileWriter = fileWriter;
}
}
public StreamWriter InitCSVFile()
{
StreamWriter fileWriter = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/points-MSK2-" + getUnixEpochTime().ToString().Replace(",", ".") + ".csv", false);
fileWriter.WriteLine("Joint, sensor, user, joint, x, y, z, confidence, time");
fileWriter.WriteLine("HandState, sensor, user, joint, x, y, z, confidence, state, stateConfidence, time");
fileWriter.WriteLine("Face, sensor, user, pitch, yaw, roll, time");
fileWriter.WriteLine("FaceProperty, sensor, user, happy, engaged, wearingGlasses, leftEyeClosed, rightEyeClosed, mouthOpen, mouthMoved, lookingAway, time");
return fileWriter;
}
private void CloseCSVFile()
{
if (fileWriter != null)
{
lock (fileWriter)
{
if (fileWriter != null)
{
fileWriter.Close();
fileWriter = null;
}
}
}
}
public void CheckForShortcut()
{
try
{
if (System.Diagnostics.Debugger.IsAttached)
{
return;
}
System.Deployment.Application.ApplicationDeployment ad = default(System.Deployment.Application.ApplicationDeployment);
ad = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
if ((ad.IsFirstRun))
{
System.Reflection.Assembly code = System.Reflection.Assembly.GetExecutingAssembly();
string company = string.Empty;
string description = string.Empty;
if ((Attribute.IsDefined(code, typeof(System.Reflection.AssemblyCompanyAttribute))))
{
System.Reflection.AssemblyCompanyAttribute ascompany = null;
ascompany = (System.Reflection.AssemblyCompanyAttribute)Attribute.GetCustomAttribute(code, typeof(System.Reflection.AssemblyCompanyAttribute));
company = ascompany.Company;
}
if ((Attribute.IsDefined(code, typeof(System.Reflection.AssemblyTitleAttribute))))
{
System.Reflection.AssemblyTitleAttribute asdescription = null;
asdescription = (System.Reflection.AssemblyTitleAttribute)Attribute.GetCustomAttribute(code, typeof(System.Reflection.AssemblyTitleAttribute));
description = asdescription.Title;
}
if ((company != string.Empty & description != string.Empty))
{
//description = Replace(description, "_", " ")
string desktopPath = string.Empty;
desktopPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "\\", description, ".appref-ms");
string shortcutName = string.Empty;
shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", company, "\\", description, ".appref-ms");
if (!File.Exists(shortcutName))
shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", company, "\\", description, " - 1 .appref-ms");
if (!File.Exists(shortcutName))
shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", company, "\\", description, " - 2 .appref-ms");
System.IO.File.Copy(shortcutName, desktopPath, true);
}
else
{
System.Windows.MessageBox.Show("Missing company or description: " + company + " - " + description);
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(GetErrorText(ex));
}
}
public void EnqueueBody(int sensorId, int user, Body b)
{
if (!capturing) { return; }
if (b == null) { return; }
trackingInformationQueue.Add(new BodyTrackingInformation(sensorId, user, b, handsOnly, getTime()));
}
float detectionResultToConfidence(DetectionResult r)
{
switch (r)
{
case DetectionResult.Unknown:
return 0.5f;
case DetectionResult.Maybe:
return 0.5f;
case DetectionResult.No:
return 0f;
case DetectionResult.Yes:
return 1f;
default:
return 0.5f;
}
}
public void EnqueueFaceTracking(int sensorId, int user, Microsoft.Kinect.Face.FaceFrameResult faceResult)
{
if (!capturing) { return; }
if (faceResult == null) { return; }
// extract face rotation in degrees as Euler angles
if (faceResult.FaceRotationQuaternion != null)
{
int pitch, yaw, roll;
MainWindow.ExtractFaceRotationInDegrees(faceResult.FaceRotationQuaternion, out pitch, out yaw, out roll);
trackingInformationQueue.Add(new FaceRotationTrackingInformation(sensorId, user, pitch, yaw, roll, getTime()));
}
// extract each face property information and store it in faceText
if (faceResult.FaceProperties != null)
{
trackingInformationQueue.Add(new FacePropertyTrackingInformation(sensorId, user,
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.Happy]),
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.Engaged]),
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.WearingGlasses]),
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.LeftEyeClosed]),
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.RightEyeClosed]),
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.MouthOpen]),
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.MouthMoved]),
detectionResultToConfidence(faceResult.FaceProperties[FaceProperty.LookingAway]),
getTime()));
}
}
void SendTrackingInformation()
{
while (true)
{
TrackingInformation i = trackingInformationQueue.Take();
if (i != null && capturing)
{
if (fileWriter != null)
{
lock (fileWriter)
{
i.Send(pointScale, osc, fileWriter);
}
}
else
{
i.Send(pointScale, osc, fileWriter);
}
}
}
}
private double getTime()
{
if (useUnixEpochTime)
return getUnixEpochTime();
return stopwatch.ElapsedMilliseconds;
}
private double getUnixEpochTime()
{
var unixTime = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return unixTime.TotalMilliseconds;
}
private long getUnixEpochTimeLong()
{
var unixTime = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return System.Convert.ToInt64(unixTime.TotalMilliseconds);
}
private string GetErrorText(Exception ex)
{
string err = ex.Message;
if (ex.InnerException != null)
{
err += " - More details: " + ex.InnerException.Message;
}
return err;
}
bool StringToBool(String msg)
{
msg = msg.ToLower();
return msg.Equals("1") || msg.ToLower().Equals("true");
}
}
}