-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
323 lines (247 loc) · 10.7 KB
/
Copy pathProgram.cs
File metadata and controls
323 lines (247 loc) · 10.7 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
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
using Microsoft.Kinect;
using System;
using System.IO;
using static System.Net.Mime.MediaTypeNames;
using System.Globalization;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.Net;
public class KinectInputController
{
public SkeletonStream Start(KinectSensor kinectSensor)
{
kinectSensor.Start();
Console.WriteLine("Kinnect processes have started!");
//kinectSensor.ElevationAngle = 14;
return StartStream(kinectSensor);
}
public SkeletonStream StartStream(KinectSensor kinectSensor)
{
SkeletonStream stream = kinectSensor.SkeletonStream;
stream.Enable();
stream.EnableTrackingInNearRange= true;
stream.TrackingMode = SkeletonTrackingMode.Default;
return stream;
}
public Boolean Loop(KinectSensor kinectSensor, SkeletonStream stream)
{
int i = 0;
int old_best_trackable_pt_x = 0;
int old_best_trackable_pt_y = 0;
int old_best_trackable_pt_z = 0;
int old_act = 0;
int lastTrackedVal = -1;
while (i < 1000000)
{
SkeletonFrame frame = stream.OpenNextFrame(0);
if (frame != null)
{
Skeleton[] skeletonArr = new Skeleton[frame.SkeletonArrayLength];
frame.CopySkeletonDataTo(skeletonArr);
if (skeletonArr.Length > 0 && skeletonArr[0] != null)
{
Skeleton trackedSkeleton = skeletonArr[0];
JointCollection trackedSkeletonJoints = trackedSkeleton.Joints;
JointType jointType = JointType.Head; // Replace with desired joint type
Joint bestTrackableJoint = trackedSkeletonJoints[jointType];
Position coords = new Position(0, 0, 3);
Jointy darkestTrackablePoint = new Jointy(coords);
//Step 3: Trig
//p: darkest point, k: kinect point(origin), j: person (head) point, Sides are across from corresponding angles
double dotOfPandJ = bestTrackableJoint.Position.X * darkestTrackablePoint.Position.X +
bestTrackableJoint.Position.Z * darkestTrackablePoint.Position.Z;
double J = Math.Sqrt(Math.Pow(bestTrackableJoint.Position.X, 2) +
Math.Pow(bestTrackableJoint.Position.Z, 2));
double P = Math.Sqrt(Math.Pow(darkestTrackablePoint.Position.X, 2) +
Math.Pow(darkestTrackablePoint.Position.Z, 2));
double k = (Math.PI / 2) - (Math.Asin((dotOfPandJ) / (J * P)));
double K = Math.Sqrt(Math.Pow(J, 2) * Math.Pow(P, 2) - 2 * J * P * Math.Cos(k));
double p = Math.Asin((Math.Sin(k) * K) / P) * (180 / Math.PI);
p = bestTrackableJoint.Position.X > 0 ? -p : p;
if (i % 100 == 0)
{
Console.WriteLine("[Frame " + i + "] Coords: X: " + bestTrackableJoint.Position.X + " Y: " + bestTrackableJoint.Position.Y + " Z: " + bestTrackableJoint.Position.Z);
}
if (bestTrackableJoint.Position.X != 0 || bestTrackableJoint.Position.Y != 0 || bestTrackableJoint.Position.Z != 0)
{
lastTrackedVal = i;
//Console.WriteLine("[Frame " + i + "] Coords: X: " + bestTrackableJoint.Position.X + " Y: " + bestTrackableJoint.Position.Y + " Z: " + bestTrackableJoint.Position.Z);
//Console.WriteLine("Angle: " + p + "°\n");
//Console.WriteLine("Debugging: J=" + J + ", P=" + P + ", k=" + k * (180 / Math.PI) + ", K=" + K);
p *= 3;
int act = (int)p + 188;
//Console.WriteLine("ACT: " + p);
if (act < 110)
{
act = 110;
}
else if (act > 255)
act = 255;
//Console.WriteLine($"ACT: {act}");
//Console.WriteLine($"OLD-ACT: {old_act}");
if (Math.Abs(old_act - act) > 0)
{
//Console.WriteLine($"writiing to https://www.xcribbage.com/api/v0/push?s={act}");
Send(act);
Console.WriteLine("[Frame " + i + "] Coords: X: " + bestTrackableJoint.Position.X + " Y: " + bestTrackableJoint.Position.Y + " Z: " + bestTrackableJoint.Position.Z);
old_act = act;
}
}
else if(i - lastTrackedVal > 40)
{
EndStream(kinectSensor);
//Console.WriteLine("OFF");
StartStream(kinectSensor);
//Console.WriteLine("ON");
lastTrackedVal = i;
}
//int actuator_val =
//Step 4: Calculate linear actuator position
//Step 5: Send results to server
//Step 6: Disposes of the skeleton frame
/*old_best_trackable_pt_x = (int)bestTrackableJoint.Position.X;
old_best_trackable_pt_y = (int)bestTrackableJoint.Position.Y;
old_best_trackable_pt_z = (int)bestTrackableJoint.Position.Z;*/
i++;
}
frame.Dispose();
}
}
return true;
}
public String Send(int act)
{
string url = "https://www.xcribbage.com/api/v0/push?s=" + act;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET"; // or "POST" if the API expects a POST request
try
{
// Get the response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Read the response (optional, if needed)
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (var reader = new System.IO.StreamReader(responseStream))
{
string responseText = reader.ReadToEnd();
//Console.WriteLine("Response received: " + responseText);
return responseText;
}
}
}
}
}
catch (WebException ex)
{
// Handle exceptions
Console.WriteLine("Error: " + ex.Message);
return "ERROR";
}
return "";
}
public Boolean EndStream(KinectSensor kinectSensor)
{
SkeletonStream stream = kinectSensor.SkeletonStream;
stream.Disable();
stream.EnableTrackingInNearRange = true;
stream.AppChoosesSkeletons = false; //Change later if you want to change how skeletons are tracked
return true;
}
public void Stop(KinectSensor kinectSensor)
{
EndStream(kinectSensor);
//kinectSensor.ElevationAngle = kinectSensor.MaxElevationAngle;
kinectSensor.Stop();
Console.WriteLine("Kinnect processes have stopped!");
}
}
class MainClass
{
static void Main(string[] args)
{
KinectInputController controller = new KinectInputController();
// Find a Kinect sensor
KinectSensorCollection kinectSensors = KinectSensor.KinectSensors;
KinectSensor kinectSensor = kinectSensors[0];
SkeletonStream stream = controller.Start(kinectSensor);
controller.Loop(kinectSensor, stream);
controller.Stop(kinectSensor);
// Wait for input to keep the console window open
Console.ReadKey();
}
public void TakePicture(KinectSensor kinectSensor)
{
ColorImageStream stream = kinectSensor.ColorStream;
stream.Enable();
ColorImageFrame frame = stream.OpenNextFrame(1000);
if (frame != null)
{
try
{
int width = frame.Width;
int height = frame.Height;
byte[] pixelData = new byte[frame.PixelDataLength];
frame.CopyPixelDataTo(pixelData);
string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat);
string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
string path = Path.Combine(myPhotos, "KinectSnapshot-" + time + ".png");
// MyPic() { };
using (FileStream fs = new FileStream(path, FileMode.Create))
{
// encoder.Save(fs);
}
/*MemoryStream ms = new MemoryStream();
ms.Write(pixelData, 0, pixelData.Length);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);*/
}
catch
{
Console.WriteLine("There was an error with saving the image");
}
finally
{
frame.Dispose();
}
}
else
{
// Handle the case where no frame was available within the timeout period.
Console.WriteLine("No color frame available within the timeout period.");
}
}
private static void SaveImage(ColorImageFrame data, Stream saveStream)
{
// var encoder = BitmapEncoder.Create(data.Decoder.CodecInfo.ContainerFormat);
// encoder.Frames.Add(data);
using (var memoryStream = new MemoryStream())
{
// encoder.Save(memoryStream);
memoryStream.Position = 0;
memoryStream.CopyTo(saveStream);
}
}
}
public struct Position
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public Position(int x, int y, int z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
}
public struct Jointy
{
public Position Position;
public Jointy(Position coords)
{
this.Position = coords;
}
}